我在 .NET 3.5 解决方案上使用 DotNetOpenAuth 为我们的客户提供社交登录。我成功地实现了 Facebook、Google 和 Twitter,但我被困在了亚马逊。我对 OAuth 2.0 提供商使用相同的代码,但是当亚马逊加载登录屏幕时,我输入了用户名和密码,当我单击登录时,我的代码在调用 ProcessUserAuthorization() 时崩溃,抛出以下错误:DNOA 抱怨:“发送时发生错误直接消息或得到回应。” 和内部异常:“远程服务器返回错误:(400)错误请求。”。这是我的方法:
private static AuthenticationDetailsType LoginOauth2(string[] scope = null, Uri return_uri = null)
{
try
{
// get provider name
string provider_name = ((LoginProviders)providerID).ToString();
var response = new AuthenticationDetailsType();
// get configuration details
var token_endpoint = ConfigurationManager.AppSettings[provider_name + "TokenEndPoint"];
var auth_endpoint = ConfigurationManager.AppSettings[provider_name + "AuthEndPoint"];
var app_key = ConfigurationManager.AppSettings[provider_name + "AppKey"];
var app_secret = ConfigurationManager.AppSettings[provider_name + "AppSecret"];
// instantiate a server
var server = new AuthorizationServerDescription
{
TokenEndpoint = new Uri(token_endpoint),
AuthorizationEndpoint = new Uri(auth_endpoint)
};
// instantiate a client
var client = new WebServerClient(server)
{
ClientIdentifier = app_key,
ClientCredentialApplicator = ClientCredentialApplicator.PostParameter(app_secret)
};
IAuthorizationState auth_state = client.ProcessUserAuthorization();
if (auth_state == null)
{
client.RequestUserAuthorization(scope, return_uri);
}
else
{
response = new AuthenticationDetailsType
{
auth_provider = providerID,
auth_token = auth_state.AccessToken,
auth_token_secret = null,
user_id = null
};
}
return response;
}
catch (Exception ex)
{
ErrorLogger.ErrorLog.WriteError("Error on loging in with provider " + provider_name + " error: "+ ex.StackTrace.ToString());
return null;
}
}
这是我的配置键:
<add key="amazonTokenEndPoint" value="https://api.amazon.com/auth/o2/token"/>
<add key="amazonAuthEndPoint" value="https://www.amazon.com/ap/oa"/>
<add key="amazonAppKey" value="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"/>
<add key="amazonAppSecret" value="xxxxxxxxxxxxxxxxxxxxxxx"/>
<add key="amazonDetailsRequestEndPoint" value="https://api.amazon.com/user/profile"/>
<add key="amazonReturnUri" value="https://localhost/SocialLogin.aspx?providerid=x"/>
这就是我调用此方法的方式:
return LoginOauth2(new string[] { "profile" }, new Uri(System.Configuration.ConfigurationManager.AppSettings["amazonReturnUri"]));
在亚马逊登录屏幕上单击登录后,在第二次调用此方法时会引发异常。
我尝试了 google-ing,但找不到指向我的问题的资源。
感谢帮助 :)