如何在条带付款中的身份验证标头中添加用户名?(How do I add just a username within an authentication header in stripe

编程入门 行业动态 更新时间:2024-10-27 03:37:13
如何在条带付款中的身份验证标头中添加用户名?(How do I add just a username within an authentication header in stripe-payments?)

我正在尝试通过Stripe.js API获得一个简单的帖子请求来创建客户。

https://stripe.com/docs/api/java#authentication

我在vb.net中这样做,不想使用stripe.net库。

我一直得到授权失败。 我必须传递的只是标题中的用户名,或者在这种情况下,用户名是我的测试api密钥。

这是代码的一大块:

Dim asPostRequest As HttpWebRequest = WebRequest.Create(String.Format(ApiEndpoint)) Dim as_ByteArray As Byte() = Encoding.UTF8.GetBytes(stripeccw.ToString) asPostRequest.Method = "POST" asPostRequest.ContentType = "application/json" 'asPostRequest.Headers("Authorization") = "Basic" + apikey 'asPostRequest.Credentials("bearer", apikey) 'asPostRequest.Headers.Add("Authorization") = apikey 'asPostRequest.Credentials("Username") = apikey 'asPostRequest.Credentials = New NetworkCredential(apikey, "") asPostRequest.ContentLength = as_ByteArray.Length Dim as_DataStream As Stream = asPostRequest.GetRequestStream() as_DataStream.Write(as_ByteArray, 0, as_ByteArray.Length) as_DataStream.Close()

我已经注释掉了......那些是我试过的不同方式。 我知道有些只是愚蠢的尝试,但只是感到沮丧。 我知道我的api密钥是正确的。 我可以通过导航到https://api.stripe.com/v1/customers并仅输入我的用户名来验证这一点。

希望有人能发现简单的东西:)

谢谢!

I'm trying to get a simple post request to work to create a customer via the Stripe.js API.

https://stripe.com/docs/api/java#authentication

I'm doing this in vb.net and don't want to use the stripe.net library.

I keep getting authorization failed. All I have to pass is the username in the header, or in this case the username is my test api key.

Here's a chunk of the code:

Dim asPostRequest As HttpWebRequest = WebRequest.Create(String.Format(ApiEndpoint)) Dim as_ByteArray As Byte() = Encoding.UTF8.GetBytes(stripeccw.ToString) asPostRequest.Method = "POST" asPostRequest.ContentType = "application/json" 'asPostRequest.Headers("Authorization") = "Basic" + apikey 'asPostRequest.Credentials("bearer", apikey) 'asPostRequest.Headers.Add("Authorization") = apikey 'asPostRequest.Credentials("Username") = apikey 'asPostRequest.Credentials = New NetworkCredential(apikey, "") asPostRequest.ContentLength = as_ByteArray.Length Dim as_DataStream As Stream = asPostRequest.GetRequestStream() as_DataStream.Write(as_ByteArray, 0, as_ByteArray.Length) as_DataStream.Close()

Where I've commented out... those are different ways that I've tried. I know some are just stupid attempts, but just getting frustrated. I know for a fact my api key is correct. I can verify this by navigating to https://api.stripe.com/v1/customers and entering it in for my username only.

Hoping someone can spot something simple :)

Thank you!

最满意答案

如果我在你的鞋子里,我要做的第一件事就是看看Stripe.Net是如何做到的。 即使您不想自己使用该库,也不意味着您不能将源代码用作参考。

来自Requestor.cs :

internal static WebRequest GetWebRequest(string url, string method, string apiKey = null, bool useBearer = false)
{
    apiKey = apiKey ?? StripeConfiguration.GetApiKey();

    var request = (HttpWebRequest)WebRequest.Create(url);
    request.Method = method;

    if(!useBearer)
        request.Headers.Add("Authorization", GetAuthorizationHeaderValue(apiKey));
    else
        request.Headers.Add("Authorization", GetAuthorizationHeaderValueBearer(apiKey));

    request.Headers.Add("Stripe-Version", StripeConfiguration.ApiVersion);

    request.ContentType = "application/x-www-form-urlencoded";
    request.UserAgent = "Stripe.net (https://github.com/jaymedavis/stripe.net)";

    return request;
}

private static string GetAuthorizationHeaderValue(string apiKey)
{
    var token = Convert.ToBase64String(Encoding.UTF8.GetBytes(string.Format("{0}:", apiKey)));
    return string.Format("Basic {0}", token);
}

private static string GetAuthorizationHeaderValueBearer(string apiKey)
{
    return string.Format("Bearer {0}", apiKey);
}
 

所以似乎有两种方法可以做到这一点。 您可以使用“Bearer”格式,即:

asPostRequest.Headers.Add("Authorization", "Bearer " & apiKey)

或者您可以使用“基本”格式,即:

asPostRequest.Headers.Add("Authorization", _ "Basic " & Convert.ToBase64String(Encoding.UTF8.GetBytes(apiKey & ":")))

If I were in your shoes, the first thing I'd do is take a look at how Stripe.Net does it. Even if you don't want to use that library yourself, that doesn't mean you can't use the source code as a reference.

From Requestor.cs:

internal static WebRequest GetWebRequest(string url, string method, string apiKey = null, bool useBearer = false)
{
    apiKey = apiKey ?? StripeConfiguration.GetApiKey();

    var request = (HttpWebRequest)WebRequest.Create(url);
    request.Method = method;

    if(!useBearer)
        request.Headers.Add("Authorization", GetAuthorizationHeaderValue(apiKey));
    else
        request.Headers.Add("Authorization", GetAuthorizationHeaderValueBearer(apiKey));

    request.Headers.Add("Stripe-Version", StripeConfiguration.ApiVersion);

    request.ContentType = "application/x-www-form-urlencoded";
    request.UserAgent = "Stripe.net (https://github.com/jaymedavis/stripe.net)";

    return request;
}

private static string GetAuthorizationHeaderValue(string apiKey)
{
    var token = Convert.ToBase64String(Encoding.UTF8.GetBytes(string.Format("{0}:", apiKey)));
    return string.Format("Basic {0}", token);
}

private static string GetAuthorizationHeaderValueBearer(string apiKey)
{
    return string.Format("Bearer {0}", apiKey);
}
 

So it seems there are two ways to do it. You can either use "Bearer" format, which is:

asPostRequest.Headers.Add("Authorization", "Bearer " & apiKey)

or you can use "Basic" format which is:

asPostRequest.Headers.Add("Authorization", _ "Basic " & Convert.ToBase64String(Encoding.UTF8.GetBytes(apiKey & ":")))

更多推荐

本文发布于:2023-07-31 07:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1341909.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:条带   身份验证   用户名   如何在   authentication

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!