简体   繁体   中英

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. Just a simple program to try to learn about 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.

I've tried using both DesktopConsumers and WebConsumers, both of which fail in the same way. 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.

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.

Here's the code for 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

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. 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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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