0

Hier ist der vollständige Fehler.Die Datei konnte nicht über die Drive-API abgerufen werden. Knotefehler: Tägliches Limit für nicht authentifizierte Verwendung überschritten. Die fortgesetzte Nutzung erfordert die Registrierung]

{ 
"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." 
} 
} 

Was ich tun möchte, ist, den Inhalt eines bestimmten Google-Blatt auf meinem Desktop zum Download bereit. Ich habe die Google Drive-API-Dokumentation wie in den Abschnitten here und here beschrieben verfolgt, sodass ich keine Ahnung habe, wo ich falsch liege.

Hier ist der vollständige 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);  
} 
+0

haben Sie die API aktiviert, die Sie verwenden? Bitte überprüfen Sie Ihre Google Developer Console, falls diese aktiviert ist. Basierend auf dieser [vorherigen Frage] (http://stackoverflow.com/questions/35467945/google-drive-api-error-daily-limit-for-unauthenticated-use-exceeded?rq=1) erhalten Sie diesen Fehler wenn Sie führen eine Anfrage ohne API-Schlüssel (alte API) oder (client_secret, client_id) aus. –

+0

Dieser Fehler bedeutet normalerweise, dass Ihre GET-Anfrage einen Autorisierungs-HTTP-Header fehlt. – pinoyyid

Antwort

0

Sie nicht auth in Ihrem Code verwenden. Sie möchten auch ein spezielles Format, ich empfehle stattdessen 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('Error during download', err); 
    }) 
    .pipe(dest);  
}