Skip to content

Instantly share code, notes, and snippets.

@acucciniello
Created October 10, 2016 00:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acucciniello/2b747e5291673da5c92973a8adf6ec00 to your computer and use it in GitHub Desktop.
Save acucciniello/2b747e5291673da5c92973a8adf6ec00 to your computer and use it in GitHub Desktop.
I am able to print out the token, but Google says there is none set
//docs.js
var fs = require('fs');
var readline = require('readline');
var google = require('googleapis');
var googleAuth = require('google-auth-library');
//If modifying these scopes, delete your previously saved credentials
// at ~/.credentials/drive-nodejs-quikcstart.json
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 + 'drive-nodejs-quikcstart.json';
module.exports = function loadDocs(clientSecretsFile, accessToken, SPEECH, fillOutput, sendResponseCB) {
fs.readFile(clientSecretsFile.toString(), function processClientSecrets(err, content) {
if(err){
console.log('Error Loading client secret file: ' + err);
return;
}
//console.log(accessToken);
//SPEECH = "Here is your access token: " + accessToken;
//console.log(JSON.parse(content));
//callback(SPEECH);
authorize(JSON.parse(content), listFiles, accessToken, SPEECH, fillOutput, sendResponseCB);
});
}
// Load client secrets from a local file.
/**
* 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, listFilesFunction, token, speech, callback, sendResponseCB){
var clientSecret = credentials.web.client_secret;
var clientId = credentials.web.client_id;
var redirectUrl = credentials.web.redirect_uris[4];
var auth = new googleAuth();
var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);
//Check if we have previously stored a token
if(token == undefined) {
speech = "token is undefined, please link your account to use this skill";
/*oauth2Client.refreshAccessToken(function(err, tokens){
if(err)
{
speech = "failed getting refresh token the error was : " + err;
return;
}
oauth2Client.setCredentials(tokens);
});
//console.log(speech);*/
sendResponseCB(speech);
}
else{
//speech = "Here is your access token: " + token;
oauth2Client.credentials = JSON.parse(token);
//console.log(oauth2Client.credentials);
//sendResponseCB(speech);
listFilesFunction(oauth2Client, callback, speech, sendResponseCB);
}
}
/**
* 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 r1 = readline.createInterface({
input: process.stdin,
output: process.stdout
});
r1.question('Enter the code from that page here: ', function(code){
r1.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 100 files.
*
* @param {google.auth.OAuth2} auth An authorized OAuth2 client.
*/
function listFiles(auth, callback, speech, sendResponseCB) {
var service = google.drive('v3');
service.files.list({
auth: auth,
pageSize: 10,
fields: "nextPageToken, files(id, name)"
}, function(err, response){
if(err) {
speech = auth.credentials + ' The API return an error: ' + err;
sendResponseCB(speech);
console.log('The API return an error: ' + err);
return;
}
files = response.files;
if ( files.length == 0) {
speech = "No files found.";
console.log(' No files found.');
sendResponseCB(speech);
//callback(err, null, speech, sendResponseCB);
} else {
speech = "We got access to your files";
sendResponseCB(speech);
//callback(null, files, speech, sendResponseCB)
}
});
}
//enables strict mode which helps catch common JS programming blunders
'use strict';
var APP_ID = 'amzn1.ask.skill.9fadc9b7-4377-44f0-9d40-9e7f015a8bf7';
var AlexaSkill = require('./AlexaSkill');
var SPEECH_OUTPUT = new Object();
const docs = require('./docs.js');
var clientSecretsFile = 'client_secret.json';
//Define a ListFilesService function which inherits from AlexaSkill.js class
var ListFilesService = function() {
AlexaSkill.call(this, APP_ID);
};
ListFilesService.prototype = Object.create(AlexaSkill.prototype);
//builds a response to Alexa skill interface and
//tells Alexa how to respond to users request
var ListFilesResponseFunction = function(intent, session, response) {
var token = JSON.stringify(session.user.accessToken);
//SPEECH_OUTPUT = "Here is your access token: " + JSON.stringify(session.user.accessToken);
docs(clientSecretsFile, token, SPEECH_OUTPUT, callback, function(speech) {
console.log("we made it here");
console.log(speech);
response.tell(speech);
});
};
//this will be invoked when the user first launches or opens the skill with its invocation name
//this is triggered when said "Alexa, list of files"
ListFilesService.prototype.eventHandlers.onLaunch = ListFilesResponseFunction;
ListFilesService.prototype.intentHandlers = {
'ListFilesIntent' : ListFilesResponseFunction
};
exports.handler = function(event, context) {
var listFilesService = new ListFilesService();
listFilesService.execute(event, context);
};
function callback(err, files) {
if(err){
console.log("Error in callback")
return err;
}
for (var i = 0; i < files.length; i++) {
SPEECH_OUTPUT[i] = files[i].name;
JSON.stringify(SPEECH_OUTPUT[i]);
//console.log(SPEECH_OUTPUT[i].name);
}
console.log(SPEECH_OUTPUT);
};
function getUserToken(err, token) {
SPEECH_OUTPUT = JSON.stringify(token);
response.tell(SPEECH_OUTPUT);
}
@acucciniello
Copy link
Author

At line 129 of docs.js that is where I have the token and the error message getting added to the speech response and then the callback is called which makes alexa say it. The output from alexa is (access_token would be here but removed for privacy) The API return an error: Error: No access or refresh token is set."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment