0

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?

  • When you check the [documentation](https://developers.google.com/drive/v3/web/quickstart/nodejs), it was stated in the prerequisites that when trying the quickstart, you'll need to these things with you (`Node.js installed`, `The` [**`npm`**](https://www.npmjs.com/) `package management tool (comes with Node.js)`, `Access to the internet and a web browser` and `A Google account with Google Drive enabled)`. Have some time to try it. Also your error tells you that you are using an [expired or invalid access token](https://developers.google.com/drive/v3/web/handle-errors#401_invalid_credentials). – MαπμQμαπkγVπ.0 Jan 05 '18 at 11:42
  • "you'll need to these things with you (Node.js installed, The npm package management tool (comes with Node.js), Access to the internet and a web browser and A Google account with Google Drive enabled) Have some time to try it." All this was done before i posted this question! – Love and peace - Joe Codeswell Jan 05 '18 at 16:17

0 Answers0