简体   繁体   English

当我尝试使用DotNetOpenAuth访问其API时,从LinkedIn获得HTTP 400错误

[英]Getting a HTTP 400 Error from LinkedIn when I try to access its API with DotNetOpenAuth

I'm trying to write a basic program using DotNetOpenAuth to search for people on behalf of a user. 我正在尝试使用DotNetOpenAuth编写一个基本程序来代表用户搜索人。 Just a simple program to try to learn about OAuth. 只是一个简单的程序,可以尝试了解OAuth。 Right now I'm stuck trying to authorize, but when I try to do so I get a HTTP 400 status code from linkedIn. 目前,我一直在尝试进行授权,但是当我尝试进行授权时,我从linkedIn得到了HTTP 400状态代码。

I've tried using both DesktopConsumers and WebConsumers, both of which fail in the same way. 我试过同时使用DesktopConsumers和WebConsumers,它们都以相同的方式失败。 I know that the specific function that is caused the exceptioin is the call to RequestUserAuthorization on line 34 and I've tried using both empty dictionaries and nulls as inputs for it, but nothing's happened. 我知道引起异常的特定函数是对第34行的RequestUserAuthorization的调用,我尝试使用空字典和null作为输入,但是什么也没发生。

I'm using two classes for the program a TestLinkedIn.cs class containing the main and a LinkedInTokenManager.cs class which is a pretty straightforward implementation of IConsumerTokenManager. 我在程序中使用了两个类,一个包含main的TestLinkedIn.cs类和一个LinkedInTokenManager.cs类,这是IConsumerTokenManager的非常简单的实现。

Here's the code for TestLinkedIn.cs. 这是TestLinkedIn.cs的代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DotNetOpenAuth.Messaging;
using DotNetOpenAuth.OAuth.Messages;
using DotNetOpenAuth.OAuth;
using DotNetOpenAuth.OAuth.ChannelElements;

namespace Project
{
    class TestLinkedIn
    {
        #region Variables
        private static string APIKey = "APIKey";
        private static string APISecret = "APISecret";
        private static string OAuthToken = "OAuthToken";
        private static string OAuthSecret = "OAuthSecret";
        #endregion

        static void Main(string[] args)
        {
            ServiceProviderDescription linkedIn = LinkedInDescription();
            LinkedInTokenManager tokenManager = new LinkedInTokenManager(OAuthToken, OAuthSecret);//APIKey,APISecret);//
            DesktopConsumer web = new DesktopConsumer(linkedIn,tokenManager); 
            string requestToken = "";
            Uri authUrl = new Uri("https://steve.com");// ("https://api.linkedin.com/uas/oauth/requestToken");//"http" + "://" + "api.linkedin.com" + "/Home/OAuthCallBack");
            Dictionary<string, string> empty = new Dictionary<string, string>();
            try{
                //UserAuthorizationRequest request = web.PrepareRequestUserAuthorization(null, null, null, out requestToken);//null, null, null);//authUrl, null, null);
                //UserAuthorizationRequest request =web.Channel.Request(new AccessProtectedResourceRequest());
                //web.Channel.Send(request);
                authUrl = web.RequestUserAuthorization(empty, empty, out requestToken);
                Console.WriteLine(requestToken);
                Console.ReadKey();
            }
            catch (ProtocolException e)
            {
                Console.Write(e.StackTrace);
                Console.WriteLine("Error detected");
                //Console.ReadKey();
            }
        }

        private static ServiceProviderDescription LinkedInDescription(){
            ServiceProviderDescription linkedIn = new ServiceProviderDescription();
            linkedIn.AccessTokenEndpoint = new DotNetOpenAuth.Messaging.MessageReceivingEndpoint
                ("https://api.linkedin.com/uas/oauth/accessToken", DotNetOpenAuth.Messaging.HttpDeliveryMethods.PostRequest);
            linkedIn.RequestTokenEndpoint = new DotNetOpenAuth.Messaging.MessageReceivingEndpoint
                ("https://api.linkedin.com/uas/oauth/requestToken",DotNetOpenAuth.Messaging.HttpDeliveryMethods.PostRequest);
            linkedIn.UserAuthorizationEndpoint = new MessageReceivingEndpoint("https://www.linkedin.com/uas/oauth/authorize",
                HttpDeliveryMethods.PostRequest);
            linkedIn.TamperProtectionElements =new ITamperProtectionChannelBindingElement[]
                {new HmacSha1SigningBindingElement()};
            linkedIn.ProtocolVersion=ProtocolVersion.V10a;
            return linkedIn;
        }
    }
}

