1# Using OAuth 2.0 for Web Server Applications
2
3This document explains how web server applications use the Google API Client Library for Python to implement OAuth 2.0 authorization to access Google APIs. OAuth 2.0 allows users to share specific data with an application while keeping their usernames, passwords, and other information private. For example, an application can use OAuth 2.0 to obtain permission from users to store files in their Google Drives.
4
5This OAuth 2.0 flow is specifically for user authorization. It is designed for applications that can store confidential information and maintain state. A properly authorized web server application can access an API while the user interacts with the application or after the user has left the application.
6
7Web server applications frequently also use [service accounts](service-accounts.md) to authorize API requests, particularly when calling Cloud APIs to access project-based data rather than user-specific data. Web server applications can use service accounts in conjunction with user authorization.
8
9## Prerequisites
10
11### Enable APIs for your project
12
13Any application that calls Google APIs needs to enable those APIs in the API Console. To enable the appropriate APIs for your project:
14
151. Open the [Library](https://console.developers.google.com/apis/library) page in the API Console.
161. Select the project associated with your application. Create a project if you do not have one already.
171. Use the **Library** page to find each API that your application will use. Click on each API and enable it for your project.
18
19### Create authorization credentials
20
21Any application that uses OAuth 2.0 to access Google APIs must have authorization credentials that identify the application to Google's OAuth 2.0 server. The following steps explain how to create credentials for your project. Your applications can then use the credentials to access APIs that you have enabled for that project.
22
23<ol>
24  <li>Open the <a href="https://console.developers.google.com/apis/credentials">Credentials page</a> in the API Console.</li>
25
26  <li>Click <b>Create credentials &gt; OAuth client ID</b>.</li>
27  <li>Complete the form. Set the application type to <code>Web
28      application</code>. Applications that use languages and frameworks
29      like PHP, Java, Python, Ruby, and .NET must specify authorized
30      <b>redirect URIs</b>. The redirect URIs are the endpoints to which the
31      OAuth 2.0 server can send responses.<br><br>
32      For testing, you can specify URIs that refer to the local machine,
33      such as <code>http://localhost:8080</code>. With that in mind, please
34      note that all of the examples in this document use
35      <code>http://localhost:8080</code> as the redirect URI.
36      <br><br>
37      We recommend that you <a href="#protectauthcode">design your app's auth
38      endpoints</a> so that your application does not expose authorization
39      codes to other resources on the page.</li>
40</ol>
41
42After creating your credentials, download the **client_secret.json** file from the API Console. Securely store the file in a location that only your application can access.
43
44> **Important:** Do not store the **client_secret.json** file in a publicly-accessible location. In addition, if you share the source code to your application—for example, on GitHub—store the **client_secret.json** file outside of your source tree to avoid inadvertently sharing your client credentials.
45
46### Identify access scopes
47
48Scopes enable your application to only request access to the resources that it needs while also enabling users to control the amount of access that they grant to your application. Thus, there may be an inverse relationship between the number of scopes requested and the likelihood of obtaining user consent.
49
50Before you start implementing OAuth 2.0 authorization, we recommend that you identify the scopes that your app will need permission to access.
51
52We also recommend that your application request access to authorization scopes via an incremental authorization process, in which your application requests access to user data in context. This best practice helps users to more easily understand why your application needs the access it is requesting.
53
54The [OAuth 2.0 API Scopes document](https://developers.google.com/identity/protocols/googlescopes) contains a full list of scopes that you might use to access Google APIs.