如何正确调用 post 方法来抽动 api?
How do I correctly call a post method to twitch api?
我正在设计一个预计连接到 twitch 登录。当我查看他们关于如何设置它的文档时,它是这样说的:
POST https://api.twitch.tv/kraken/oauth2/token
POST Body (URL-encoded)
client_id=<your client ID>
&client_secret=<your client secret>
&grant_type=authorization_code
&redirect_uri=<your registered redirect URI>
&code=<authorization code received above>
&state=<your unique token generated by your application>
https://dev.twitch.tv/docs/v5/guides/authentication/#implicit-grant-flow
(我正在遵循 "Authorization Code Flow" 指南)。
但是我目前在使其工作时遇到了一些问题。这是我的代码的样子:
static public async Task getTwitchData()
{
var httpClientRequest = new HttpClient();
var postData = new Dictionary<string, object>();
postData.Add("client_id", clientId);
postData.Add("client_secret", secretId);
postData.Add("grant_type", accesstoken);
postData.Add("redirect_uri", redirectURL);
postData.Add("code", accesstoken);
postData.Add("state", mycreatedtoken);
var jsonRequest = JsonConvert.SerializeObject(postData);
HttpContent content = new StringContent(jsonRequest, System.Text.Encoding.UTF8, "application/json");
var result = await httpClientRequest.PostAsync("https://api.twitch.tv/kraken/oauth2/token", content);
try
{
var resultString = await result.Content.ReadAsStringAsync();
var jsonResult = JObject.Parse(resultString);
System.Diagnostics.Debug.WriteLine(jsonResult);
return jsonResult;
}
catch
{
System.Diagnostics.Debug.WriteLine(result);
return null;
}
}
如果我 运行 这个函数它不会到达 "try" 因为它找不到 json。相反,它到达 "catch" 打印出来的地方:
StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
我应该得到的(如果正确的话)是 json 中的响应,如下所示:
{
"access_token": "<user access token>",
"scope": <your previously listed scope(s)>
}
Use the Implicit Grant Flow if your app does not use a server, such as a client-side JavaScript app or mobile app. This approach does not require a server that must make requests to the API.
如果您是通过移动应用发出这些请求,则应使用 Implicit Grant Flow
而不是 Authorization Code Flow
。
授权代码流用于从您的服务器而非移动应用程序访问 Twitch。
我正在设计一个预计连接到 twitch 登录。当我查看他们关于如何设置它的文档时,它是这样说的:
POST https://api.twitch.tv/kraken/oauth2/token
POST Body (URL-encoded)
client_id=<your client ID>
&client_secret=<your client secret>
&grant_type=authorization_code
&redirect_uri=<your registered redirect URI>
&code=<authorization code received above>
&state=<your unique token generated by your application>
https://dev.twitch.tv/docs/v5/guides/authentication/#implicit-grant-flow (我正在遵循 "Authorization Code Flow" 指南)。
但是我目前在使其工作时遇到了一些问题。这是我的代码的样子:
static public async Task getTwitchData()
{
var httpClientRequest = new HttpClient();
var postData = new Dictionary<string, object>();
postData.Add("client_id", clientId);
postData.Add("client_secret", secretId);
postData.Add("grant_type", accesstoken);
postData.Add("redirect_uri", redirectURL);
postData.Add("code", accesstoken);
postData.Add("state", mycreatedtoken);
var jsonRequest = JsonConvert.SerializeObject(postData);
HttpContent content = new StringContent(jsonRequest, System.Text.Encoding.UTF8, "application/json");
var result = await httpClientRequest.PostAsync("https://api.twitch.tv/kraken/oauth2/token", content);
try
{
var resultString = await result.Content.ReadAsStringAsync();
var jsonResult = JObject.Parse(resultString);
System.Diagnostics.Debug.WriteLine(jsonResult);
return jsonResult;
}
catch
{
System.Diagnostics.Debug.WriteLine(result);
return null;
}
}
如果我 运行 这个函数它不会到达 "try" 因为它找不到 json。相反,它到达 "catch" 打印出来的地方:
StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
我应该得到的(如果正确的话)是 json 中的响应,如下所示:
{
"access_token": "<user access token>",
"scope": <your previously listed scope(s)>
}
Use the Implicit Grant Flow if your app does not use a server, such as a client-side JavaScript app or mobile app. This approach does not require a server that must make requests to the API.
如果您是通过移动应用发出这些请求,则应使用 Implicit Grant Flow
而不是 Authorization Code Flow
。
授权代码流用于从您的服务器而非移动应用程序访问 Twitch。