node.js - Google API、Node.js - getToken 之后的redirect_uri_mismatch

标签 node.js google-api google-api-nodejs-client

我的后端应用程序上有一个路由,它应该为从前端发送的代码返回一个访问 token :

router.get('/token', (req, res) => {
  const auth = googleService.getAuth();

  auth.getToken(req.query.code, (error, res2) => {
    const data = { code: 200 }

    if (error) {
      data.code = error.code;
      data.error = error.response.data;
    } else {
      console.log(res2);
    }

    res
      .status(data.code)
      .send(data);
  })
});

我从 googleService.getAuth() 检索 auth:

const { google } = require('googleapis');
const keys = require('../config/keys');

var module = module.exports = {
  getAuth: (token = false) => {
    let auth = new google.auth.OAuth2(
      keys.google.clientID,
      keys.google.clientSecret,
      keys.google.callbackURL
    );

    if (token) {
      auth.credentials = {
        access_token: token,
        refresh_token: null
      };
    }

    return auth;
  },
  youtube: google.youtube('v3')
};

在我的配置文件中,我有callbackURL:

module.exports = {
  google: {
    apiKey: 'XXXXXXXXXXXXXXXX',
    clientID: 'XXXXXXXXXXXXXX',
    clientSecret: 'XXXXXXXXXXXXXXX',
    callbackURL: 'http://localhost:3000/google/redirect'
  }
}

我也在我的控制台中设置了它:

enter image description here

但是,我在调用该路由时总是出现以下错误:

"error": {
  "error": "redirect_uri_mismatch",
  "error_description": "Bad Request"
}

最佳答案

URI 需要从身份验证到 token 进行匹配。因此,您似乎通过 token 端点进行身份验证,并尝试将 token 发送到 google/redirect 路径。您可以解决这个问题。

为此,请验证重定向 URI 是否已在您的 Google 项目控制台中列入白名单。您可以通过 API 访问查看此信息(您可以在其中看到客户端 ID、客户端密码以及重定向 uri 列表)

关于node.js - Google API、Node.js - getToken 之后的redirect_uri_mismatch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55382047/

相关文章:

mysql - 使用 zongji 模块监听 mysql 更改事件不起作用

node.js - 蒙戈斯 : wrong ELF class: ELFCLASS32 nodejs application in openshift

android - com.google.api.services.tasks.TasksRequest 在哪里?

node.js - 从 Google Logging API 解码 protoPayload

javascript - oauth2Client.getToken 缺少 refresh_token

javascript - 如何使用nodejs从项目/特定位置下载文件?

node.js - 我可以使用 ajv json 模式验证日期,而不将日期转换为字符串吗?

javascript - 使用适用于网络浏览器的 Google URL Shortener API

ms-word - 使用Google Script将Google Doc转换为Docx

google-api - Gmail API 突然停止使用 [错误 : unauthorized_client]