我想获取Stex.com API访问令牌

时间:2019-07-01 19:42:08

标签: c#

我正在开发C#WinForms应用程序以在Stex.com上进行交易。 他们将api升级为api3。 它使用谷歌身份验证应用程序登录。 这就是为什么没有人的行为就无法获得访问令牌的原因。 最终,我决定使用邮递员来获取访问令牌,并且我希望在令牌过期时刷新令牌。

我认为这是最好的方法。 因此,我通过邮递员获得了访问令牌和刷新令牌。     https://help.stex.com/en/articles/2740368-how-to-connect-to-the-stex-api-v3-using-postman

现在轮到刷新我的令牌了。 这就是我写的。

string refresh_token = "def50200b03974080...";
string client_id = "502";
string client_secret = "SeTs50aFxV1RoMFBW1b4RVNQhh2wEdICaYQrpE3s";
string AccessToken = "eyJ0eXAiOiJKV1QiLCJhbGciO...";
string url = @"https://api3.stex.com/oauth/token";
var request = HttpWebRequest.Create(url);
            request.Method = "POST";
            request.Headers.Add("Authorization", "Bearer " + AccessToken);
            request.ContentType = "application/x-www-form-urlencoded";
 NameValueCollection outgoingQueryString = HttpUtility.ParseQueryString(String.Empty);
            outgoingQueryString.Add("grant_type", "refresh_token");
            outgoingQueryString.Add("refresh_token", refresh_token);
            outgoingQueryString.Add("client_id", client_id);
            outgoingQueryString.Add("client_secret", client_secret);
            outgoingQueryString.Add("scope", "trade profile reports");
            outgoingQueryString.Add("redirect_uri", @"https://www.getpostman.com/oauth2/callback");

byte[] postBytes = new ASCIIEncoding().GetBytes(outgoingQueryString.ToString());

Stream postStream = request.GetRequestStream();
            postStream.Write(postBytes, 0, postBytes.Length);
            postStream.Flush();
            postStream.Close();

using (WebResponse response = request.GetResponse())
{
    using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
    {
        dynamic jsonResponseText = streamReader.ReadToEnd();
     }
}

它显示401(未经授权)错误。 当我删除ContentType时,它显示400(错误请求)错误。 如果有人这样做,请帮助我。

1 个答案:

答案 0 :(得分:2)

伙计们! 最后,我找到了问题。 这个问题是由于我的无知。 遇到问题时冷静一下,放松一下。 :) 我创建了2个api3客户端,因此client_secret是不同的。 谢谢。