0

I've been getting this error:

Expected OAuth 2 access token, login cookie or other valid authentication credential

whenever I run my program. Here is my code.

const fs = require('fs');
const readline = require('readline');
const {google} = require('googleapis');
const promise = require('promise');
const slides = google.slides({
    version: 'v1',
    auth: 'CLIENT_SECRET'
});
const SCOPES = ['https://www.googleapis.com/auth/presentations', 'https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/drive.file', 'https://www.googleapis.com/auth/drive.appdata', 'https://www.googleapis.com/auth/plus.me', 'https://www.googleapis.com/auth/userinfo.profile', 'https://www.googleapis.com/auth/userinfo.email'];
const TOKEN_PATH = 'token.json';

fs.readFile('credentials.json', (err, content) => {
    if (err) return console.log('Error loading client secret file:', err);
    authorize(JSON.parse(content), createSlide);
});

function authorize(credentials, callback) {
    const {client_secret, client_id, redirect_uris} = credentials.installed;
    const oAuth2Client = new google.auth.OAuth2(
        client_id, client_secret, redirect_uris[0]
    );
    fs.readFile(TOKEN_PATH, (err, token) => {
        if (err) return getNewToken(oAuth2Client, callback);
        oAuth2Client.setCredentials(JSON.parse(token));
        callback(oAuth2Client);
    });
}

function getNewToken(oAuth2Client, callback) {
    const authUrl = oAuth2Client.generateAuthUrl({
        access_type: 'offline',
        scope: SCOPES,
    });
    console.log('Authorize this app by visiting this url:', authUrl);
    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdout,
    });
    rl.question('Enter the code from that page here: ', (code) => {
        rl.close();
        oAuth2Client.getToken(code, (err, token) => {
            if (err) return console.error('Error retrieving access token', err);
            oAuth2Client.setCredentials(token);
            fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => {
                if (err) console.error(err);
                console.log('Token stored to', TOKEN_PATH);
            });
            callback(oAuth2Client);
        });
    });
}

function createSlide(presentationId, pageId, auth) {
    return new Promise((resolve, reject) => {
        let requests = [{
            createSlide: {
                objectId: pageId,
                insertionIndex: '1',
                slideLayoutReference: {
                    predefinedLayout: 'TITLE_AND_TWO_COLUMNS',
                },
            },
        }];

        //ELEMENTS

        slides.presentations.batchUpdate({
            presentationId,
            resource: {
                requests,
            },
            }, (err, res) => {
                if (err) return console.log("Error: " + err);
                console.log(`Created slide with ID: ${res.replies[0].createSlide.objectId}`);
                resolve(res);
        });
    });
}

The code is from the node.js quickstart (https://developers.google.com/slides/quickstart/nodejs).

The function createSlide() is from the Github Node.Js snippets (https://github.com/gsuitedevs/node-samples/blob/master/slides/snippets/snippets.js#L78)

I have tried the code on the Github for the Google APIs Node.js Client (https://github.com/googleapis/google-api-nodejs-client)

I am fairly new to the Google API and Node.Js, so I'm still learning about async and promise. Can someone tell me what I am doing wrong, and perhaps provide a full working example (not snippets)? Thanks in advance!

Arkin Solomon
  • 592
  • 1
  • 5
  • 23
  • I'm not sure how or why you are experiencing the error. Try following the [codelab](https://codelabs.developers.google.com/codelabs/slides-api/#0), this is a complete step by step tutorial to use Slide API with node.js that include creating a slide. – Mr.Rebot Nov 01 '18 at 00:44
  • @Mr.Rebot It worked! I don't know how I logged on today and it worked. It might have been a google issue, but I doubt it. – Arkin Solomon Nov 01 '18 at 05:13
  • Your current code is now working? or the codelab? – Mr.Rebot Nov 01 '18 at 05:14
  • My current code @Mr.Rebot – Arkin Solomon Nov 13 '18 at 00:58

0 Answers0