调用Google Drive API返回Invalid_grant OAuth2

Calling Google Drive API returns Invalid_grant OAuth2

本文关键字:grant OAuth2 Invalid 返回 Google Drive API 调用      更新时间:2023-09-26

已解决

问题不在于我的代码。我在Linux服务器上手动设置时间,这意味着谷歌的服务器拒绝让我访问。我在服务器上使用以下命令设置时间:ntpdate 0.europe.pool.ntp.org

原始问题:

我目前正在尝试用NodeJS开发一个小脚本,它从谷歌电子表格中读取一些值,并将其写入JSON格式的文件。

该代码在我的Mac上运行时没有任何问题,但在上传到Ubuntu 14.04.4后,"谷歌电子表格"模块中的一些函数无法工作,并返回未定义的结果。

我不能100%确定节点模块是否有问题,或者我的代码是否有问题。

进行调用的代码:

var GoogleSpreadsheet = require('google-spreadsheet');
var fs = require('fs');
function scheduleBidding(sheetSecretKey) {
    this.sheetKey = sheetSecretKey;
}
scheduleBidding.prototype.getBiddingHeaders = function (filePrefix) {
var doc = new GoogleSpreadsheet(this.sheetKey);
var sheet;
var daysSinceYearStart = getDaysIntoYear();
var localSheetKey = this.sheetKey;
var biddingObject = new Object();
var filePath = filePrefix+"staticConfigFiles/googleApiKey";
console.log(filePath);
fs.readFile(filePath, 'utf8', function (err, data) {
    creds = JSON.parse(data);
    console.log(creds);
    doc.useServiceAccountAuth(creds, function (err) {
        if(err) throw err;
        doc.getInfo(function (err, info) {
            if(err) throw err;
            console.log(info);
            sheet = info.worksheets[0];
            var numberOfColumns = sheet.colCount;
            sheet.getCells({
                'min-row': 1,
                'max-row': 1,
                'min-col': 2,
                'max-col': numberOfColumns,
                'return-empty': true
            }, function(err, cells) {
                sheet.getRows({
                    offset: 1,
                    limit: 366
                }, function( err, rows ){
                    yesterdaysBid = rows[daysSinceYearStart-2];
                    todaysBidding = rows[daysSinceYearStart-1];
                    cells.forEach(function (listItem, indexArray) {
                        columnName = cells[indexArray].value;
                        var functionName = String(columnName.toLowerCase());
                        functionName = functionName.replace(/'s/g, '').replace(":", "");
                        biddingObject[columnName] = {
                            "yesterdaysBid": yesterdaysBid[functionName],
                            "todaysBid": todaysBidding[functionName]
                        };
                        newFileName = filePrefix+"scheduleData/"+localSheetKey+".json";
                        prettyJson = JSON.stringify(biddingObject);
                        fs.writeFile(newFileName, prettyJson);
                        console.log("ScheduleBidding JSON has been updated");
                    });
                });
            });
        });
    });
});
function getDaysIntoYear() {
    var todaysDate = new Date();
    var daysAgo;
    //
    returnDays = parseInt(todaysDate.getDate())+daysAgo;
    return returnDays;
}
};

当我在Ubuntu服务器上执行脚本时,我收到了以下异常:

    Error: invalid_grant
www-0     at Request._callback (/SomePath/node_modules/google-spreadsheet/node_modules/google-auth-library/node_modules/gtoken/lib/index.js:215:34)
www-0     at Request.self.callback (/SomePath/node_modules/google-spreadsheet/node_modules/google-auth-library/node_modules/gtoken/node_modules/request/request.js:187:22)
www-0     at Request.EventEmitter.emit (events.js:98:17)
www-0     at Request.<anonymous> (/SomePath/node_modules/google-spreadsheet/node_modules/google-auth-library/node_modules/gtoken/node_modules/request/request.js:1044:10)
www-0     at Request.EventEmitter.emit (events.js:95:17)
www-0     at IncomingMessage.<anonymous> (/SomePath/node_modules/google-spreadsheet/node_modules/google-auth-library/node_modules/gtoken/node_modules/request/request.js:965:12)
www-0     at IncomingMessage.EventEmitter.emit (events.js:117:20)
www-0     at _stream_readable.js:920:16
www-0     at process._tickDomainCallback (node.js:459:13)

提前感谢您的帮助。如果缺少任何信息,请告诉我。

在我看来,您的身份验证凭据丢失或不正确。您还应该对useServiceAccountAuth回调中可能出现的错误做出反应。

var filePath = filePrefix+"/some/filePath.json"; // <- correct credentials?
doc.useServiceAccountAuth(creds, function (err) { 
  if(err) // handle error, you should see whats wrong
});