javascript - 来自 Node.js 的 Javascript 中的 getToken 的 invalid_request

标签 javascript node.js

我在 node.js 服务器应用程序中有以下代码

app.get('/AuthorizeGoogle.html',function(req,res) {
    var auth        = new googleapis.OAuth2Client(config.google_login.client_id, config.google_login.client_secret, config.google_login.redirect_uri);
    var queryData   = url.parse(req.url,true).query;
    var code        = encodeURIComponent(queryData.code);

    console.log('Authorization Request recieved ');;

    console.log('Retrieiving token');
    auth.getToken(code,function(err,tokens) {
        console.log('Retrievied token ');
        console.log(tokens);
        console.log('**** ERROR ****');
        console.log(err);

        console.log('Calling setCredentials');
        auth.setCredentials(tokens);
        console.log('*****FINISHED!!!!!');
    });

    res.send('Authorization recieved ' + queryData.code)
});

当 Google 返回并且用户已授权访问其 Google 帐户时调用此方法。

我得到一个代码。但是,当 auth.getToken() 我收到 invalid_request 时。我已经在 C# 中成功完成了这项工作,但我现在正在转向更多开源工具,因此将项目转移到 Node.js

提前致谢

好的 - 我再次查看了建议的页面并对我的代码进行了一些重构并且成功了。我认为问题可能出在最初用于获取 token 的 Url。

我已经在初始化 oauth2Client

var OAuth2Client = googleapis.OAuth2Client;
var oauth2Client;
oauth2Client    = new OAuth2Client(config.google_login.client_id, config.google_login.client_secret, config.google_login.redirect_uri);

所需的 Client Id、secret 和重定向 Url 已在配置文件中定义

所以首先我改变了生成该 url 的方式。首先,我将登录 Google 的 Url 设置为 GoogleLogin.html,当服务器收到对此页面的请求时执行以下操作。

app.get('/GoogleLogin.html',function(req,res) {
    var scopes = "";

    // retrieve google scopes
    scopes += config.google_login.scopes.baseFeeds + " "
    scopes += config.google_login.scopes.calendar + " "
    scopes += config.google_login.scopes.drive + " "
    scopes += config.google_login.scopes.driveMetadata + " "
    scopes += config.google_login.scopes.profile + " "
    scopes += config.google_login.scopes.email + " "
    scopes += config.google_login.scopes.tasks

    var url = oauth2Client.generateAuthUrl({
        access_type: 'offline',
        scope: scopes
    });

    res.writeHead(302, {location: url});
    res.end();


});

这首先构建一个范围字符串,然后在重定向到生成的 url 之前生成授权 Url

当 Google 重定向回该站点时,将执行以下操作

app.get('/AuthorizeGoogle.html',function(req,res) {
    // var auth = new googleapis.OAuth2Client(config.google_login.client_id,config.google_login.client_secret, config.google_login.redirect_uri);
    var queryData   = url.parse(req.url,true).query;
    var code        = queryData.code;

    console.log('Authorization Request recieved ');;

    console.log('Retrieving token');
    oauth2Client.getToken(code,function(err,tokens) {
        console.log('Retrieved token ');
        console.log(tokens);
        console.log('**** ERROR ****');
        console.log(err);

        console.log('Calling setCredentials');
        oauth2Client.setCredentials(tokens);
        console.log('*****FINISHED!!!!!');
    });

    res.send('Authorization recieved ' + queryData.code)
});

现在可以正确返回 token 和刷新 token 。由于是相同的代码,所以我唯一能做错的就是对 Google 的原始调用。

最佳答案

我认为您最好的选择是研究以下 github 示例中的内容:https://github.com/google/google-api-nodejs-client/blob/master/examples/oauth2.js

关于javascript - 来自 Node.js 的 Javascript 中的 getToken 的 invalid_request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21100433/

相关文章:

javascript - 将 .png img 添加到 index.ejs 中动态创建的表中的 td 元素

Ubuntu : no valid seed servers in list 上的 Node.js MongoDB

javascript - 如何让nodejs返回对象 promise 以外的东西

javascript - 导出到 Excel/打印带网格线的 HTML 表格

javascript - 为什么我的 td onClick 没有触发?

javascript - 使用 javascript 在鼠标悬停时显示图像的灰度版本。在 Firefox 中可能吗?

javascript - Typescript、Browserify 和 angularjs 超出两个文件示例

javascript - 如何将数据从响应对象分配给对象

javascript - 选择具有相同名称但大写的对象属性

javascript - 如何让 eslint 为 Node js 中的整个目录工作