我正在尝试使用 OAuth2 身份验证从 StockX API 检索访问令牌。我遵循 StockX 提供的文档并实现了以下 C# 代码:
csharp
private async void simpleButton16_ClickAsync(object sender, EventArgs e)
{
var clientId = "6yZdVNq48X_the rest of my token";
var clientSecret = "JVqpAdmJP0a3Uh_aUwPb_ the rest of my token";
// StockX token endpoint
string tokenEndpoint = "https://gateway.stockx.com/api/v1/oauth/token";
try
{
using (HttpClient httpClient = new HttpClient())
{
// Create a request body with client credentials
var requestContent = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("grant_type", "client_credentials"),
new KeyValuePair<string, string>("client_id", clientId),
new KeyValuePair<string, string>("client_secret", clientSecret)
});
// Send a POST request to the token endpoint
var response = await httpClient.PostAsync(tokenEndpoint, requestContent);
if (response.IsSuccessStatusCode)
{
// Parse and extract the access token from the response
var responseContent = await response.Content.ReadAsStringAsync();
// You may want to use a JSON parser to extract the access token from the response content.
Console.WriteLine("Access Token: " + responseContent);
}
else
{
Console.WriteLine("Failed to obtain access token. Status code: " + response.StatusCode);
}
}
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
但是,我不断收到这样的回复:
“无法获取访问令牌。状态代码:禁止”。
我已经仔细检查了我的客户端 ID 和客户端密码,但我仍然面临这个问题。
有人可以帮助我确定我在这段代码中可能做错了什么,或者为我指出解决此问题的正确方向吗?
抱歉,我没有您的问题的答案,但我有兴趣知道您是如何获得 stockx api 访问密钥的。我可以通过电子邮件与您联系吗?谢谢