通过DotNetOpenAuth对FreshBooks进行身份validation

我正在尝试使用OAuth从我的ASP.NET MVC C#应用程序中对FreshBooks API进行身份validation。 这是我到目前为止:

我在这里使用DotNetOpenAuth是我在控制器操作中的代码

if (TokenManager != null) { ServiceProviderDescription provider = new ServiceProviderDescription(); provider.ProtocolVersion = ProtocolVersion.V10a; provider.AccessTokenEndpoint = new MessageReceivingEndpoint ("https://myfbid.freshbooks.com/oauth/oauth_access.php", DotNetOpenAuth.Messaging.HttpDeliveryMethods.PostRequest); provider.RequestTokenEndpoint = new DotNetOpenAuth.Messaging.MessageReceivingEndpoint("https://myfbid.freshbooks.com/oauth/oauth_request.php", DotNetOpenAuth.Messaging.HttpDeliveryMethods.PostRequest); provider.UserAuthorizationEndpoint = new DotNetOpenAuth.Messaging.MessageReceivingEndpoint("https://myfbid.freshbooks.com/oauth/oauth_authorize.php", DotNetOpenAuth.Messaging.HttpDeliveryMethods.GetRequest); provider.TamperProtectionElements = new ITamperProtectionChannelBindingElement[] { new HmacSha1SigningBindingElement() }; var consumer = new WebConsumer(provider, TokenManager); var response = consumer.ProcessUserAuthorization(); if (response != null) { this.AccessToken = response.AccessToken; } else { // we need to request authorization consumer.Channel.Send(consumer.PrepareRequestUserAuthorization( new Uri("http://localhost:9876/home/testoauth/"), null, null)); } } 

TokenManager与DotNetOpenAuth示例提供的是同一个类,我设置了FreshBooks给我的消费者秘密。

consumer.Channel.Send(consumer.PrepareRequestUserAuthorization(...))我有以下exception:

“远程服务器返回错误:(400)错误请求。”

我这样做了吗? 基于FreshBooks文档和DotNetOpenAuth样本应该正常工作。

有没有更简单的方法来validationOAuth,因为DotNetOpenAuth对于简单地使用OAuth身份validation来说有点大?

如果你想使用DotNetOpenAuth,你需要确保:

  • 你使用签名方法“PLAINTEXT”
  • 并使用PlaintextSigningBindingElement作为TamperProtectionElements

这样的事情对我有用:

 public static readonly ServiceProviderDescription ServiceDescription = new ServiceProviderDescription { ProtocolVersion = ProtocolVersion.V10a, RequestTokenEndpoint = new MessageReceivingEndpoint(oAuthBase + "/oauth_request.php", HttpDeliveryMethods.PostRequest), UserAuthorizationEndpoint = new MessageReceivingEndpoint(oAuthBase + "/oauth_authorize.php", HttpDeliveryMethods.GetRequest | HttpDeliveryMethods.AuthorizationHeaderRequest), AccessTokenEndpoint = new MessageReceivingEndpoint(oAuthBase + "/oauth_access.php", HttpDeliveryMethods.PostRequest | HttpDeliveryMethods.AuthorizationHeaderRequest), TamperProtectionElements = new ITamperProtectionChannelBindingElement[] { new PlaintextSigningBindingElement() } }; public static void RequestAuthorization(WebConsumer consumer) { if (consumer == null) { throw new ArgumentNullException("consumer"); } var extraParameters = new Dictionary { { "oauth_signature_method", "PLAINTEXT" }, }; Uri callback = Util.GetCallbackUrlFromContext(); var request = consumer.PrepareRequestUserAuthorization(callback, extraParameters, null); consumer.Channel.Send(request); } 

您可以尝试使用我的开源OAuth库。 它非常简单易用。 我有一个示例项目可以在下载中连接到谷歌,Twitter,雅虎和Vimeo。 我故意保持代码非常简单,因此很容易理解。

OAuth C#Library

我没有使用过FreshBooks,但它应该是一个简单的问题,即更改示例应用程序中某个提供程序的url,当然还要设置提供程序特定的键等。