【问题标题】:c# - Cannot implicitly convert type 'oAuthCredentials' to 'string' [closed]c# - 无法将类型“oAuthCredentials”隐式转换为“字符串”[关闭]
【发布时间】:2013-06-19 20:50:14
【问题描述】:

我正在尝试使用 API 从linkedin 获取 cmets

但我在少数地方遇到了错误

谁能帮我解决一个错误?

我已经写了这段代码

 protected void Page_Load(object sender, EventArgs e)
    {
        var request = new RestRequest { Path = "posts?order=recency&category=discussion" };

        var credentials = new OAuthCredentials

        {
            ConsumerKey = "kmkzt9cqml1s", 
            ConsumerSecret = "hNiwIrZWGSMykoD2", 
        };


        var client = new RestClient()
        {
            Authority = "http://api.linkedin.com/v1/groups/345455/",
            Credentials = credentials//Here I'm getting an error
        };

        //Here how to create Request method
        var MyInfo = client.Request(request);

        String content = MyInfo.Content;

        string strURL = "http://api.linkedin.com/";
        System.Net.HttpWebRequest objWebRequest = null;
        System.Net.HttpWebResponse objWebResponse = null;
        System.IO.StreamReader streamReader = null;
        string strHTML = null;

        System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate(object s, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors) { return true; };

        objWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(strURL);
        objWebRequest.PreAuthenticate = true;
        objWebRequest.Method = "GET";

        objWebRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;

        objWebResponse = (System.Net.HttpWebResponse)objWebRequest.GetResponse();

        streamReader = new System.IO.StreamReader(objWebResponse.GetResponseStream());

        strHTML = streamReader.ReadToEnd();


        streamReader.Close();
        objWebResponse.Close();
        objWebRequest.Abort();

        Response.ContentType = "text/xml";
        Response.Write(strHTML);
        Response.End();

    }  

RestClient.cs

 public class RestClient
  {
    private string _consumerKey = "";
    private string _consumerSecret = "";

    #region Properties
    public string Authority
    {
        get
        {
            return _consumerKey;
        }
        set { _consumerKey = value; }
    }

    //Here giving string
    public string Credentials
    {
        get
        {
            return _consumerSecret;
        }
        set { _consumerSecret = value; }
    }
    #endregion
}

如何创建 Request() 方法? 有任何想法吗?提前致谢。

【问题讨论】:

  • 由无效的隐式转换导致的主题异常。检查编译器指向您的行并解决问题。您提供的代码太常见了。
  • 双击错误,您将进入导致错误的行。将鼠标悬停在分配的左侧和右侧以查看您使用的数据类型不兼容。

标签: c# .net


【解决方案1】:

正如错误所说,您正在尝试将OAuthCredentials 对象(凭据)分配给RestClient 对象的字符串类型属性:Credentials

您必须要么做Credentials = credentials.toString() 要么更改RestClient 类。

【讨论】:

【解决方案2】:

您的RestClient 有一个Credentials 属性,它是一个字符串;您的代码试图将不是字符串的内容(在您的情况下为OAuthCredentials)放入其中。这是不允许的,因为这两种类型之间没有隐式转换。

【讨论】:

    【解决方案3】:

    RestClient 中,Credentials 是字符串,但您正在影响OAuthCredentialsCredentials = credentials)。

    尝试将属性类型(和支持字段)Credentials 更改为 OAuthCredentials

    【讨论】:

      【解决方案4】:

      Credentials 不是OAuthCredentials 类型,它是string 类型,所以你不能用credentials 变量分配它,它是OAuthCredentials 类型。由于RestClient 类引用了一个名为_consumerSecret 的变量,所以假设您确实想要这样做:

      Authority = credentials.ConsumerKey;
      Credentials = credentials.ConsumerSecret;
      

      【讨论】:

        猜你喜欢
        • 2022-01-24
        • 2016-09-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-09
        相关资源
        最近更新 更多