<commit_before># Google APIs NodeJS Client [![Build Status][travisimg]][travis]

`google-api-nodejs-client` is Google's officially supported
[node.js][node] client library for using Google APIs.
It also supports authorization and authentication with OAuth 2.0.

### Questions/problems?

* Ask your development related questions on [![Ask a question on Stackoverflow](https://googledrive.com/host/0ByfSjdPVs9MZbkhjeUhMYzRTeEE/stackoveflow-tag.png)][stackoverflow]
* If you found a bug, please [file a bug][bugs].

## Installation

The library is distributed on `npm`. In order to add it as a dependency,
run the following command:

~~~~ sh
$ npm install googleapis
~~~~

## Usage

### Making requests

``` js
var google = require('googleapis');

var urlshortener = google.urlshortener('v1');
var plus = google.plus('v1');

var params = { shortUrl: 'http://goo.gl/DdUKX' };

urlshortener.url.get(params, function (err, response) {
  console.log('Long url is', response.longUrl);
});

plus.people.get({ userId: '+burcudogan' }, function(err, user) {
  console.log('User id is: ' + user.id);
});
```

Supported APIs are listed on
[Google APIs Explorer][apiexplorer].

### Authorization and Authentication

This client comes with an OAuth2 client that allows you to retrieve an access token and
refreshes the token and re-try the request seamlessly if token is expired. The
basics of Google's OAuth 2.0 implementation is explained on
[Google Authorization and Authentication
documentation](https://developers.google.com/accounts/docs/OAuth2Login).

A complete sample application that authorizes and authenticates with OAuth2.0
client is available at `examples/oauth2.js`.

#### Consent Page Url

To ask for permissions from a user to retrieve an access token, you
redirect them to a consent page. To create a consent page URL:

``` js
var google = require('googleapis'),
    OAuth2 = google.auth.OAuth2;

var oauth2Client = new OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL);

// generates a url that allows offline access and asks permissions
// for Google+ scope.
var scopes = [
  'https://www.googleapis.com/auth/plus.me',
  'https://www.googleapis.com/auth/calendar'
];

var url = oauth2Client.generateAuthUrl({
  access_type: 'offline',
  scope: scopes.join(' ') // space delimited string of scopes
});
```

#### Retrieving Tokens

Once a user has given permissions on the consent page, Google will redirect
the page to the redirect URL you have provided with a code query parameter.

    GET /oauthcallback?code={authorizationCode}

With the code returned, you can ask for an access token as shown below:

``` js
oauth2Client.getToken(code, function(err, tokens) {
  // contains an access_token and optionally a refresh_token.
  // save them permanently.
});
```

### API Client

API Client endpoints are available for many APIs. Below is an
example of loading a client for [URL Shortener API][urlshort].

``` js
var urlshortener = google.urlshortener('v1'); // load version 'v1' of urlshortener
```

### Requests

The following sample loads a client for URL Shortener and retrieves the long url
of the given short url:

``` js
var urlshortener = google.urlshortener('v1');

urlshortener.url.get({ shortUrl: 'http://goo.gl/DdUKX' }, function(err, result) {<commit_msg>Update documentation around using auth<commit_after># Google APIs NodeJS Client [![Build Status][travisimg]][travis]

`google-api-nodejs-client` is Google's officially supported
[node.js][node] client library for using Google APIs.
It also supports authorization and authentication with OAuth 2.0.

### Questions/problems?

* Ask your development related questions on [![Ask a question on Stackoverflow](https://googledrive.com/host/0ByfSjdPVs9MZbkhjeUhMYzRTeEE/stackoveflow-tag.png)][stackoverflow]
* If you found a bug, please [file a bug][bugs].

## Installation

The library is distributed on `npm`. In order to add it as a dependency,
run the following command:

~~~~ sh
$ npm install googleapis
~~~~

## Usage

``` js
var google = require('googleapis');

var urlshortener = google.urlshortener('v1');
var plus = google.plus('v1');

var params = { shortUrl: 'http://goo.gl/DdUKX' };

urlshortener.url.get(params, function (err, response) {
  console.log('Long url is', response.longUrl);
});

plus.people.get({ userId: '+burcudogan' }, function(err, user) {
  console.log('User id is: ' + user.id);
});
```

Supported APIs are listed on
[Google APIs Explorer][apiexplorer].

### Authorization and Authentication

This client comes with an OAuth2 client that allows you to retrieve an access token and
refreshes the token and re-try the request seamlessly if token is expired. The
basics of Google's OAuth 2.0 implementation is explained on
[Google Authorization and Authentication
documentation](https://developers.google.com/accounts/docs/OAuth2Login).

A complete sample application that authorizes and authenticates with the OAuth 2.0
client is available at `examples/oauth2.js`.

#### Consent Page Url

To ask for permissions from a user to retrieve an access token, you
redirect them to a consent page. To create a consent page URL:

``` js
var google = require('googleapis'),
    OAuth2 = google.auth.OAuth2;

var oauth2Client = new OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL);

// generates a url that allows offline access and asks permissions
// for Google+ scope.
var scopes = [
  'https://www.googleapis.com/auth/plus.me',
  'https://www.googleapis.com/auth/calendar'
];

var url = oauth2Client.generateAuthUrl({
  access_type: 'offline',
  scope: scopes.join(' ') // space delimited string of scopes
});
```

#### Retrieving Tokens

Once a user has given permissions on the consent page, Google will redirect
the page to the redirect URL you have provided with a code query parameter.

    GET /oauthcallback?code={authorizationCode}

With the code returned, you can ask for an access token as shown below:

``` js
oauth2Client.getToken(code, function(err, tokens) {
  // Now tokens contains an access_token and an optional refresh_token. Save them.
  if(!err) oauth2Client.setCredentials(tokens);
});
```

### API Client

API Client endpoints are available for many APIs. Below is an
example of loading a client for [URL Shortener API][urlshort].

Just specify the version as a string to load that particular client.
Here we load version `v1` of the `urlshortener` client.
``` js
var urlshortener = google.urlshortener('v1');
```

You can also specify additional options when loading a specific client endpoint. For example,
to specify an default `auth` option (API key or oauth2Client), simply pass it in like so:
``` js
var urlshortener = google.urlshortener({ version: 'v1', auth: 'API KEY' || oauth2Client });
```

### Requests

The following sample loads a client for URL Shortener and retrieves the long url
of the given short url:

``` js
var urlshortener = google.urlshortener('v1');

urlshortener.url.get({ shortUrl: 'http://goo.gl/DdUKX' }, function(err, result) {
{'fraction_non_alphanumeric': 0.10941987363584146, 'fraction_numerical': 0.0068925904652498565, 'mean_word_length': 4.7514450867052025, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 8, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6827748', 'n_tokens_mistral': 2134, 'n_tokens_neox': 2052, 'n_words': 789}