and here's the code for LinkedInTokenManager 这是LinkedInTokenManager的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DotNetOpenAuth.OAuth.ChannelElements;
using DotNetOpenAuth.OAuth.Messages;

namespace Project
{
    class LinkedInTokenManager : IConsumerTokenManager
    {
        private Dictionary<string, string> tokens = new Dictionary<string, string>();
        private string consumerKey;
        private string consumerSecret;

    public LinkedInTokenManager(string Key, string Secret)
    {
        consumerKey = Key;
        consumerSecret = Secret;
    }
    public string ConsumerKey
    {
        get { return consumerKey; }
    }

    public string ConsumerSecret
    {
        get { return consumerSecret; }
    }

    public void ExpireRequestTokenAndStoreNewAccessToken(string consumerKey, string requestToken, string accessToken, string accessTokenSecret)
    {
        tokens.Remove(requestToken);
        tokens[accessToken] = accessTokenSecret;
    }

    public string GetTokenSecret(string token)
    {
        try
        {
            return tokens[token];
        }
        catch (KeyNotFoundException k)
        {
            return null;
        }
    }

    public TokenType GetTokenType(string token)
    {
        throw new NotImplementedException();
    }

    public void StoreNewRequestToken(UnauthorizedTokenRequest request, ITokenSecretContainingMessage response)
    {
        tokens[response.Token] = response.TokenSecret;
    }
  }
}

This is the stuff I'm posting to the server. 这是我要发布到服务器的内容。

oauth_callback=oob&oauth_consumer_key=6415f7ed-d618-422a-b090-73f3056653d7&oauth_nonce=nGQjFcC1&oauth_signature_method=HMAC-SHA1&oauth_signature=XmUBfGVGDoBZDOC%2Bjp4Fj68MPGI%3D&oauth_version=1.0&oauth_timestamp=1357136418

and here is the stack trace from the error message. 这是错误消息中的堆栈跟踪。

at DotNetOpenAuth.Messaging.StandardWebRequestHandler.GetResponse(HttpWebRequest request, DirectWebRequestOptions options)
   at DotNetOpenAuth.Messaging.StandardWebRequestHandler.GetResponse(HttpWebRequest request)
       at DotNetOpenAuth.Messaging.Channel.GetDirectResponse(HttpWebRequest webRequest)
       at DotNetOpenAuth.Messaging.Channel.RequestCore(IDirectedProtocolMessage request)
       at DotNetOpenAuth.Messaging.Channel.Request(IDirectedProtocolMessage requestMessage)
       at DotNetOpenAuth.Messaging.Channel.Request[TResponse](IDirectedProtocolMessage requestMessage)
       at DotNetOpenAuth.OAuth.ConsumerBase.PrepareRequestUserAuthorization(Uri callback, IDictionary`2 requestParameters, IDictionary`2 redirectParameters, String& requestToken)
       at DotNetOpenAuth.OAuth.DesktopConsumer.RequestUserAuthorization(IDictionary`2 requestParameters, IDictionary`2 redirectParameters, String& requestToken)
       at APIphanySalesProject.TestLinkedIn.Main(String[] args) in c:\Users\Admin\Documents\Visual Studio 2012\Projects\Project\Project\TestLinkedIn.cs:line 34

Any thoughts? 有什么想法吗?

If you're encountering the same problem as I was, try checking your computer's time settings. 如果遇到与我相同的问题,请尝试检查计算机的时间设置。 Both the time and time zone need to be correct for it to generate correct timestamps, which is what was wrong with my program. 时间时区都需要正确,以生成正确的时间戳,这是我的程序的问题。

I encountered the same error while working on Linkedin API when you register your application on https://www.linkedin.com/secure/developer make sure when OAuth Keys are generated you click on revoke this will remove OAuth User Token and User secret and once call is made through your application it will dynamically create new OAuth tokens. 当您在https://www.linkedin.com/secure/developer上注册应用程序时,在使用Linkedin API时遇到相同的错误, 确保在生成OAuth密钥时,单击“撤消”,这将删除OAuth用户令牌和用户密码,通过应用程序进行调用后,它将动态创建新的OAuth令牌。 Read the following document to understand more https://developer.linkedin.com/documents/authentication For detailed steps follow http://infinityexist.wordpress.com/2014/08/25/solution-for-400-bad-request-of-access-token-in-linkedin-api/ 阅读以下文档以了解更多信息https://developer.linkedin.com/documents/authentication有关详细步骤,请访问http://infinityexist.wordpress.com/2014/08/25/solution-for-400-bad-request- linkedin api中的访问令牌

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

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