Skip to content

Commit 1821f6f

Browse files
awfuchsasrivas
authored andcommitted
Create output-json.py (googleworkspace#98)
Sample code that outputs the contents of a document as JSON
1 parent f63c501 commit 1821f6f

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

docs/output-json/output-json.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from __future__ import print_function
2+
from apiclient import discovery
3+
from httplib2 import Http
4+
from oauth2client import client
5+
from oauth2client import file
6+
from oauth2client import tools
7+
import json
8+
9+
# Set doc ID, as found at `https://docs.google.com/document/d/YOUR_DOC_ID/edit`
10+
DOCUMENT_ID=YOUR_DOC_ID
11+
12+
# Set the scopes and discovery info
13+
SCOPES = 'https://www.googleapis.com/auth/documents.readonly'
14+
DISCOVERY_DOC = 'https://docs.googleapis.com/$discovery/rest?version=v1&key=<YOUR_API_KEY>'
15+
16+
# Initialize credentials and instantiate Docs API service
17+
store = file.Storage('token.json')
18+
creds = store.get()
19+
if not creds or creds.invalid:
20+
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
21+
creds = tools.run_flow(flow, store)
22+
service = discovery.build('docs', 'v1', http=creds.authorize(
23+
Http()), discoveryServiceUrl=DISCOVERY_DOC)
24+
25+
# Do a document "get" request and print the results as formatted JSON
26+
result = service.documents().get(documentId=DOCUMENT_ID).execute()
27+
print(json.dumps(result, indent=4, sort_keys=True))

0 commit comments

Comments
 (0)