How to use "node-rest-client" npm package with google drive API?

503 Views Asked by At

I have the following "node-rest-client" npm package code. However, using it with Google Drive API causes a 'Login Required' error. How can I use the "node-rest-client" npm package with the Google Drive API?

Here is the code.

const fs = require('fs-extra') // this will have all of a copy of the normal fs methods as well  https://www.npmjs.com/package/fs.extra
var inFileId = 'DELETED-INFO' // january 7 from david

var Client = require('node-rest-client').Client
var client = new Client()
// define args
var args = {
    path: { "fileId": inFileId }, 
    // commented out by joe for testing
    // data: { test: "hello" }, // data passed to REST method (only useful in POST, PUT or PATCH methods) 
    // path: { "id": 120 }, // path substitution var 
    // parameters: { arg1: "hello", arg2: "world" }, // this is serialized as URL parameters 
    headers: { "test-header": "client-api" } // request headers 
}

// register Method - see https://developers.google.com/drive/v3/reference/files/copy
client.registerMethod("joesJsonMethod", "https://www.googleapis.com/drive/v3/files/${fileId}/copy", "POST");


// run registered Method with args  == Let her rip - joe
const out = fs.createWriteStream('./stdout.log') 
const err = fs.createWriteStream('./stderr.log')
const jconsole = new console.Console(out, err);

client.methods.joesJsonMethod(args, function (data, response) {
    // parsed response body as js object
    jconsole.log('j-=-= parsed response body as js object' )
    jconsole.log(data);

    // raw response 
    jconsole.log('\n\nj-=-= raw response' )
    jconsole.log(response);
});

Here is the stdout.log error message.

j-=-= parsed response body as js object
{ error: { errors: [ [Object] ], code: 401, message: 'Login Required' } }

How can I use the "node-rest-client" npm package with the Google Drive API?

0

There are 0 best solutions below