google-oauth-jwt icon indicating copy to clipboard operation
google-oauth-jwt copied to clipboard

Need some help

Open ghost opened this issue 9 years ago • 5 comments

Hi guys, I can not get what should I do with tokens that I get after auth methods, can You show some examples, I need to make req to google.drive and make insert, where I should use token, please help, thx!

ghost avatar May 13 '16 13:05 ghost

If I understand correctly, you have successfully obtained a token via the authenticate method. For properly authenticating a request with most Google APIs, you need to add this HTTP header: Authorization: Bearer YOUR_TOKEN_HERE.

That's great if you want to build your own HTTP requests for tools like curl or any http client for Node. However, this module comes bundled with a plugin for the request module which does all the work automatically (see the docs for more details).

extrabacon avatar May 13 '16 14:05 extrabacon

"If I understand correctly, you have successfully obtained a token via the authenticate method", Yes, I did. If I want to make req with module, should I use it after authenticate method? Should I send token to this module? Thank you)

ghost avatar May 13 '16 14:05 ghost

You have 2 options: authenticate allows you to get the token and use it anywhere you like, but you can also use this module to make the request as well, via a modified instance of the request module bundled with this module. This way the token is requested, cached, reused and renewed automatically.

If you just want to make a request, change your code from:

var googleAuth = require('google-oauth-jwt');

googleAuth.authenticate({
  // use the email address of the service account, as seen in the API console
  email: '[email protected]',
  // use the PEM file we generated from the downloaded key
  keyFile: 'my-service-account-key.pem',
  // specify the scopes you wish to access
  scopes: ['https://www.googleapis.com/auth/drive.readonly']
}, function (err, token) {
  console.log(token);
});

to:

// obtain a JWT-enabled version of request
var request = require('google-oauth-jwt').requestWithJWT();

request({
  url: 'https://www.googleapis.com/drive/v2/files',
  jwt: {
    // use the email address of the service account, as seen in the API console
    email: '[email protected]',
    // use the PEM file we generated from the downloaded key
    keyFile: 'my-service-account-key.pem',
    // specify the scopes you wish to access - each application has different scopes
    scopes: ['https://www.googleapis.com/auth/drive.readonly']
  }
}, function (err, res, body) {
    console.log(JSON.parse(body));
});

extrabacon avatar May 13 '16 14:05 extrabacon

And how I should use this req to make google.drive.insert?

ghost avatar May 13 '16 15:05 ghost

This module will help you with the authorization part only. You need to follow the official documentation for the rest.

https://developers.google.com/drive/v3/web/manage-uploads#uploads

extrabacon avatar May 13 '16 15:05 extrabacon