我想授權我的ASP.net應用程序使用Linkdin API,但似乎無法正確認證。我希望有人能看看我的源代碼,看看我可能做錯了什麼。C#Asp.net Linkedin授權與DotNetOpenAuth使用Linkedintoolkit
首先,我試圖按照Linkedintoolkit Documentation中的步驟操作,儘管我遵循了這些步驟,但仍然沒有運氣。
我瞭解它的過程,最簡單的說法如下。
- 創建的DotNetOpenAuth的IConsumerTokenManager
- 實現使用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
ConsumerTokenManager consumerTokenManager = new ConsumerTokenManager();
_webOAuthorization = new WebOAuthAuthorization(consumerTokenManager, _accessToken);
_webOAuthorization.BeginAuthorize();
_accessToken = _webOAuthorization.CompleteAuthorize();
當「BeginAuthorization()」叫我重定向到「授予K2PS出版商訪問您的LinkedIn帳戶‘帕特里克’ 只允許訪問,如果你與你的LinkedIn網絡信息信任該應用」 LinkedIn頁面,然後點擊「確定我會允許它返回到我的web應用程序。然而,'_completeAuthorization()'方法從來沒有命中,如果我嘗試返回字符串,它的值爲空。
另外值得注意的是,我的控制檯輸出過程中引發了線程異常。
下面是類型「System.Threading.ThreadAbortException」的第一次機會異常出現在mscorlib.dll 型「System.Threading.ThreadAbortException」的一個例外,我的控制檯的副本出來
發生在mscorlib.dll,但沒有在用戶代碼中處理
我對Oath相當陌生,但一直在傾吐API,似乎無法解決這個問題。任何幫助將不勝感激。