Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript google api重定向\u uri\u不匹配错误_Javascript_Node.js_Google Api Js Client - Fatal编程技术网

Javascript google api重定向\u uri\u不匹配错误

Javascript google api重定向\u uri\u不匹配错误,javascript,node.js,google-api-js-client,Javascript,Node.js,Google Api Js Client,我试图让google plus认证为一个web应用程序工作,但不断地出现重定向错误。我已经检查并重新检查了代码和google dev设置中是否有匹配的重定向URL 我已标记出发生故障的位置->**此处 任何帮助都将不胜感激 js绑定 window.gplusSigninCallback = function(authResult) { if (!authResult.code) { return; } request .post('/api/users')

我试图让google plus认证为一个web应用程序工作,但不断地出现重定向错误。我已经检查并重新检查了代码和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>

问题的原因是服务器端的重定向URL也需要设置为postmessage。

这些似乎是一些很好的解决方案:有趣的是,如果您使用JSAPI获取临时令牌(不是登录按钮,只是gapi.auth.sign),那么设置客户端重定向uri是完全隐藏的。所以我花了大约4个小时试图弄明白为什么我一直得到重定向uri不匹配错误。非常感谢你。
'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();
    });
  }
});