超出未经身份验证的使用的每日限制。继续使用需要注册错误

时间:2016-05-04 12:54:16

标签: google-drive-api

我正在尝试使用node.js从google驱动器下载文件。这是我第一次尝试使用google sdks,所以我只是按照说明操作并复制了示例中的代码。我可以成功地从我的gdrive中存在的文件中获取元数据,但每当我尝试下载文件时,都会抛出错误。请在下面找到我正在使用的代码片段,它只是文档中提供的示例。

            var fs = require('fs');
            var readline = require('readline');
            var google = require('googleapis');
            var googleAuth = require('google-auth-library');
            var request = require("request");

            // If modifying these scopes, delete your previously saved credentials
            // at ~/.credentials/drive-nodejs-quickstart.json
            //var SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly'];
            var SCOPES = ['https://www.googleapis.com/auth/drive'];
            /* var TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH ||
                process.env.USERPROFILE) + '/.credentials/'; */
            var TOKEN_DIR = process.env.USERPROFILE + '/.credentials/'; 
            var TOKEN_PATH = TOKEN_DIR + 'drive-nodejs-quickstart.json';

            // Load client secrets from a local file.
            fs.readFile('client_secret.json', function processClientSecrets(err, content) {
              if (err) {
                console.log('Error loading client secret file: ' + err);
                return;
              }
              // Authorize a client with the loaded credentials, then call the
              // Drive API.
              authorize(JSON.parse(content), listFiles);
            });

            /**
             * Create an OAuth2 client with the given credentials, and then execute the
             * given callback function.
             *
             * @param {Object} credentials The authorization client credentials.
             * @param {function} callback The callback to call with the authorized client.
             */
            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();
              var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);

              // Check if we have previously stored a token.
              fs.readFile(TOKEN_PATH, function(err, token) {
                if (err) {
                  getNewToken(oauth2Client, callback);
                } else {
                  oauth2Client.credentials = JSON.parse(token);
                  callback(oauth2Client);
                }
              });
            }

            /**
             * Get and store new token after prompting for user authorization, and then
             * execute the given callback with the authorized OAuth2 client.
             *
             * @param {google.auth.OAuth2} oauth2Client The OAuth2 client to get token for.
             * @param {getEventsCallback} callback The callback to call with the authorized
             *     client.
             */
            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);
                });
              });
            }

            /**
             * Store token to disk be used in later program executions.
             *
             * @param {Object} token The token to store to disk.
             */
            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);
            }

            /**
             * Lists the names and IDs of up to 10 files.
             *
             * @param {google.auth.OAuth2} auth An authorized OAuth2 client.
             */
            function listFiles(auth) {
              var service = google.drive('v3');
              service.files.list({
                auth: auth,
                pageSize: 10,
                fields: "nextPageToken, files(id, name)"
              }, function(err, response) {
                if (err) {
                  console.log('The API returned an error: ' + err);
                  return;
                }
                var files = response.files;
                if (files.length == 0) {
                  console.log('No files found.');
                } else {
                  for (var i = 0; i < files.length; i++) {
                    var file = files[i];
                    if(file.name.indexOf('Expense') > -1) {
                        downloadFile(file.id);
                    }


                  }
                }
              });
            }

            /**
             * Download a file's content.
             *
             * @param {File} file Drive File instance.
             * @param {Function} callback Function to call when the request is complete.
             */


            function downloadFile(fileId) {
            //file['exportLinks']['application/pdf'];

              //var fileId = '0BwwA4oUTeiV1UVNwOHItT0xfa2M';
                    var service = google.drive('v3');
                    var dest = fs.createWriteStream('/test.doc');
                    service.files.get({
                       fileId: fileId,
                       alt: 'media'
                    })
                    .on('end', function() {
                      console.log('Done');
                    })
                    .on('error', function(err) {
                      console.log('Error during download', err);
                    })
                    .pipe(dest);

            }

我在过去一小时内只尝试了17-20个请求,并且我认为我没有使用过所有配额。我已经检查过我的项目中启用了GDrive api。请帮忙。

1 个答案:

答案 0 :(得分:0)

根据此documentation,错误403表示您已达到Google Drive API的最高请求率。限制因请求的类型而异。建议的操作是Batch the requests。尽量减少客户端必须进行的HTTP连接数量。

另一种解决方法是在开发者控制台中查看Google+ API的状态。它应该设置为ON。打开后等待几分钟,确保获得新的令牌。

检查这些相关的SO问题: