Google officially supported the Nodejs Access API, providing background login authorization _node.js

Source: Internet
Author: User
Tags auth

Installation

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

$ NPM Install Googleapis

Complete API Support List Https://developers.google.com/apis-explorer

Use

Example 1: Get full address via 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 u RL is ', Response.longurl;
 });

Example 2: Login authorization

This sample integrates OAUTH2 authentication, which allows you to gain access to the user token and refresh the token to prevent the session from expiring.

 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: ' A ccess TOKEN here ',
  refresh_token: ' Refresh TOKEN here '
 };
 Plus.people.get ({userId: ' Me ', auth:oauth2client}, function (err, response) {
  //Handle ERR and Response
 });

An example of a complete login authorization. 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 questions, ask the StackOverflow.

If a vulnerability is found, it can be submitted to GitHub Issue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.