Skip to content

Commit 5c11b0a

Browse files
grantbusunkim96
authored andcommitted
docs: Add docs folder with guides from developers.google.com
1 parent 715bd7f commit 5c11b0a

18 files changed

+1690
-8
lines changed

README.md

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,38 @@ This is the Python client library for Google's discovery based APIs. To get star
88

99
These client libraries are officially supported by Google. However, the libraries are considered complete and are in maintenance mode. This means that we will address critical bugs and security issues but will not add any new features.
1010

11+
## Documentation
12+
13+
See the [docs folder](docs/README.md) for more detailed instructions and additional documentation.
14+
1115
## Google Cloud Platform
1216

1317
For Google Cloud Platform APIs such as Datastore, Cloud Storage or Pub/Sub, we recommend using [Cloud Client Libraries for Python](https://github.com/GoogleCloudPlatform/google-cloud-python) which is under active development.
1418

15-
# Installation
19+
## Installation
20+
1621
To install, simply use `pip` or `easy_install`:
1722

1823
```bash
19-
$ pip install --upgrade google-api-python-client
24+
pip install --upgrade google-api-python-client
2025
```
26+
2127
or
28+
2229
```bash
23-
$ easy_install --upgrade google-api-python-client
30+
easy_install --upgrade google-api-python-client
2431
```
2532

26-
See the [Developers Guide](https://developers.google.com/api-client-library/python/start/get_started) for more detailed instructions and additional documentation.
33+
## Supported Python Versions
2734

28-
# Supported Python Versions
2935
Python 3.4, 3.5, 3.6 and 3.7 are fully supported and tested. This library may work on later versions of 3, but we do not currently run tests against those versions
3036

31-
# Deprecated Python Versions
37+
## Deprecated Python Versions
38+
3239
Python == 2.7
3340

34-
# Third Party Libraries and Dependencies
41+
## Third Party Libraries and Dependencies
42+
3543
The following libraries will be installed when you install the client library:
3644
* [httplib2](https://github.com/httplib2/httplib2)
3745
* [uritemplate](https://github.com/sigmavirus24/uritemplate)
@@ -41,5 +49,6 @@ For development you will also need the following libraries:
4149
* [pycrypto](https://pypi.python.org/pypi/pycrypto)
4250
* [pyopenssl](https://pypi.python.org/pypi/pyOpenSSL)
4351

44-
# Contributing
52+
## Contributing
53+
4554
Please see the [contributing page](http://google.github.io/google-api-python-client/contributing.html) for more information. In particular, we love pull requests - but please make sure to sign the contributor license agreement.

docs/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Google API Client LIbrary for Python Docs
2+
3+
The Google API Client Library for Python is designed for Python
4+
client-application developers. It offers simple, flexible
5+
access to many Google APIs.
6+
7+
## Features
8+
9+
- Call Google APIs simply
10+
- Use the library with Google App Engine
11+
- Handle Auath with fewer lines of code
12+
- Use standard tooling for installation
13+
14+
## Documentation
15+
16+
Learn how to use the Google API Python Client with these guides:
17+
18+
- [Getting Started](start.md)
19+
- [Auth](auth.md)
20+
- [API Keys](api-keys.md)
21+
- [OAuth 2.0](oauth.md)
22+
- [OAuth 2.0 for Web Server Applications](oauth-web.md)
23+
- [OAuth 2.0 for Installed Applications](oauth-installed.md)
24+
- [OAuth 2.0 for Server to Server Applications](oauth-server.md)
25+
- [Client Secrets](client-secrets.md)
26+
- How to...
27+
- [Use Logging](logging.md)
28+
- [Upload Media](media.md)
29+
- [Use Mocks](mocks.md)
30+
- [Use Pagination](pagination.md)
31+
- [Improve Performance](performance.md)
32+
- [Understand Thread Safety](thread_safety.md)
33+
- [Use Google App Engine](google_app_engine.md)
34+
- [Use Django](django.md)
35+

docs/api-keys.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# API Keys
2+
3+
When calling APIs that do not access private user data, you can use simple API keys. These keys are used to authenticate your application for accounting purposes. The Google Developers Console documentation also describes [API keys](https://developers.google.com/console/help/using-keys).
4+
5+
> Note: If you do need to access private user data, you must use OAuth 2.0. See [Using OAuth 2.0 for Web Server Applications](oauth-server.md) and [Using OAuth 2.0 for Server to Server Applications](oauth-web.md) for more information.
6+
7+
## Using API Keys
8+
9+
To use API keys, pass them to the `build()` function when creating service objects. For example:
10+
11+
```py
12+
books_service = build('books', 'v1', developerKey='api_key')
13+
```
14+
15+
All calls made using that service object will include your API key.

docs/auth.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Authentication Overview
2+
3+
This document is an overview of how authentication, authorization, and accounting are accomplished. For all API calls, your application needs to be authenticated. When an API accesses a user's private data, your application must also be authorized by the user to access the data. For example, accessing a public Google+ post would not require user authorization, but accessing a user's private calendar would. Also, for quota and billing purposes, all API calls involve accounting. This document summarizes the protocols used by Google APIs and provides links to more information.
4+
5+
## Access types
6+
7+
It is important to understand the basics of how API authentication and authorization are handled. All API calls must use either simple or authorized access (defined below). Many API methods require authorized access, but some can use either. Some API methods that can use either behave differently, depending on whether you use simple or authorized access. See the API's method documentation to determine the appropriate access type.
8+
9+
### 1. Simple API access (API keys)
10+
11+
These API calls do not access any private user data. Your application must authenticate itself as an application belonging to your Google API Console project. This is needed to measure project usage for accounting purposes.
12+
13+
**API key:** To authenticate your application, use an [API key](api-keys.md) for your API Console project. Every simple access call your application makes must include this key.
14+
15+
> **Warning:** Keep your API key private. If someone obtains your key, they could use it to consume your quota or incur charges against your API Console project.
16+
17+
### 2. Authorized API access (OAuth 2.0)
18+
19+
These API calls access private user data. Before you can call them, the user that has access to the private data must grant your application access. Therefore, your application must be authenticated, the user must grant access for your application, and the user must be authenticated in order to grant that access. All of this is accomplished with OAuth 2.0 and libraries written for it.
20+
21+
**Scope:** Each API defines one or more scopes that declare a set of operations permitted. For example, an API might have read-only and read-write scopes. When your application requests access to user data, the request must include one or more scopes. The user needs to approve the scope of access your application is requesting.
22+
23+
**Refresh and access tokens:** When a user grants your application access, the OAuth 2.0 authorization server provides your application with refresh and access tokens. These tokens are only valid for the scope requested. Your application uses access tokens to authorize API calls. Access tokens expire, but refresh tokens do not. Your application can use a refresh token to acquire a new access token.
24+
25+
> **Warning:** Keep refresh and access tokens private. If someone obtains your tokens, they could use them to access private user data.
26+
27+
**Client ID and client secret:** These strings uniquely identify your application and are used to acquire tokens. They are created for your project on the [API Console](https://console.developers.google.com/). There are three types of client IDs, so be sure to get the correct type for your application:
28+
29+
- [Web application](https://developers.google.com/accounts/docs/OAuth2WebServer) client IDs
30+
- [Installed application](https://developers.google.com/accounts/docs/OAuth2InstalledApp) client IDs
31+
- [Service Account](https://developers.google.com/accounts/docs/OAuth2ServiceAccount) client IDs
32+
33+
> **Warning:** Keep your client secret private. If someone obtains your client secret, they could use it to consume your quota, incur charges against your Console project, and request access to user data.
34+
35+
## Using API keys
36+
37+
More information and examples for API keys are provided on the [API Keys](api-keys.md) page.
38+
39+
## Using OAuth 2.0
40+
41+
More information and examples for OAuth 2.0 are provided on the [OAuth 2.0](oauth.md) page.

docs/client-secrets.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Client Secrets
2+
3+
The Google APIs Client Library for Python uses the `client_secrets.json` file format for storing the `client_id`, `client_secret`, and other OAuth 2.0 parameters.
4+
5+
The `client_secrets.json` file format is a [JSON](http://www.json.org/) formatted file containing the client ID, client secret, and other OAuth 2.0 parameters. Here is an example client_secrets.json file for a web application:
6+
7+
```json
8+
{
9+
"web": {
10+
"client_id": "asdfjasdljfasdkjf",
11+
"client_secret": "1912308409123890",
12+
"redirect_uris": ["https://www.example.com/oauth2callback"],
13+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
14+
"token_uri": "https://accounts.google.com/o/oauth2/token"
15+
}
16+
}
17+
```
18+
19+
Here is an example client_secrets.json file for an installed application:
20+
21+
```json
22+
{
23+
"installed": {
24+
"client_id": "837647042410-75ifg...usercontent.com",
25+
"client_secret":"asdlkfjaskd",
26+
"redirect_uris": ["http://localhost", "urn:ietf:wg:oauth:2.0:oob"],
27+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
28+
"token_uri": "https://accounts.google.com/o/oauth2/token"
29+
}
30+
}
31+
```
32+
33+
The format defines one of two client ID types:
34+
35+
- `web`: Web application.
36+
- `installed`: Installed application.
37+
38+
The `web` and `installed` sub-objects have the following mandatory members:
39+
40+
- `client_id` (string): The client ID.
41+
- `client_secret` (string): The client secret.
42+
- `redirect_uris` (list of strings): A list of valid redirection endpoint URIs. This list should match the list entered for the client ID on the [API Access pane](https://code.google.com/apis/console#:access) of the Google APIs Console.
43+
- `auth_uri` (string): The authorization server endpoint URI.
44+
- `token_uri` (string): The token server endpoint URI.
45+
46+
All of the above members are mandatory. The following optional parameters may appear:
47+
48+
- `client_email` (string) The service account email associated with the client.
49+
- `auth_provider_x509_cert_url` (string) The URL of the public x509 certificate, used to verify the signature on JWTs, such as ID tokens, signed by the authentication provider.
50+
- `client_x509_cert_url` (string) The URL of the public x509 certificate, used to verify JWTs signed by the client.
51+
52+
The following examples show how use a `client_secrets.json` file to create a `Flow` object in either an installed application or a web application:
53+
54+
### Installed App
55+
56+
```py
57+
from google_auth_oauthlib.flow import InstalledAppFlow
58+
...
59+
flow = InstalledAppFlow.from_client_secrets_file(
60+
'path_to_directory/client_secret.json',
61+
scopes=['https://www.googleapis.com/auth/calendar'])
62+
```
63+
64+
### Web Server App
65+
66+
```py
67+
import google.oauth2.credentials
68+
import google_auth_oauthlib.flow
69+
70+
flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
71+
'path_to_directory/client_secret.json',
72+
scopes=['https://www.googleapis.com/auth/calendar'])
73+
74+
flow.redirect_uri = 'https://www.example.com/oauth2callback'
75+
```
76+
77+
## Motivation
78+
79+
Traditionally providers of OAuth endpoints have relied upon cut-and-paste as the way users of their service move the client id and secret from a registration page into working code. That can be error prone, along with it being an incomplete picture of all the information that is needed to get OAuth 2.0 working, which requires knowing all the endpoints and configuring a Redirect Endpoint. If service providers start providing a downloadable client_secrets.json file for client information and client libraries start consuming client_secrets.json then a large amount of friction in implementing OAuth 2.0 can be reduced.
80+

docs/django.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Using Django
2+
3+
The Google APIs Client Library for Python has special support for the [Django](https://www.djangoproject.com/) web framework. In particular, there are classes that simplify the OAuth 2.0 protocol steps. This document describes the Django-specific classes available for working with [Flow](https://developers.google.com/api-client-library/python/guide/aaa_oauth#flows), [Credentials](https://developers.google.com/api-client-library/python/guide/aaa_oauth#credentials), and [Storage](https://developers.google.com/api-client-library/python/guide/aaa_oauth#storage) objects.
4+
5+
## Flows
6+
7+
Use the [oauth2client.contrib.django\_orm.FlowField](https://oauth2client.readthedocs.io/en/latest/source/oauth2client.contrib.django_orm.html#oauth2client.contrib.django_orm.FlowField) class as a Django model field so that `Flow` objects can easily be stored. When your application is simultaneously going through OAuth 2.0 steps for many users, it's normally best to store per-user `Flow` objects before the first redirection. This way, your redirection handlers can retrieve the `Flow` object already created for the user. In the following code, a model is defined that allows `Flow` objects to be stored and keyed by `User`:
8+
9+
```py
10+
from django.contrib.auth.models import User
11+
from django.db import models
12+
from oauth2client.contrib.django_orm import FlowField
13+
...
14+
class FlowModel(models.Model):
15+
id = models.ForeignKey(User, primary_key=True)
16+
flow = FlowField()
17+
```
18+
19+
## Credentials
20+
21+
Use the [oauth2client.contrib.django\_orm.CredentialsField](https://oauth2client.readthedocs.io/en/latest/source/oauth2client.contrib.django_orm.html#oauth2client.contrib.django_orm.CredentialsField) class as a Django model field so that `Credentials` objects can easily be stored. Similar to `Flow` objects, it's normally best to store per-user `Credentials` objects. In the following code, a model is defined that allows `Credentials` objects to be stored and keyed by `User`:
22+
23+
```py
24+
from django.contrib.auth.models import User
25+
from django.db import models
26+
from oauth2client.contrib.django_orm import CredentialsField
27+
...
28+
class CredentialsModel(models.Model):
29+
id = models.ForeignKey(User, primary_key=True)
30+
credential = CredentialsField()
31+
```
32+
33+
## Storage
34+
35+
Use the [oauth2client.contrib.django\_orm.Storage](https://oauth2client.readthedocs.io/en/latest/source/oauth2client.contrib.django_orm.html#oauth2client.contrib.django_orm.Storage) class to store and retrieve `Credentials` objects using a model defined with a `CredentialsField` object. You pass the model, field name for the model key, value for the model key, and field name to the `CredentialsField` constructor. The following shows how to create, read, and write `Credentials` objects using the example `CredentialsModel` class above:
36+
37+
```py
38+
from django.contrib.auth.models import User
39+
from oauth2client.contrib.django_orm import Storage
40+
from your_project.your_app.models import CredentialsModel
41+
...
42+
user = # A User object usually obtained from request.
43+
storage = Storage(CredentialsModel, 'id', user, 'credential')
44+
credential = storage.get()
45+
...
46+
storage.put(credential)
47+
```

0 commit comments

Comments
 (0)