C# LinkedIn oAuth RequestToken“;401“未经授权”;

C# LinkedIn oAuth RequestToken“;401“未经授权”;,c#,api,oauth,linkedin,C#,Api,Oauth,Linkedin,我正试图通过oAuth获取LinkedIn API的RequestToken。我已经尝试了很多方法,阅读了所有我能找到的与这个主题相关的文档,并且始终如一地尝试了我能想到的所有解决方案 我使用的代码如下: public Dictionary<string, string> GetAccessToken() { Dictionary<string, string> tokens = new Dictionary<string, str

我正试图通过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;
    }
公共字典GetAccessToken()
{
字典标记=新字典();
字符串url=string.Format(“https://api.linkedin.com/uas/oauth/requestToken");
HttpWebRequest-request=WebRequest.Create(url)为HttpWebRequest;
字符串头=string.Format(@“OAuth OAuth\u nonce=”“{0}”、OAuth\u签名方法=“{1}”、OAuth\u时间戳=“{2}”、OAuth\u消费者密钥=“{3}”、OAuth\u签名=“{4}”、OAuth\u版本=“{5}”,
HttpUtility.UrlEncode(generateOnce()),HttpUtility.UrlEncode(getSignatureMethod()),HttpUtility.UrlEncode(GenerateTimeStamp()),HttpUtility.UrlEncode(getClientID()),HttpUtility.UrlEncode(getClientSecret()),HttpUtility.UrlEncode(getVersion());
添加(“授权”,标题);
request.Method=“POST”;
HRArtis_Security.MyProxy HRProxy=新的HRArtis_Security.MyProxy();
request.Proxy=HRProxy;
使用(HttpWebResponse=request.GetResponse()作为HttpWebResponse)
{
StreamReader=新的StreamReader(response.GetResponseStream());
string retVal=reader.ReadToEnd();
foreach(retVal.Split('&')中的字符串标记)
{
tokens.Add(token.Substring(0,token.IndexOf(“=”)),
子字符串(token.IndexOf(“=”)+1,token.Length-token.IndexOf(“=”-1));
}
}
归还代币;
}
然后,我向该URL执行一个POST。 尝试获取行
HttpWebResponse=request.GetResponse()为HttpWebResponse

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


我是遗漏了什么还是做了个白痴?

在我看来,您对oAuth头的配置有误。我建议您使用吊床或RestSharp library向LinkedIn授权。下面是几个例子:和(LinkedIn和Twitter都在使用oAuth v1.0)。

我以前看过这些例子。但我会给他们一个机会,看看它是否有效(如果有效,我会把你的答案标记为接受):)。谢谢。我成功地实现了RestSharp库。谢谢你的帮助,太好了!如果希望使用accessToken获得永久授权,请不要忘记保存oauth验证器。这是LinkedIn和TwitterOAuth实现之间的一个区别。我已经领先并拥有accessToken。一旦我破解了令牌,其余的就不难了。再次感谢=)。我以前使用过Facebook API,这也有点不同。