【问题标题】:Authentication to FreshBooks via DotNetOpenAuth通过 DotNetOpenAuth 对 FreshBooks 进行身份验证
【发布时间】:2011-06-25 20:01:33
【问题描述】:

我正在尝试使用 OAuth 从我的 ASP.NET MVC C# 应用程序中对 FreshBooks API 进行身份验证。这是我目前所拥有的:

我正在使用 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(...)) 上,我遇到了以下异常:

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

我这样做正确吗?基于应正常工作的 FreshBooks 文档和 DotNetOpenAuth 示例。

是否有更简单的 OAuth 身份验证方法,因为 DotNetOpenAuth 对于简单地使用 OAuth 身份验证来说有点庞大?

【问题讨论】:

    标签: c# asp.net oauth dotnetopenauth


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      您可以尝试使用我的开源 OAuth 库。它使用起来非常简单。我有一个示例项目,可在下载中连接到 Google、Twitter、Yahoo 和 Vimeo。我特意将代码保持得很简单,以便于理解。

      OAuth C# Library

      我没有使用过 FreshBooks,但更改示例应用程序中的一个提供者的 url 应该很简单,当然还需要设置提供者特定的密钥等。

      【讨论】:

      • 感谢分享,我承认你的图书馆很容易使用,虽然我有同样的 400 Bad request 错误,我会开始认为我与 FreshBooks 相关,而不是我的实现。我会直接与他们核实。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-18
      • 1970-01-01
      • 1970-01-01
      • 2017-04-07
      • 2015-12-08
      • 2016-06-17
      • 1970-01-01
      相关资源
      最近更新 更多