gpt4 book ai didi

javascript - google api redirect_uri_mismatch 错误

转载 作者:搜寻专家 更新时间:2023-10-31 23:42:59 26 4
gpt4 key购买 nike

我正在尝试让 google plus 身份验证适用于网络应用程序,但不断出现 redirect_uri_mismatch 错误。我检查并重新检查了我在代码和谷歌开发设置中是否有匹配的重定向 url。

我已经标记了发生故障的位置 -> ** CRASHES HERE

如有任何帮助,我们将不胜感激。

js绑定(bind)

window.gplusSigninCallback = function(authResult) {
if (!authResult.code) {
return;
}

request
.post('/api/users')
.send({ code: authResult.code })
.end(function(err, res) {
dom('#signinButton').remove();
});
};

google+ 组件

<span id="signinButton">
<span class="g-signin"
data-scope="https://www.googleapis.com/auth/plus.login"
data-clientid="{clientId}"
data-redirecturi="postmessage"
data-accesstype="offline"
data-cookiepolicy="single_host_origin"
data-callback="gplusSigninCallback">
</span>
</span>

POST/用户

使用的代码来自 https://github.com/google/google-api-nodejs-client/blob/master/examples/oauth2.js

'use strict';

const router = require('express').Router();
const User = require('../models/user');

const GoogleCreds = require('../config/google_api_credentials').web;
const googleapis = require('googleapis');
const OAuth2Client = googleapis.OAuth2Client;

module.exports = router;

/**
*
*/
router.post('/', function(req, res) {
/* jshint camelcase:false */

const clientId = GoogleCreds.client_id;
const clientSecret = GoogleCreds.client_secret;
const redirectUrl = 'http://localhost:3000';
const oneTimeCode = req.body.code;

try {
authenticate();
} catch (ex) {
console.log(ex.message);
}

function authenticate() {
googleapis
.discover('plus', 'v1')
.execute(function(err, client) {
let oauth2Client = new OAuth2Client(clientId, clientSecret, redirectUrl);

getAccessToken(oauth2Client, function() {
getUserProfile(client, oauth2Client, 'me', function(err, profile) {
if (err) {
throw new Error(err);
}

let userAttrs = {
foo: 'bar'
};

User.findOrCreate(userAttrs, function(err, user) {
res.set('X-API-TOKEN', user.apiToken);
res.send(user);
});
});
});
});
}

function getUserProfile(client, authClient, userId, callback) {
client
.plus.people.get({ userId: userId })
.withAuthClient(authClient)
.execute(callback);
}

function getAccessToken(oauth2Client, callback) {
oauth2Client.generateAuthUrl({
access_type: 'offline', // will return a refresh token
scope: 'https://www.googleapis.com/auth/plus.login'
});

oauth2Client.getToken(oneTimeCode, function(err, tokens) {
if (err) {
// ** CRASHES HERE
throw new Error(err);
}
oauth2Client.setCredentials(tokens);
callback();
});
}
});

最佳答案

问题的原因是server端的redirectUrl也需要设置为postmessage

关于javascript - google api redirect_uri_mismatch 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23451873/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com