从 StockX API (OAuth2) 获取访问令牌时遇到问题

Trouble Getting Access Token from StockX API (OAuth2)

提问人:Michael 提问时间:11/7/2023 最后编辑:Brian Tompsett - 汤莱恩Michael 更新时间:11/7/2023 访问量:30

问:

我正在尝试使用 OAuth2 身份验证从 StockX API 检索访问令牌。我遵循了 StockX 在此处提供的文档,并实现了以下 C# 代码:

夏普

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 和客户端密码,但我仍然面临这个问题。

有人可以帮助我确定我在此代码中可能做错了什么,或者为我指出解决此问题的正确方向吗?

文档在这里 : https://developer.stockx.com/portal/authentication

C# 访问令牌

评论


答: 暂无答案