开发者

LinkedIn oAuth RequestToken "401 Unauthorized"

I am attempting to acquire a RequestToken for the LinkedIn API via oAuth. 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_sig开发者_运维问答nature=""{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. The error occurs when trying to get the response with the line 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. I suggest you to use Hammock or RestSharp library to authorize with LinkedIn. Here are several examples how to do this: Hammock and RestSharp (LinkedIn and Twitter both are using oAuth v1.0).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