NodeJS access API officially supported by Google provides background login authorization

  • 2020-03-30 03:38:08
  • OfStack

The installation

This library is published through NPM. Install googleapis and their dependencies with the following command


$ npm install googleapis

use

Example 1: get the full address from the Google short address


 var google = require('googleapis');
 var urlshortener = google.urlshortener('v1');
 var params = { shortUrl: 'http://goo.gl/xKbRu3' };
 // get the long url of a shortened url
 urlshortener.url.get(params, function (err, response) {
  console.log('Long url is', response.longUrl);
 });

Example 2: login authorization

This example integrates OAuth2 authentication, which allows you to obtain the user's access Token and flush the Token to prevent session expiration.

   


 var google = require('googleapis');
 var plus = google.plus('v1');
 var oauth2Client = new OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL);
 // Retrieve tokens via token exchange explained above or set them:
 oauth2Client.setCredentials({
  access_token: 'ACCESS TOKEN HERE',
  refresh_token: 'REFRESH TOKEN HERE'
 });
 plus.people.get({ userId: 'me', auth: oauth2Client }, function(err, response) {
  // handle err and response
 });

The complete login authorization example. (link: https://github.com/google/google-api-nodejs-client/blob/master/examples/oauth2.js)

Example 3: file upload


 var fs = require('fs');
 var drive = google.drive({ version: 'v2', auth: oauth2Client });
 drive.files.insert({
  resource: {
  title: 'testimage.png',
  mimeType: 'image/png'
  },
  media: {
  mimeType: 'image/png',
  body: fs.createReadStream('awesome.png') // read streams are awesome!
  }
 }, callback);

Question answer ?

If you have any question to question (link: http://stackoverflow.com/questions/tagged/google-api-nodejs-client)

If you find bugs can be submitted on to making (link: https://github.com/google/google-api-nodejs-client/issues)


Related articles: