无法通过Drive API在节点错误中获取文件:超出未经验证使用的每日限制。继续使用需要注册。] [英] Unable to get file via Drive API in Node-Error:Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.]
本文介绍了无法通过Drive API在节点错误中获取文件:超出未经验证使用的每日限制。继续使用需要注册。]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
{
error:{
errors :[
{
domain:usageLimits,
reason:dailyLimitExceededUnreg,
message:超出未认证使用的每日限额。注册。,
extendedHelp:https://code.google.com/apis/console
}
],
code:403,
消息:超出未经验证使用的每日限制。继续使用需要注册。
}
}
我想要做的是下载内容一个特定的Google表格到我的桌面。我如此处所述的那样遵循Google Drive API文档, 这里,所以我不知道我哪里出错了。
以下是完整的代码:
var express = require( '表现');
var app = express();
var fs = require('fs');
var readline = require('readline');
var google = require('googleapis');
var googleAuth = require('google-auth-library');
var SCOPES = [
'https://www.googleapis.com/auth/drive'
];
var TOKEN_DIR =(process.env.HOME || process.env.HOMEPATH ||
process.env.USERPROFILE)+'/.credentials/';
var TOKEN_PATH = TOKEN_DIR +'test-app-1.json';
$ b fs.readFile('client_secret.json',function processClientSecrets(err,content){
if(err){
console.log('Error loading客户机秘密文件:'+ err);
return;
}
authorize(JSON.parse(content),getFile);
});
函数授权(凭证,回调){
var clientSecret = credentials.installed.client_secret;
var clientId = credentials.installed.client_id;
var redirectUrl = credentials.installed.redirect_uris [0];
var auth = new googleAuth();
oauth2Client = new auth.OAuth2(clientId,clientSecret,redirectUrl);
fs.readFile(TOKEN_PATH,function(err,token){
if(err){
getNewToken(oauth2Client,callback);
} else {
var token = token;
oauth2Client.credentials = JSON.parse(token);
callback(oauth2Client);
});
$ b $ function getNewToken(oauth2Client,callback){
var authUrl = oauth2Client.generateAuthUrl({
access_type:'offline',
scope:SCOPES
});
console.log('通过访问此URL授权此应用程序:',authUrl);
var rl = readline.createInterface({
input:process.stdin,
output:process.stdout
});
rl.question('从该页面输入代码:',function(code){
rl.close();
oauth2Client.getToken(code,function(err,token) {
if(err){
console.log('尝试检索访问令牌时发生错误,错误);
返回;
}
oauth2Client.credentials = token;
storeToken(token);
callback(oauth2Client);
});
});
函数storeToken(令牌){
try {
fs.mkdirSync(TOKEN_DIR);
} catch(err){
if(err.code!='EEXIST'){
throw err;
}
}
fs.writeFile(TOKEN_PATH,JSON.stringify(token));
console.log('Token stored to'+ TOKEN_PATH);
}
function getFile(auth){
var drive = google.drive('v3');
var fileId ='< FileId>';
var dest = fs.createWriteStream('/ desktop / test.xlsx');
drive.files.get({
fileId:fileId,
mimeType:'application / vnd.openxmlformats-officedocument.spreadsheetml.sheet'
})
.on ('end',function(){
console.log('Done');
})
.on('error',function(err){
console。日志('下载期间出错',错误);
})
.pipe(dest);
解决方案
code> auth 在你的代码中。
另外,你需要一个特殊的格式,我建议使用 export
来代替。
function getFile(auth){
var drive = google.drive('v3');
var fileId ='< FileId>';
var dest = fs.createWriteStream('/ desktop / test.xlsx');
drive.files.export({
auth:auth,
fileId:fileId,
mimeType:'application / vnd.openxmlformats-officedocument.spreadsheetml.sheet'
})
.on('end',function(){
console.log('Done');
})
.on('error',function(err ){
console.log('下载期间出错',错误);
})
.pipe(dest);
}
Here is the full error:
{
"error": {
"errors": [
{
"domain": "usageLimits",
"reason": "dailyLimitExceededUnreg",
"message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.",
"extendedHelp": "https://code.google.com/apis/console"
}
],
"code": 403,
"message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."
}
}
What I want to do is to download the contents of a particular Google Sheet to my desktop. I was following the Google Drive API documentation as outlined here and here, so I have no idea where I am going wrong.
Here is the full code:
var express = require('express');
var app = express();
var fs = require('fs');
var readline = require('readline');
var google = require('googleapis');
var googleAuth = require('google-auth-library');
var SCOPES = [
'https://www.googleapis.com/auth/drive'
];
var TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH ||
process.env.USERPROFILE) + '/.credentials/';
var TOKEN_PATH = TOKEN_DIR + 'test-app-1.json';
fs.readFile('client_secret.json', function processClientSecrets(err, content) {
if (err) {
console.log('Error loading client secret file: ' + err);
return;
}
authorize(JSON.parse(content), getFile);
});
function authorize(credentials, callback) {
var clientSecret = credentials.installed.client_secret;
var clientId = credentials.installed.client_id;
var redirectUrl = credentials.installed.redirect_uris[0];
var auth = new googleAuth();
oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);
fs.readFile(TOKEN_PATH, function(err, token) {
if (err) {
getNewToken(oauth2Client, callback);
} else {
var token = token;
oauth2Client.credentials = JSON.parse(token);
callback(oauth2Client);
}
});
}
function getNewToken(oauth2Client, callback) {
var authUrl = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES
});
console.log('Authorize this app by visiting this url: ', authUrl);
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Enter the code from that page here: ', function(code) {
rl.close();
oauth2Client.getToken(code, function(err, token) {
if (err) {
console.log('Error while trying to retrieve access token', err);
return;
}
oauth2Client.credentials = token;
storeToken(token);
callback(oauth2Client);
});
});
}
function storeToken(token) {
try {
fs.mkdirSync(TOKEN_DIR);
} catch (err) {
if (err.code != 'EEXIST') {
throw err;
}
}
fs.writeFile(TOKEN_PATH, JSON.stringify(token));
console.log('Token stored to ' + TOKEN_PATH);
}
function getFile(auth) {
var drive = google.drive('v3');
var fileId = '<FileId>';
var dest = fs.createWriteStream('/desktop/test.xlsx');
drive.files.get({
fileId: fileId,
mimeType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
})
.on('end', function() {
console.log('Done');
})
.on('error', function(err) {
console.log('Error during download', err);
})
.pipe(dest);
}
解决方案
You don't use auth
in your code.
Also, you want a special format, I suggest using export
instead.
function getFile(auth) {
var drive = google.drive('v3');
var fileId = '<FileId>';
var dest = fs.createWriteStream('/desktop/test.xlsx');
drive.files.export({
auth: auth,
fileId: fileId,
mimeType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
})
.on('end', function() {
console.log('Done');
})
.on('error', function(err) {
console.log('Error during download', err);
})
.pipe(dest);
}
这篇关于无法通过Drive API在节点错误中获取文件:超出未经验证使用的每日限制。继续使用需要注册。]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文