Node.js googleAPI如何仅通过GET请求对JWT客户端进行身份验证

时间:2019-03-24 04:00:51

标签: node.js google-api

众所周知,GoogleAPI基本上只是建立在GET请求上,而助手node.js模块googleapis似乎可以让您以更简单的方式发送这些GET请求,例如:

fetch("https://www.googleapis.com/drive/v3/files/MYID/export?mimeType=text/plain",
    {
        method:"GET",
        headers: {
            'Accept-Encoding': 'gzip',
            'User-Agent': 'google-api-nodejs-client/0.7.2 (gzip)',
            Authorization:t.token_type +" "+ t.access_token,
            Accept:"application/json"
        }
    })
        .then(res => res.text())
        .then(body => console.log(body));

与使用其内置方法的效果相同:

var drive = google.drive("v3");
    drive.files.export({
        auth:j,
        fileId:"1oBL8sBylvODW1Md0gjXf2UgwBHtb6aRcgyGOYr0kRvY"
    }, (e,r) => {
        if(e) console.log(e);
        else {
            console.log(r,"data:::::",r.data);
        }
    });

所以问题是:当我使用自定义服务密钥在nodejs中验证JWT客户端时,我会这样做:

var google = require("googleapis").google,
    creds = require("./mycreds.json"),
    fetch = require("node-fetch");


    var j = new google.auth.JWT(
        creds.client_email,
        null,
        creds.private_key,
        [
            "https://www.googleapis.com/auth/drive"
        ]
    );
    console.log(google.auth.JWT, "END this");

j.authorize((r,t) => {
    doOther(t);
});

我怀疑,就像到目前为止我所遇到的google API的其他每个部分一样,这只是一种将GET请求发送到具有某些标头(包含授权密钥)的特定URL的简便方法。

问题:要做与j.authorize相同的操作所需的URL和标头到底是什么?我找不到参考中的位置。

提示:从jwtclient.js文件的159行开始:

createGToken() {
        if (!this.gtoken) {
            this.gtoken = new gtoken_1.GoogleToken({
                iss: this.email,
                sub: this.subject,
                scope: this.scopes,
                keyFile: this.keyFile,
                key: this.key,
                additionalClaims: this.additionalClaims
            });
        }
        return this.gtoken;
    }

仍在查看gtoken_1.GoogleToken等。

0 个答案:

没有答案
相关问题