开发者

C# Asp.net Linkedin authorization using Linkedintoolkit with DotNetOpenAuth

开发者 https://www.devze.com 2023-03-25 10:34 出处:网络
I am trying to authorize my ASP.net application to use the Linkdin APIs but can\'t seem to authenticate properly.I was hoping someone could take a look at my source code and see whatI might be doing w

I am trying to authorize my ASP.net application to use the Linkdin APIs but can't seem to authenticate properly. I was hoping someone could take a look at my source code and see what I might be doing wrong.

First off, I'm trying to follow the steps in this Linkedintoolkit Documentation and although I'm following the steps I'm still having no l开发者_JAVA百科uck.

The process as I understand it, in its most simple terms is as follows.

  1. Create an implementation of DotNetOpenAuth's IConsumerTokenManager
  2. Using the WebOAuthAuthorization class to Authorize my app and return an access token for future API calls.

My IConsumerTokenManager Implementation is as follows

I've added a fake api key and secret key for obvious reasons in this example

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

namespace UserLibrary.SocialMedia
{
    public class ConsumerTokenManager : IConsumerTokenManager
    {


        private const string API_KEY = "ylk7j2jq4j7l";
        private const string SECRET_KEY = "WH3dlPFhPyWG0xlw";

        private Dictionary<string, string> tokensAndSecrets = new Dictionary<string, string>();

        public ConsumerTokenManager()
        {
            ConsumerKey = API_KEY;
            ConsumerSecret = SECRET_KEY;
        }


        #region ITokenManager Members

        public string ConsumerKey { get; private set; }

        public string ConsumerSecret { get; private set; }

        public string GetTokenSecret(string token)
        {
            return this.tokensAndSecrets[token];
        }

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

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

        /// <summary>
        /// Classifies a token as a request token or an access token.
        /// </summary>
        /// <param name="token">The token to classify.</param>
        /// <returns>Request or Access token, or invalid if the token is not recognized.</returns>
        public TokenType GetTokenType(string token)
        {
            throw new NotImplementedException();
        }

        #endregion

    }
}

And Here is the Code Block that calls BeginAuthorization

ConsumerTokenManager consumerTokenManager = new ConsumerTokenManager();
            _webOAuthorization = new WebOAuthAuthorization(consumerTokenManager, _accessToken);

        _webOAuthorization.BeginAuthorize();

        _accessToken = _webOAuthorization.CompleteAuthorize();

When 'BeginAuthorization()' is called I am redirected to the "Grant K2PS Publisher access to your LinkedIn Account "Patrick A" Only allow access if you trust this application with your LinkedIn network information" Linkedin page and click 'Ok I'll allow it' which returns me to my web app. However the '_completeAuthorization()' method is never hit and its value is null if I try to return the string.

Also of note, my console output Throws a Threading exception during the process.

Below is a copy of my console out

A first chance exception of type 'System.Threading.ThreadAbortException' occurred in mscorlib.dll An exception of type 'System.Threading.ThreadAbortException' occurred in mscorlib.dll but was not handled in user code

I'm fairly new to Oath but have been pouring over the API and can't seem to solve this one. Any help would be greatly appreciated.


you can try this sample I found from here I implemented successfully earlier..

0

精彩评论

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

关注公众号