【发布时间】:2018-06-15 23:52:25
【问题描述】:
我有一个使用 react-google-login 的简单客户端应用程序:
<GoogleLogin
clientId={config.CLIENT_ID}
scope={config.SCOPES.join(' ')}
buttonText="Login With Google"
onSuccess={response => onSignIn('google', response)}
onFailure={this.failure.bind(this)}
accessType="offline"
responseType="code"
/>
它成功检索代码并将其发送到用NodeJS编写的后端服务器。
服务器端代码如下所示:
const { google } = require('googleapis');
const config = global.config;
const oauth2Client = new google.auth.OAuth2({
clientId: config.google.CLIENT_ID,
clientSecret: config.google.CLIENT_SECRET,
});
// code omitted for the sake of simplicity
var authCode = req.body.authCode; // the provided code by google
const { tokens } = await oauth2Client.getToken(authCode);
return tokens;
当我运行代码时,它会抛出错误:
{ error: 'invalid_request',
error_description: 'Missing parameter: redirect_uri' } },
code: '400' }
如果我将redirectUrl 添加到开发者控制台、客户端应用程序和服务器端应用程序,我将收到redirect_uri_mismatch 错误。
我有点卡在这里,在网上找不到任何有用的东西。
任何解决方法将不胜感激。
【问题讨论】:
-
您必须在您的 Google 应用中设置相同的 redirect_uri 服务器端。
-
我做到了。我收到了
redirect_uri_mismatch错误。 -
您收到不匹配错误,因为您在应用中保存的 URL 与您在请求中发送的 URL 不同。 URL 必须完全相同。
-
谢谢@marekful,但我找到了解决方案并在下面分享。问题根本不是网址。我不得不使用
postmessage而不是 url
标签: node.js google-oauth google-api-nodejs-client