Friday 18 March 2016

Google-drive api (NodeJS) Error: Request had insufficient authentication scopes

Hello!I am trying to generate a URL to send to users so I may access a particular sheet(if they accept the request). To that end, I was using the tutorial at Google Drive REST API here, and I was able to successfully call authorize(JSON.parse(content), listFiles); to get a list of files in a user's drive. Now, what I actually want is to read the contents of a particular spreedsheet, so I looked around and found this here :function callAppsScript(auth) { var scriptId = 'ENTER_YOUR_SCRIPT_ID_HERE'; var script = google.script('v1'); // Make the API request. The request object is included here as 'resource'. script.scripts.run({ auth: auth, resource: { function: 'getFoldersUnderRoot' }, scriptId: scriptId }, function(err, resp) { if (err) { // The API encountered a problem before the script started executing. console.log('The API returned an error: ' + err); return; } if (resp.error) { // The API executed, but the script returned an error. // Extract the first (and only) set of error details. The values of this // object are the script's 'errorMessage' and 'errorType', and an array // of stack trace elements. var error = resp.error.details[0]; console.log('Script error message: ' + error.errorMessage); console.log('Script error stacktrace:'); if (error.scriptStackTraceElements) { // There may not be a stacktrace if the script didn't start executing. for (var i = 0; i < error.scriptStackTraceElements.length; i++) { var trace = error.scriptStackTraceElements[i]; console.log('\t%s: %s', trace.function, trace.lineNumber); } } } else { // The structure of the result will depend upon what the Apps Script // function returns. Here, the function returns an Apps Script Object // with String keys and values, and so the result is treated as a // Node.js object (folderSet). var folderSet = resp.response.result; if (Object.keys(folderSet).length == 0) { console.log('No folders returned!'); } else { console.log('Folders under your root folder:'); Object.keys(folderSet).forEach(function(id){ console.log('\t%s (%s)', folderSet[id], id); }); } } }); } I pasted this code in my quickstart.js file, but now, when I called authorize(JSON.parse(content), callAppsScript);, I got the following error:The API returned an error: Error: Request had insufficient authentication scopes Here is the complete code :var fs = require('fs'); var readline = require('readline'); var google = require('googleapis'); var googleAuth = require('google-auth-library'); var SCOPES = ['http://ift.tt/JALASw','http://ift.tt/1mEd3VM','http://ift.tt/AjNHPk','http://ift.tt/1isPOKm','http://ift.tt/1cMkUd0']; var TOKEN_DIR = (process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE) + '/.credentials/'; var TOKEN_PATH = TOKEN_DIR + 'drive-nodejs-quickstart.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), callAppsScript); }); 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); } }); } 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 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 { console.log('Files:'); for (var i = 0; i < files.length; i++) { var file = files[i]; console.log('%s (%s)', file.name, file.id); } } }); } function callAppsScript(auth) { //same as above }else { var folderSet = resp.response.result; if (Object.keys(folderSet).length == 0) { console.log('No folders returned!'); } else { console.log('Folders under your root folder:'); Object.keys(folderSet).forEach(function(id){ console.log('\t%s (%s)', folderSet[id], id); }); } } }); }

Submitted March 18, 2016 at 11:17PM by CreaTuRe_X

No comments:

Post a Comment