【发布时间】:2019-12-06 03:36:16
【问题描述】:
我无法使用 OAuth2 授权代码授予获取 Discord API 的访问令牌。通过从我的网络应用发送以下请求,我能够成功获取令牌:
const params = `client_id=${clientId}&client_secret=${clientSecret}&grant_type=authorization_code&code=${code}&redirect_uri=${redirect_uri}&response_type=code&scope=identify`;
fetch(`${apiUrl}/oauth2/token`, {
method: 'POST',
body: params,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
}).then(response => {
console.log(response);
return response.json();
}).then(json => {
console.log(json);
}).catch(e => {
console.log(`ERROR: ${e}`);
});
为了将我的用户注册到我的 Firebase 应用程序,我有点按照this question 的答案中概述的步骤通过云功能请求访问令牌,下一步是创建一个自定义令牌,然后使用signInWithCustomToken 使用我的应用对用户进行身份验证。
但是,当我在云函数中运行完全相同的 sn-p 代码时,我从 Discord 的 OAuth2 服务器得到的响应是这样的:
{
error: 'invalid_request',
error_description: 'Invalid "code" in request.'
}
我不确定这意味着什么。除非我遗漏了什么,否则我在两个请求中传递的code 参数是相同的,我通过记录和比较两者来确认这一点。我认为这可能与函数的运行位置(某处的某个 Google 服务器)有关,但我认为这会很好,因为我的 client_id 和 client_secret 相同?
【问题讨论】:
标签: firebase oauth-2.0 google-cloud-functions discord