调用Google Drive API返回Invalid_grant OAuth2
Calling Google Drive API returns Invalid_grant OAuth2
已解决
问题不在于我的代码。我在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
});
相关文章:
- 公共第一方客户端的适当OAuth2流
- Node JS-构造OAuth2请求
- 在appcelerator中使用OAuth2
- 应用程序引擎终结点:用于OAuth2授权的Javascript客户端库需要多个弹出窗口
- 使用hello.js自定义OAuth2提供程序
- 如何通过Google Drive Javascript OAuth2 API创建Google Sheets文档
- 可以'无法使用Oauth2/BaseCamp API获取响应数据
- 客户端JavaScript Facebook OAuth2实现
- Grant vs Passport.js?
- IFRAME沙盒中的Google OAuth2和应用程序脚本
- 如何在Javascript中实现安全的OAuth2消费
- 如何正确使用Google'的Javascript OAuth2.0库
- Yahoo - OAuth2 - SocialAPI:在初始响应中不返回“Access-Control-Allow-Or
- 如何在同一域中托管的 JS 应用和 OAuth2.0 服务器之间安全地通信
- 将参数传递给 uber 回调网址 Oauth2
- 使用 Oauth2 将 REST 用于 Google 云端硬盘
- Google Oauth2 使用 javascript API 登录不适用于 IE
- 如何使用Google OAuth2 javascript获取刷新令牌
- 在哪里存储具有第三方OAuth2服务的SPA / REST应用程序的refresh_token
- OAuth2 Implicit Grant with Ajax