编程语言
首页 > 编程语言> > JavaScript-Google API redirect_uri_mismatch错误

JavaScript-Google API redirect_uri_mismatch错误

作者:互联网

我正在尝试使google plus身份验证适用于Web应用程序,但不断收到redirect_uri_mismatch错误.我已经检查并重新检查代码和google dev设置中是否有匹配的重定向URL.

我已标记出发生故障的位置-> **这里崩溃

任何帮助,将不胜感激.

js绑定

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

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

谷歌组件

<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();
    });
  }
});

解决方法:

该问题的原因是服务器端的redirectUrl也需要设置为postmessage.

标签:node-js,google-api-js-client,javascript
来源: https://codeday.me/bug/20191029/1961146.html