NodeJS access API officially supported by Google, which provides background logon authorization and nodejsapi

Source: Internet
Author: User

NodeJS access API officially supported by Google, which provides background logon authorization and nodejsapi

Install

This library is released through npm. Run the following command to install googleapis and its dependencies:

$ npm install googleapis

Complete list of API support https://developers.google.com/apis-explorer

Use

Example 1: Get the complete address through 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: logon authorization

This example is integrated with oau22. you can obtain the user's access Token and refresh 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 });

Complete logon authorization example. 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?

If you have any questions, you can ask Stackoverflow

If any vulnerability is found, submit the Issue on GitHub.


How does nodejs access external interfaces?

One is premade packages that are referenced by require, such as fs;
Also, access through the network, such as accessing restful APIs through http.
 
How does js use JS to simulate form submission/login operations on a website of another user? There is another nodejs issue.

1. First you have to use js, then use iframe to load the original webpage, and then call js
2. js is simple. It is written on a webpage and loaded with winform. nodejs is complicated. First, you need to ask the user to install nodejs on windows (I have not tried it) and then call the nodejs environment, nodejs is used as a server in linux and does not work with windows programs.

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.