【发布时间】:2017-04-22 22:13:52
【问题描述】:
我正在处理一个预计连接到 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 (我遵循“授权代码流程”指南)。
但是我目前在使它工作时遇到了一些问题。这就是我的代码的样子:
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)>
}
【问题讨论】:
-
您是否通过 https 发送请求?
-
是的,我使用“Microsoft.Net.Http”框架。在代码的开头你可以看到我是如何使用它的:
var httpClientRequest = new HttpClient(); -
您是否在调试模式下启用了 ssl? httpclient 类将使用您设置应用程序使用的任何协议。我问是因为大多数 oauth2 实现会强制您使用 https,尽管他们应该在返回的错误消息中告诉您这一点,而不是给您 400...
-
我正在为一个应用程序编写此代码,它也来自我使用此代码的应用程序。我以前没听说过ssl,我去google一下看看
-
我看到了一篇关于您提到的关于 ssl 的确切帖子:forums.xamarin.com/discussion/34678/… 似乎可能存在一些问题。我现在正在“模拟器”而不是真实设备上进行测试,所以这可能是问题所在。