2011-02-11 32 views
5

ASP.NET MVC C# uygulamasından FreshBooks API için kimlik doğrulama için OAuth kullanmaya çalışıyorum. İşte ben bugüne kadar ne var: Burada DotNetOpenAuth kullanıyorumDotNetOpenAuth üzerinden FreshBooks Kimlik Doğrulaması

olan benim denetleyicisi eylem var kod

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 Ben, DotNetOpenAuth örnek ile sağlanan aynı sınıftır Tüketici sırrını FreshBooks'un bana verdiğini ayarladım. Bunu doğru yapıyor

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

Am: consumer.Channel.Send(consumer.PrepareRequestUserAuthorization(...)) günü

Ben şu istisna var? Doğru çalışması gereken FreshBooks belgelerine ve DotNetOpenAuth örneklerine dayanmaktadır.

OAuth kimlik doğrulaması kullanmak için DotNetOpenAuth biraz büyük olduğundan, OAuth ile kimlik doğrulamanın daha basit bir yolu var mı?

cevap

5

vb sağlayıcı kurma belirli tuşlar örnek uygulamada ve tabii sağlayıcılarından biri için url değiştirme basit bir mesele olmalı Eğer DotNetOpenAuth kullanmak isterseniz emin olmak gerekir:

  • kullandığınız imza yöntemi "plaintext"
  • ve TamperProtect olarak PlaintextSigningBindingElement kullanmak ionElements böyle

şey benim için çalışıyor: paylaşım için

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

Açık kaynak OAuth Kitaplığı'nı kullanmayı deneyebilirsiniz. Kullanımı ve gitmesi çok basit. Google, Twitter, Yahoo ve Vimeo'ya bağlanan indirmede kullanılabilen örnek bir projem var. Kasten kodu çok basit tuttum, anlaşılması kolay.

OAuth C# Library

ben Freshbooks kullanılmaz ettik, ama

+0

Teşekkür ben de aynı 400 Geçersiz istek hata var olsa kütüphane, kullanımı basittir itiraf, ben başlayacağım FreshBooks ile ilgili olduğumu düşünmek ve benim uygulamamı değil. Onlarla doğrudan kontrol edeceğim. –