我正在尝试使用 DotNetOpenAuth 编写一个基本程序来代表用户搜索人员。只是一个尝试了解 OAuth 的简单程序。现在我一直在尝试授权,但是当我尝试这样做时,我从linkedIn 获得了 HTTP 400 状态代码。
我试过同时使用 DesktopConsumers 和 WebConsumers,两者都以同样的方式失败。我知道导致异常的特定函数是第 34 行对 RequestUserAuthorization 的调用,我尝试使用空字典和空值作为它的输入,但什么也没发生。
我在程序中使用了两个类,一个包含 main 的 TestLinkedIn.cs 类和一个 LinkedInTokenManager.cs 类,这是 IConsumerTokenManager 的一个非常简单的实现。
这是 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;
}
}
}
这是代码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;
}
}
}
这是我发布到服务器的东西。
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
这是错误消息中的堆栈跟踪。
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
有什么想法吗?