簡體   English   中英

調用Google Drive API會返回Invalid_grant OAuth2

[英]Calling Google Drive API returns Invalid_grant OAuth2

解決了!

問題不是我的代碼。 我會在Linux服務器上手動設置時間,這意味着Google的服務器拒絕授予我訪問權限。 我在服務器上使用了以下命令來設置時間: ntpdate 0.europe.pool.ntp.org

原始發行:

我目前正在嘗試在NodeJS中開發一個小的腳本,該腳本從Google Spreadsheet讀取一些值,並將其寫入JSON格式的文件中。

代碼在我的Mac上正常運行,但是在上傳到Ubuntu 14.04.4之后,“ google-spreadsheet”模塊中的某些功能無法正常工作,並且返回未定義。

我不確定節點模塊是否有問題,或者我的代碼是否有問題,不是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
});

謝謝您的幫助! 我終於解決了問題。 不是代碼,而是我的服務器設置。 我已經更新了問題,以更具體地解決該問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM