使用 Hammock 通过 BasicAuthCredentials 调用循环 REST API

发布于 2024-12-22 22:31:08 字数 2079 浏览 3 评论 0原文

我正在尝试使用 .NET 的 Hammock C# 库调用 Recurly REST API。 API 调用需要 HttpRequest 上的授权标头,并且身份验证方案是基本身份验证,并在标头的用户名部分中使用 Base64 编码的 API 密钥。我认为我可以使用该对象的 Username 属性中的编码密钥创建一个新的 Hammock BasicAuthCredentials 对象,然后将该对象分配给 RestClient 或 RestRequest 对象的 Credentials 属性。但是,这似乎不会在出站 HttpRequest 上生成授权标头。

如果我使用这些对象之一的 AddHeader 方法手动添加授权标头,则 API 调用会成功。如果我将 Credentials 属性与 BasicAuthCredentials 对象一起使用,我会从 Recurly 收到“拒绝访问”错误。

这看起来很基本,所以我知道我做错了什么。那么,在 Hammock 中,RestClient 或 RestRequest 对象上的 Credentials 属性是否应该在 Http 请求上创建 Authorization 标头?

感谢超级 Hammock 用户的帮助!

失败的代码:

class Program
{
    public static void Main(string[] args)
    {

        string encodedAPIKey = Convert.ToBase64String(Encoding.UTF8.GetBytes("APIKeyHere"));
        BasicAuthCredentials credentials = new BasicAuthCredentials
        {
            Username = encodedAPIKey
        };

        RestClient client = new RestClient
        {
            Authority = "https://api.recurly.com",
            VersionPath = "v2"
        };

        client.AddHeader("Accept", "application/xml");

        RestRequest request = new RestRequest
        {
            Credentials = credentials,
            Path = "plans"

        };

        RestResponse response = client.Request(request);

        Console.WriteLine(response.Content);
        Console.ReadLine();


    }
}

成功的代码:

class Program
{
    public static void Main(string[] args)
    {

        string encodedAPIKey = Convert.ToBase64String(Encoding.UTF8.GetBytes("APIKeyHere"));

        RestClient client = new RestClient
        {
            Authority = "https://api.recurly.com",
            VersionPath = "v2"
        };

        client.AddHeader("Accept", "application/xml");
        client.AddHeader("Authorization", "Basic " + encodedAPIKey);

        RestRequest request = new RestRequest
        {
            Path = "plans"

        };

        RestResponse response = client.Request(request);

        Console.WriteLine(response.Content);
        Console.ReadLine();


    }
}

I'm trying to call the Recurly REST API using the Hammock C# library for .NET. The API calls require an Authorization header on the HttpRequest, and the authentication scheme is Basic authentication with the Base64 encoded API key in the username portion of the header. I thought that I could create a new Hammock BasicAuthCredentials object with the encoded key in the Username property of the object, then assign the object to the Credentials property of either the RestClient or RestRequest objects. However, this does not seem to generate an Authorization header on the outbound HttpRequest.

If I add the Authorization header manually using the AddHeader method on one of those objects, the API call succeeds. If I use the Credentials property with the BasicAuthCredentials object, I get an Access Denied error from Recurly.

This seems pretty basic, so I know I'm doing something wrong. So, in Hammock, is the Credentials property on either the RestClient or RestRequest object supposed to create an Authorization header on the Http request?

Thanks for any help from a super Hammock user!

The code that fails:

class Program
{
    public static void Main(string[] args)
    {

        string encodedAPIKey = Convert.ToBase64String(Encoding.UTF8.GetBytes("APIKeyHere"));
        BasicAuthCredentials credentials = new BasicAuthCredentials
        {
            Username = encodedAPIKey
        };

        RestClient client = new RestClient
        {
            Authority = "https://api.recurly.com",
            VersionPath = "v2"
        };

        client.AddHeader("Accept", "application/xml");

        RestRequest request = new RestRequest
        {
            Credentials = credentials,
            Path = "plans"

        };

        RestResponse response = client.Request(request);

        Console.WriteLine(response.Content);
        Console.ReadLine();


    }
}

The code that succeeds:

class Program
{
    public static void Main(string[] args)
    {

        string encodedAPIKey = Convert.ToBase64String(Encoding.UTF8.GetBytes("APIKeyHere"));

        RestClient client = new RestClient
        {
            Authority = "https://api.recurly.com",
            VersionPath = "v2"
        };

        client.AddHeader("Accept", "application/xml");
        client.AddHeader("Authorization", "Basic " + encodedAPIKey);

        RestRequest request = new RestRequest
        {
            Path = "plans"

        };

        RestResponse response = client.Request(request);

        Console.WriteLine(response.Content);
        Console.ReadLine();


    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

囍笑 2024-12-29 22:31:08

在我的问题没有得到答案后,我搜索了 .NET 的替代 Rest 库,并找到了 RestSharp。我在第一次尝试时就能够使用 Recurly 内置的基本授权实现来使其与 Recurly 一起工作,因此我将使用 RestSharp 来实现。代码看起来非常相似,因此迁移应该很容易。

After getting no answers to my question, I did a search for alternative Rest libraries for .NET and found RestSharp. I was able to get it working with Recurly using its built-in Basic Authorization implementation on the first try, so I will be implementing using RestSharp. The code looks very similar, so the migration should be an easy one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文