【发布时间】:2019-07-02 04:44:25
【问题描述】:
我正在开发 C# WinForms 应用程序以在 Stex.com 上进行交易。 他们将 api 升级到 api3。 它使用谷歌身份验证应用程序登录。 这就是为什么没有人的行为就无法获得访问令牌的原因。 最后决定用postman来获取access token,并且想在token过期的时候刷新token。
我认为这是最好的方法。 所以我通过邮递员获得了访问令牌和刷新令牌。 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(Bad Request) 错误。 如果有人这样做,请帮助我。
【问题讨论】:
标签: c#