我正在尝试通过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 & ":")))
更多推荐
发布评论