我的后端应用程序上有一个路由,它应该为从前端发送的代码返回一个访问 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'
}
}
我也在我的控制台中设置了它:
但是,我在调用该路由时总是出现以下错误:
"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/