【问题标题】:OAuth2 access token exchange failing when called from Firebase cloud function从 Firebase 云函数调用时,OAuth2 访问令牌交换失败
【发布时间】: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_idclient_secret 相同?

【问题讨论】:

    标签: firebase oauth-2.0 google-cloud-functions discord


    【解决方案1】:

    知道了。

    原来我发送了两次访问令牌交换请求(一次来自客户端,一次来自云函数),这导致云函数总是最后发送请求的速度稍慢,并且由于第二个请求重用了第一次请求的授权码,被认为是无效的。

    【讨论】:

      猜你喜欢
      • 2021-09-08
      • 1970-01-01
      • 2021-09-17
      • 2016-11-12
      • 2011-07-28
      • 2011-01-19
      • 2018-05-06
      • 1970-01-01
      相关资源
      最近更新 更多