Skip to content

Commit 0a5296c

Browse files
asrivasgrant
authored andcommitted
Add Docs quickstart
1 parent 4df1a2d commit 0a5296c

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

docs/quickstart/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Google Docs Python Quickstart
2+
3+
Complete the steps described in the [quickstart instructions](
4+
https://developers.google.com/docs/api/quickstart/python), and in about five
5+
minutes you'll have a simple Python command-line application that makes
6+
requests to the Google Docs API.
7+
8+
## Install
9+
10+
```
11+
pip install -r requirements.txt
12+
```
13+
14+
## Run
15+
16+
After following the quickstart setup instructions, run the sample:
17+
18+
```
19+
python quickstart.py
20+
```

docs/quickstart/quickstart.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Copyright 2018 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START docs_quickstart]
16+
from __future__ import print_function
17+
import pickle
18+
import os.path
19+
from googleapiclient.discovery import build
20+
from google_auth_oauthlib.flow import InstalledAppFlow
21+
from google.auth.transport.requests import Request
22+
23+
# If modifying these scopes, delete the file token.pickle.
24+
SCOPES = ['https://www.googleapis.com/auth/documents.readonly']
25+
26+
# The ID of a sample document.
27+
DOCUMENT_ID = '195j9eDD3ccgjQRttHhJPymLJUCOUjs-jmwTrekvdjFE'
28+
29+
def main():
30+
"""Shows basic usage of the Docs API.
31+
Prints the title of a sample document.
32+
"""
33+
creds = None
34+
# The file token.pickle stores the user's access and refresh tokens, and is
35+
# created automatically when the authorization flow completes for the first
36+
# time.
37+
if os.path.exists('token.pickle'):
38+
with open('token.pickle', 'rb') as token:
39+
creds = pickle.load(token)
40+
# If there are no (valid) credentials available, let the user log in.
41+
if not creds or not creds.valid:
42+
if creds and creds.expired and creds.refresh_token:
43+
creds.refresh(Request())
44+
else:
45+
flow = InstalledAppFlow.from_client_secrets_file(
46+
'credentials.json', SCOPES)
47+
creds = flow.run_local_server()
48+
# Save the credentials for the next run
49+
with open('token.pickle', 'wb') as token:
50+
pickle.dump(creds, token)
51+
52+
service = build('docs', 'v1', credentials=creds)
53+
54+
# Retrieve the documents contents from the Docs service.
55+
document = service.documents().get(documentId=DOCUMENT_ID).execute()
56+
57+
print('The title of the document is: {}'.format(document.get('title')))
58+
59+
60+
if __name__ == '__main__':
61+
main()
62+
# [END docs_quickstart]

docs/quickstart/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
google-api-python-client==1.7.8
2+
google-auth-httplib2==0.0.3
3+
google-auth-oauthlib==0.2.0

0 commit comments

Comments
 (0)