oAuth2Client很奇怪

时间:2019-06-14 15:59:45

标签: google-api oauth2client

我对oAuth2Client的对象感到很奇怪,当我直接传递对象时,我得到了“ API返回了一个错误:错误:该请求缺少有效的API密钥。”,但如果我通过了将其传递给其他变量,或者将其作为函数的参数传递即可。

知道为什么会发生吗?

api错误:

const {client_secret, client_id, redirect_uris} = {"installed":
    {x}}.installed;

const oAuth2Client = new google.auth.OAuth2(
client_id, client_secret, redirect_uris[0]);

oAuth2Client.setCredentials({x})


return google.sheets({version: 'v4', oAuth2Client}).spreadsheets.values.get({
    spreadsheetId: options.sheetid,
    range: options.range,
    majorDimension: 'COLUMNS',
    }, (err, res) => {
    if (err) {
        console.log('The API returned an error: ' + err);
        return reject ({status: 919,data: []});
    }

    if(res.data && res.data.values){
        return resolve({status: 420,data: res.data.values});
    }

    return reject({status: 125,data: []});
    });

});

添加“让auth = oAuth2Client;”没有api错误,可以正常工作

oAuth2Client.setCredentials({x})

let auth = oAuth2Client;

return google.sheets({version: 'v4', auth }).spreadsheets.values.get({
    spreadsheetId: options.sheetid,
    range: options.range,
    majorDimension: 'COLUMNS',
    }, (err, res) => {

将auth放入功能参数中也可以正常工作

return new Promise((resolve,reject)=>{
const {client_secret, client_id, redirect_uris} = {"installed":
    {x}}.installed;

const oAuth2Client = new google.auth.OAuth2(
client_id, client_secret, redirect_uris[0]);

oAuth2Client.setCredentials({x})

function pull(auth){
return google.sheets({version: 'v4', auth}).spreadsheets.values.get({
    spreadsheetId: options.sheetid,
    range: options.range,
    majorDimension: 'COLUMNS',
    }, (err, res) => {
    if (err) {
        console.log('The API returned an error: ' + err);
        return reject ({status: 919,data: []});
    }

    if(res.data && res.data.values){
        return resolve({status: 420,data: res.data.values});
    }

    return reject({status: 125,data: []});
    });
}
pull(oAuth2Client);

0 个答案:

没有答案