Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need some help #23

Open
ghost opened this issue May 13, 2016 · 5 comments
Open

Need some help #23

ghost opened this issue May 13, 2016 · 5 comments

Comments

@ghost
Copy link

ghost commented May 13, 2016

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!

@extrabacon
Copy link
Owner

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).

@ghost
Copy link
Author

ghost commented May 13, 2016

"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)

@extrabacon
Copy link
Owner

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

@ghost
Copy link
Author

ghost commented May 13, 2016

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

@extrabacon
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant