2011-02-11 48 views
5

我想從我的ASP.NET MVC C#應用程序中使用OAuth驗證FreshBooks API。以下是我迄今爲止:通過DotNetOpenAuth驗證FreshBooks

我在這裏使用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(...))我有以下異常:

"The remote server returned an error: (400) Bad Request.".

上午我做正確嗎?基於FreshBooks文檔和應能正常工作的DotNetOpenAuth示例。

是否有一種更簡單的方法來使用OAuth進行身份驗證,因爲DotNetOpenAuth對於簡單地使用OAuth身份驗證有點巨大?

回答

5

一個簡單的事情如果你想使用DotNetOpenAuth你需要確保:

  • 您使用簽名法「明文字母」
  • 和使用PlaintextSigningBindingElement作爲TamperProtect ionElements

這樣的事情對我的作品:

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<string, string> { 
     { "oauth_signature_method", "PLAINTEXT" }, 
    }; 
    Uri callback = Util.GetCallbackUrlFromContext(); 
    var request = consumer.PrepareRequestUserAuthorization(callback, extraParameters, null); 
    consumer.Channel.Send(request); 
} 
1

您可以嘗試使用我的開源OAuth庫。使用和使用起來非常簡單。我有一個連接到Google,Twitter,Yahoo和Vimeo的下載示例項目。我故意保持代碼非常簡單,所以很容易理解。

OAuth C# Library

我沒有用FreshBooks,但它應該是改變的URL,在示例應用程序和過程的供應商之一,建立供應商的特定按鍵等

+0

感謝分享,我承認你的庫使用簡單,雖然我已經得到了相同的400錯誤的請求錯誤,我要開始認爲我與FreshBooks有關而不是我的實施。我會直接與他們覈對。 – 2011-02-12 13:03:06