调用 Google Drive API returns Invalid_grant OAuth2

Calling Google Drive API returns Invalid_grant OAuth2

已解决!

问题不是我的代码。我在我的 Linux 服务器上手动设置了时间,这意味着 Google 的服务器拒绝给我访问权限。我在我的服务器上使用以下命令来设置时间:ntpdate 0.europe.pool.ntp.org

原刊:

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

代码在我的 Mac 上运行没有任何问题,但是在上传到 Ubuntu 14.04.4 后,'google-spreadsheet' 模块中的一些功能无法运行并且returns未定义。

我不是100%确定,是node模块有问题,还是我的代码有问题。

调用的代码:

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
});

感谢您的帮助!我终于解决了这个问题。这不是代码,而是我的服务器设置。我已经更新了问题,以更具体地与问题相关。