使用 Linkedintoolkit 和 DotNetOpenAuth 进行 C# Asp.net Linkedin 授权

发布于 2024-11-27 13:28:03 字数 3317 浏览 4 评论 0原文

我正在尝试授权我的 ASP.net 应用程序使用 Linkdin API,但似乎无法正确进行身份验证。我希望有人可以看一下我的源代码,看看我可能做错了什么。

首先,我尝试按照此 Linkedintoolkit 文档 中的步骤操作尽管我正在遵循这些步骤,但我仍然没有运气。

据我了解,最简单的流程如下。

  1. 创建 DotNetOpenAuth 的 IConsumerTokenManager 的实现
  2. ,使用 WebOAuthAuthorization 类来授权我的应用程序并返回访问令牌以供将来的 API 调用。

我的 IConsumerTokenManager 实现如下

出于明显的原因,在本示例中我添加了一个假的 api 密钥和秘密密钥

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

    }
}

这是调用 BeginAuthorization 的代码块< /strong>

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

        _webOAuthorization.BeginAuthorize();

        _accessToken = _webOAuthorization.CompleteAuthorize();

当调用“BeginAuthorization()”时,我会被重定向到“授予 K2PS 发布者访问您的 LinkedIn 帐户“Patrick A”的权限 仅当您通过 LinkedIn 网络信息信任此应用程序时才允许访问”Linkedin 页面并单击“确定,我将允许”,这将返回到我的 Web 应用程序。但是,“_completeAuthorization()”方法永远不会被命中,其值为 null如果我尝试返回字符串。

另外值得注意的是,我的控制台输出在此过程中抛出线程异常

下面是我的控制台输出的副本

类型的第一次机会异常。 mscorlib.dll 中发生“System.Threading.ThreadAbortException” mscorlib.dll 中发生了“System.Threading.ThreadAbortException”类型的异常,但未在用户代码中处理

我对 Oath 相当陌生,但一直在研究 API,似乎无法解决此问题。任何帮助将不胜感激。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

澉约 2024-12-04 13:28:03

你可以尝试这个示例我从这里 我之前成功实现了..

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文