【问题标题】:Not returning refresh token from auth code不从身份验证代码返回刷新令牌
【发布时间】:2019-03-14 14:56:42
【问题描述】:

我正在使用来自节点的 googleapis 包从前端传递的身份验证代码中获取刷新令牌和访问令牌,但每次我收到以下错误。

  {      
      error: 'redirect_uri_mismatch',
      error_description: 'Bad Request' 
  }

我知道当我们在控制台中作为回调 URL 传递的 URL 不匹配时会出现此错误。 https://console.cloud.google.com/apis/credentials

但我已经在控制台中设置了正确的 URL。仍然不确定代码有什么问题。

使用/auth 将令牌从front-end 传递给node-server

const {
    google
} = require("googleapis");
const OAuth2 = google.auth.OAuth2;
var bodyParser = require('body-parser')

const express = require('express');
const app = express();
app.use(bodyParser.json());

app.use(function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    next();
});


app.use('/tokenCallback', (req, res) => {
    console.log(req);
    res.send('An alligator approaches!');
});

app.post('/auth', (req, res) => {
    runProcess(req.body.auth);
    res.send('An alligator approaches!');
});

app.listen(4300);

function runProcess(code) {

    const oauth2client = new OAuth2(
        "210347756664-xxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
        "57ZU6JuQ7oQ7SvSFtki5atxx", // Client Secret
        "http://localhost:4300/tokenCallback",
    );

    oauth2client.getToken(code, (e) => {
        console.log(e);
    });

    oauth2client.on('tokens', (tokens) => {
        if (tokens.refresh_token) {
            console.log("refresh token", tokens);
        }
    });

}

任何人的帮助将不胜感激。

【问题讨论】:

标签: node.js oauth google-authentication google-apis-explorer refresh-token


【解决方案1】:

redirect_uri_mismatch 表示您的应用未发送有效的重定向 URI,或者未在 Google Cloud Console 上注册。

确保在 Console -> APIs & Services -> Credentials -> OAuth client IDs -> (your key) 中添加了 http://localhost:4300/tokenCallback URI。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-08
    • 1970-01-01
    • 2020-07-07
    • 2019-11-17
    • 2015-10-01
    • 2019-02-27
    • 2018-05-27
    • 1970-01-01
    相关资源
    最近更新 更多