9月 09, 2019 Google OAuth
Some time you may want to access to user information from their google account (e.g. Calendar, Email, etc... ). To do so, you first have to create a project on Google API Console and setup OAuth settings.

### Create a new project for you application

Create a test project on Google Cloud Platform (Google API Console)

![](https://cdn.19site.net/files/27/99/2799a218-0099-4826-923c-c0d62c6c55a9.png)

### Setup OAuth 2.0 authorize page

Select API and Service

![](https://cdn.19site.net/files/38/e8/38e856fb-4bb2-4437-b1b8-77ecda5107f4.png)

Fill your application name

![](https://cdn.19site.net/files/32/48/3248ecc2-c3f4-4fe1-8630-843799ba8e53.png)

Fill in you direct URL. (OAuth2.0 need a URL to redirect back to your site form passing the token or code)

![](https://cdn.19site.net/files/6f/2e/6f2ee76a-2ac3-4232-8eb8-bb5d37ffc310.png)

### Setup OAuth 2.0 application client

You have to create an OAuth 2.0 client in order to make it work.

![](https://cdn.19site.net/files/b0/97/b097fd6f-df48-47db-ba6a-b87a5670d07f.png)

Select application type and fill your web site url. (redirect url)

![](https://cdn.19site.net/files/bc/46/bc463e32-26f4-4c58-a731-32bc0ae903e6.png)

Then you will receive a OAuth2.0 client id and secret key.

![](https://cdn.19site.net/files/29/74/2974ab35-50ce-4e6a-9792-1c6a8f5f8c73.png)

That's all of Google part setting.

### Your application

Reference URL : https://developers.google.com/identity/protocols/OAuth2WebServer

In you application, application may generate a URL to Google OAuth 2.0 server and redirect the user there.

Code below is a example in NodeJs.

```js
var query = Qs.stringify({

	scope: 'https://www.googleapis.com/auth/calendar',

	access_type: 'offline',

	include_granted_scopes: true,

	state: 'state_parameter_passthrough_value',

	redirect_uri: 'https://19site.net',

	response_type: 'code',

	client_id: 
});

var url = 'https://accounts.google.com/o/oauth2/v2/auth?' + query;
```

The URL will prompt user to select their Google account to continue.

![](https://cdn.19site.net/files/0e/59/0e5932dc-c746-47ed-b32e-a360da6f7baf.png)

Ask you user to grant the following permission to your application.

![](https://cdn.19site.net/files/02/e3/02e3f15f-0cac-4519-85a4-126ade1c81f2.png)

Confirm user action.

![](https://cdn.19site.net/files/b8/13/b81398b7-9650-4b0f-86df-68efd904cdba.png)

Then user will redirect to the URL that you set (https://19site.com).

Finish : )
過去文章
2025 (9)
4 (5)
3 (1)
2 (3)
2024 (25)
11 (3)
10 (3)
9 (1)
3 (18)
2022 (6)
10 (1)
6 (2)
5 (1)
3 (1)
1 (1)
2021 (21)
11 (7)
7 (1)
6 (2)
5 (2)
4 (6)
3 (2)
2 (1)
2020 (92)
12 (1)
11 (2)
10 (4)
9 (10)
8 (5)
7 (1)
6 (3)
5 (1)
4 (4)
3 (25)
2 (7)
1 (29)
2019 (57)
12 (25)
11 (7)
9 (25)