c# - 无法将类型'oAuthCredentials'隐式转换为'string'

时间:2021-08-20 19:19:23

I'm trying to get comments from linkedin by using API

我试图通过使用API​​来获取来自linkedin的评论

But I'm getting an error, in few places

但是我在一些地方遇到了错误

Can anyone help me to resolve an error?

任何人都可以帮我解决错误吗?

I've written this code

我写了这段代码

 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

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
}

How to create Request() Method? Any ideas? Thanks in advance.

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

4 个解决方案

#1


0  

As the error says, you're trying to assign a OAuthCredentials object (credentials) into a string typed attribute of the RestClient object : Credentials.

如错误所示,您尝试将OAuthCredentials对象(凭据)分配到RestClient对象的字符串类型属性:凭据。

You have to either do Credentials = credentials.toString() or change the RestClient class.

您必须执行Credentials = credentials.toString()或更改RestClient类。

#2


0  

Your RestClient has a Credentials property that is a string; Your code is trying to put something that isn't a string (in your case, an OAuthCredentials) into it. This is not a allowable, as there is no implicit conversion between the two types.

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

#3


0  

In RestClient, Credentials is string, but you are affecting a OAuthCredentials to it ( Credentials = credentials ).

在RestClient中,凭据是字符串,但您正在影响它的OAuthCredentials(凭据=凭据)。

Try changing the type of the property (and the backing field) Credentials to OAuthCredentials.

尝试将属性(和支持字段)的凭据类型更改为OAuthCredentials。

#4


0  

Credentials is not of type OAuthCredentials, it's of type string, so you can't assign it with your credentials variable, which is of type OAuthCredentials. Since the RestClient class references a variable named _consumerSecret, and such and so, let's assume you actually want to do this:

凭据不是OAuthCredentials类型,它的类型为字符串,因此您无法使用您的凭证变量(OAuthCredentials类型)对其进行分配。由于RestClient类引用了一个名为_consumerSecret的变量,所以我们假设你真的想要这样做:

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

#1


0  

As the error says, you're trying to assign a OAuthCredentials object (credentials) into a string typed attribute of the RestClient object : Credentials.

如错误所示,您尝试将OAuthCredentials对象(凭据)分配到RestClient对象的字符串类型属性:凭据。

You have to either do Credentials = credentials.toString() or change the RestClient class.

您必须执行Credentials = credentials.toString()或更改RestClient类。

#2


0  

Your RestClient has a Credentials property that is a string; Your code is trying to put something that isn't a string (in your case, an OAuthCredentials) into it. This is not a allowable, as there is no implicit conversion between the two types.

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

#3


0  

In RestClient, Credentials is string, but you are affecting a OAuthCredentials to it ( Credentials = credentials ).

在RestClient中,凭据是字符串,但您正在影响它的OAuthCredentials(凭据=凭据)。

Try changing the type of the property (and the backing field) Credentials to OAuthCredentials.

尝试将属性(和支持字段)的凭据类型更改为OAuthCredentials。

#4


0  

Credentials is not of type OAuthCredentials, it's of type string, so you can't assign it with your credentials variable, which is of type OAuthCredentials. Since the RestClient class references a variable named _consumerSecret, and such and so, let's assume you actually want to do this:

凭据不是OAuthCredentials类型,它的类型为字符串,因此您无法使用您的凭证变量(OAuthCredentials类型)对其进行分配。由于RestClient类引用了一个名为_consumerSecret的变量,所以我们假设你真的想要这样做:

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