Google Slides API Node.Js请求缺少有效的身份验证凭据

时间:2018-10-30 06:36:31

标签: javascript node.js google-oauth2 google-slides-api

我一直收到此错误:

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

每当我运行程序时。这是我的代码。

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);
        });
    });
}

该代码来自node.js快速入门(https://developers.google.com/slides/quickstart/nodejs)。

函数createSlide()来自Github Node.Js片段(https://github.com/gsuitedevs/node-samples/blob/master/slides/snippets/snippets.js#L78

我已经在Github上尝试了Google API Node.js客户端(https://github.com/googleapis/google-api-nodejs-client)的代码

我对Google API和Node.J相当陌生,因此我仍在学习异步和承诺。有人可以告诉我我做错了什么,也许可以提供一个完整的工作示例(而不是摘要)?预先感谢!

0 个答案:

没有答案