简体   繁体   English

领英oAuth RequestToken“ 401未经授权”

[英]LinkedIn oAuth RequestToken “401 Unauthorized”

I am attempting to acquire a RequestToken for the LinkedIn API via oAuth. 我正在尝试通过oAuth获取LinkedIn API的RequestToken。 I have tried numerous things and read all of the documentation I can find related to the topic, and consiquently tried all of the solutions I could think of. 我尝试了很多事情,阅读了可以找到的与该主题相关的所有文档,因此尝试了所有我能想到的解决方案。

The code I am using is as follows: 我使用的代码如下:

    public Dictionary<string, string> GetAccessToken()
    {

        Dictionary<string, string> tokens = new Dictionary<string, string>();
        string url = string.Format("https://api.linkedin.com/uas/oauth/requestToken");
        HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;

        string header = string.Format(@"OAuth oauth_nonce=""{0}"", oauth_signature_method=""{1}"", oauth_timestamp=""{2}"", oauth_consumer_key=""{3}"", oauth_signature=""{4}"", oauth_version=""{5}""",
        HttpUtility.UrlEncode(GenerateNonce()), HttpUtility.UrlEncode(getSignatureMethod()), HttpUtility.UrlEncode(GenerateTimeStamp()), HttpUtility.UrlEncode(getClientID()), HttpUtility.UrlEncode(getClientSecret()), HttpUtility.UrlEncode(getVersion()));

        request.Headers.Add("Authorization", header);
        request.Method = "POST";

        HRArtis_Security.MyProxy HRProxy = new HRArtis_Security.MyProxy();
        request.Proxy = HRProxy;
        using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
        {
            StreamReader reader = new StreamReader(response.GetResponseStream());
            string retVal = reader.ReadToEnd();

            foreach (string token in retVal.Split('&'))
            {
                tokens.Add(token.Substring(0, token.IndexOf("=")),
                    token.Substring(token.IndexOf("=") + 1, token.Length - token.IndexOf("=") - 1));
            }
        }
        return tokens;
    }

I then perform a POST to that URL. 然后,我对该URL执行POST。 The error occurs when trying to get the response with the line HttpWebResponse response = request.GetResponse() as HttpWebResponse 尝试获取以HttpWebResponse response = request.GetResponse() as HttpWebResponse响应的HttpWebResponse response = request.GetResponse() as HttpWebResponse行的响应时发生错误

Another option I tried was adding those parameters to the header with no result. 我尝试的另一种选择是将这些参数添加到标题中而没有结果。

Am I missing something or being an idiot? 我是想念东西还是白痴?

Seems to me you have misconfigured oAuth header. 在我看来,您的oAuth标头配置错误。 I suggest you to use Hammock or RestSharp library to authorize with LinkedIn. 我建议您使用HammockRestSharp库通过LinkedIn进行授权。 Here are several examples how to do this: Hammock and RestSharp (LinkedIn and Twitter both are using oAuth v1.0). 以下是如何执行此操作的几个示例: HammockRestSharp (LinkedIn和Twitter都使用oAuth v1.0)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM