LinkedIn oAuth RequestToken“401 Unauthorized”

时间:2011-09-13 13:42:14

标签: c# api oauth linkedin

我试图通过oAuth获取LinkedIn API的RequestToken。我已经尝试了很多东西并阅读了我能找到的与该主题相关的所有文档,并且巧妙地尝试了我能想到的所有解决方案。

我使用的代码如下:

    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;
    }

然后我对该URL执行POST。 尝试使用行HttpWebResponse response = request.GetResponse() as HttpWebResponse

获取响应时发生错误

我尝试的另一个选项是将这些参数添加到标题中,但没有结果。

我错过了什么或是个白痴?

1 个答案:

答案 0 :(得分:0)

在我看来,您的oAuth标头配置错误。我建议您使用 Hammock RestSharp 库来授权LinkedIn。以下是如何执行此操作的几个示例:HammockRestSharp(LinkedIn和Twitter都使用oAuth v1.0)。