NodeJS 发布请求,请求承诺用于 Twitch 身份验证



我正在尝试通过用户的 Twitch 登录名对我的应用程序上的用户进行身份验证。我似乎无法正确request.post()(使用请求-承诺(。我尝试了许多不同的变体,我通常会在服务器日志中收到"未处理的拒绝"。Twitch API指南在这里。POST 响应应为 JSON。这是我的最新版本:

const twitchATParams =
'?client_id=' + twitchAppClientId +
'&client_secret=' + twitchClientSecret +
'&code=' + code +
'&grant_type=authorization_code' +
'&redirect_uri=' + twitchAppRedirect;
request.post(twitchATRequestUrl + twitchATParams)
.then((accessTokenResponse) => {
const accessToken = accessTokenResponse.access_token;
console.log('Got an access token: ' + accessToken);
res.status(200).send('Got an access token: ' + accessToken);
})
.catch((error) => {
res.status(error.statusCode).send(error.error.error_description);
});

我也试过这个:

request.post({
url:     twitchATRequestUrl,
form:    { client_id: twitchAppClientId,
client_secret: twitchClientSecret,
code: code,
grant_type: "authorization_code",
redirect_uri:  twitchAppRedirect}
}, function(error, accessTokenResponse, body){
const accessToken = accessTokenResponse.access_token;
console.log('Got an access token: ' + accessToken);
res.status(200).send('Got an access token: ' + accessToken);
});

这就是Twitch API指南说我需要做的,我想我在将其转换为JavaScript时遇到了麻烦:

POST https://id.twitch.tv/oauth2/token
?client_id=<your client ID>
&client_secret=<your client secret>
&code=<authorization code received above>
&grant_type=authorization_code
&redirect_uri=<your registered redirect URI>

更新: 该应用使用Cloud Functions托管在Firebase上。也许这会影响我的请求?

更新 2: 根据这个:部署的Firebase函数无法执行HTTP GET到外部API?我只能在 Firebase 付费方案中发出外部 API 请求。我假设这是我的问题。我将升级到即用即付计划(实际上免费提供大量数据(,然后重试并在此处发布我的结果。

我不知道你用的是什么库,但最后一个例子与此版本非常相似:https://www.npmjs.com/package/request。

如果是该版本,则问题是您错误地映射了回调的参数。accessTokenResponse var 是响应对象,而不是您想要的 JSON。您必须像这样解析正文参数。

function(error, response, body){
console.log(JSON.parse(body));
}

我认为您使用请求-承诺的方式是错误的。试试这个。

var rp = require('request-promise');

内部 API 函数:

const twitchATParams =
'?client_id=' + twitchAppClientId +
'&client_secret=' + twitchClientSecret +
'&code=' + code +
'&grant_type=authorization_code' +
'&redirect_uri=' + twitchAppRedirect;

var options = {
method: 'POST',
uri: twitchATRequestUrl + twitchATParams,
/* qs: {
access_token: 'xxxxx xxxxx' // -> uri + '?access_token=xxxxx%20xxxxx'
}, */ //you can pass params here too
json: true // Automatically stringifies the body to JSON
};
rp(options)
.then(function (response) {
res.status(200).send('Got an access token: ' + response.accessToken);
})
.catch(function (err) {
// Post failed...
});

已解决。事实证明,我确实需要一个付费计划(Blaze,即用即付(来访问外部 API。我升级了,这基本上解决了问题。它允许我看到一个新的错误代码:StatusCodeError: 400 - "{"status":400,"message":"Parameter redirect_uri does not match registered URI"}所以我发现我的代码中的重定向 URL 缺少"/callback"(Twitch 应用程序管理设置上的 OAuth 重定向 URL 末尾有这个"/callback"(。

我还能够使用这两个代码块成功获取访问令牌:

const twitchTokenPayload = {
client_id: twitchAppClientId,
client_secret: twitchClientSecret,
code: code,
grant_type: 'authorization_code',
redirect_uri: twitchAppRedirect,
};
request.post(twitchATRequestUrl, { json: twitchTokenPayload })
.then((accessTokenResponse) => {
const accessToken = accessTokenResponse.access_token;
console.log('Got an access token: ' + accessToken);
res.status(200).send('Got an access token: ' + accessToken);
})
.catch((error) => {
console.log('Caught error: ' + error.error.error_description);
res.status(error.statusCode).send(error.error.error_description);
});

这也奏效了:

request.post({
url:     twitchATRequestUrl,
form:    { client_id: twitchAppClientId,
client_secret: twitchClientSecret,
code: code,
grant_type: "authorization_code",
redirect_uri:  twitchAppRedirect}
}, function(error, response, body){
console.log(JSON.parse(body));
const jsonStuff = JSON.parse(body);
const accessToken = jsonStuff.access_token;
console.log('Got an access token: ' + accessToken);
res.status(200).send('Got an access token: ' + accessToken);
});

最新更新