Skip to content

Commit c98e055

Browse files
Merge branch 'master' into pr/61
2 parents 88f2088 + b254418 commit c98e055

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+1111
-203
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ client_secret.json
33
application_credentials.json
44
storage.json
55
credentials.json
6-
token.json
6+
token.pickle
77

88
.DS_Store
99

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ Python samples for [G Suite API](https://developers.google.com/gsuite/) docs.
2222

2323
- [Quickstart](https://developers.google.com/classroom/quickstart/python)
2424

25+
### Docs
26+
27+
- [Quickstart](https://developers.google.com/docs/api/quickstart/python)
28+
- [Output Doc as JSON](https://developers.google.com/docs/api/samples/output-json):
29+
Dump the contents of a Google Doc as formatted JSON
30+
- [Extract text](https://developers.google.com/docs/api/samples/extract-text):
31+
Extract only the text of a Google Doc
32+
- [Mail merge](https://developers.google.com/docs/api/samples/mail-merge):
33+
Perform mail merges from plain text or Google Sheets data sources
34+
2535
### Drive V3
2636

2737
- [Quickstart](https://developers.google.com/drive/v3/web/quickstart/python)

admin_sdk/directory/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Directory API.
88
## Install
99

1010
```
11-
pip install --upgrade google-api-python-client oauth2client
11+
pip install -r requirements.txt
1212
```
1313

1414
## Run

admin_sdk/directory/quickstart.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,39 @@
1414

1515
# [START admin_sdk_directory_quickstart]
1616
from __future__ import print_function
17+
import pickle
18+
import os.path
1719
from googleapiclient.discovery import build
18-
from httplib2 import Http
19-
from oauth2client import file, client, tools
20+
from google_auth_oauthlib.flow import InstalledAppFlow
21+
from google.auth.transport.requests import Request
2022

21-
# If modifying these scopes, delete the file token.json.
22-
SCOPES = 'https://www.googleapis.com/auth/admin.directory.user'
23+
# If modifying these scopes, delete the file token.pickle.
24+
SCOPES = ['https://www.googleapis.com/auth/admin.directory.user']
2325

2426
def main():
2527
"""Shows basic usage of the Admin SDK Directory API.
2628
Prints the emails and names of the first 10 users in the domain.
2729
"""
28-
# The file token.json stores the user's access and refresh tokens, and is
30+
creds = None
31+
# The file token.pickle stores the user's access and refresh tokens, and is
2932
# created automatically when the authorization flow completes for the first
3033
# time.
31-
store = file.Storage('token.json')
32-
creds = store.get()
33-
if not creds or creds.invalid:
34-
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
35-
creds = tools.run_flow(flow, store)
36-
service = build('admin', 'directory_v1', http=creds.authorize(Http()))
34+
if os.path.exists('token.pickle'):
35+
with open('token.pickle', 'rb') as token:
36+
creds = pickle.load(token)
37+
# If there are no (valid) credentials available, let the user log in.
38+
if not creds or not creds.valid:
39+
if creds and creds.expired and creds.refresh_token:
40+
creds.refresh(Request())
41+
else:
42+
flow = InstalledAppFlow.from_client_secrets_file(
43+
'credentials.json', SCOPES)
44+
creds = flow.run_local_server(port=0)
45+
# Save the credentials for the next run
46+
with open('token.pickle', 'wb') as token:
47+
pickle.dump(creds, token)
48+
49+
service = build('admin', 'directory_v1', credentials=creds)
3750

3851
# Call the Admin SDK Directory API
3952
print('Getting the first 10 users in the domain')
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.9
2+
google-auth-httplib2==0.0.3
3+
google-auth-oauthlib==0.4.0

admin_sdk/reports/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ that makes requests to the Google Admin SDK Reports API.
88
## Install
99

1010
```
11-
pip install --upgrade google-api-python-client oauth2client
11+
pip install -r requirements.txt
1212
```
1313

1414
## Run

admin_sdk/reports/quickstart.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,39 @@
1414

1515
# [START admin_sdk_reports_quickstart]
1616
from __future__ import print_function
17+
import pickle
18+
import os.path
1719
from googleapiclient.discovery import build
18-
from httplib2 import Http
19-
from oauth2client import file, client, tools
20+
from google_auth_oauthlib.flow import InstalledAppFlow
21+
from google.auth.transport.requests import Request
2022

21-
# If modifying these scopes, delete the file token.json.
22-
SCOPES = 'https://www.googleapis.com/auth/admin.reports.audit.readonly'
23+
# If modifying these scopes, delete the file token.pickle.
24+
SCOPES = ['https://www.googleapis.com/auth/admin.reports.audit.readonly']
2325

2426
def main():
2527
"""Shows basic usage of the Admin SDK Reports API.
2628
Prints the time, email, and name of the last 10 login events in the domain.
2729
"""
28-
# The file token.json stores the user's access and refresh tokens, and is
30+
creds = None
31+
# The file token.pickle stores the user's access and refresh tokens, and is
2932
# created automatically when the authorization flow completes for the first
3033
# time.
31-
store = file.Storage('token.json')
32-
creds = store.get()
33-
if not creds or creds.invalid:
34-
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
35-
creds = tools.run_flow(flow, store)
36-
service = build('admin', 'reports_v1', http=creds.authorize(Http()))
34+
if os.path.exists('token.pickle'):
35+
with open('token.pickle', 'rb') as token:
36+
creds = pickle.load(token)
37+
# If there are no (valid) credentials available, let the user log in.
38+
if not creds or not creds.valid:
39+
if creds and creds.expired and creds.refresh_token:
40+
creds.refresh(Request())
41+
else:
42+
flow = InstalledAppFlow.from_client_secrets_file(
43+
'credentials.json', SCOPES)
44+
creds = flow.run_local_server(port=0)
45+
# Save the credentials for the next run
46+
with open('token.pickle', 'wb') as token:
47+
pickle.dump(creds, token)
48+
49+
service = build('admin', 'reports_v1', credentials=creds)
3750

3851
# Call the Admin SDK Reports API
3952
print('Getting the last 10 login events')

admin_sdk/reports/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.9
2+
google-auth-httplib2==0.0.3
3+
google-auth-oauthlib==0.4.0

admin_sdk/reseller/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ that makes requests to the Google Admin SDK Reseller API.
88
## Install
99

1010
```
11-
pip install --upgrade google-api-python-client oauth2client
11+
pip install -r requirements.txt
1212
```
1313

1414
## Run

admin_sdk/reseller/quickstart.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,39 @@
1414

1515
# [START admin_sdk_reseller_quickstart]
1616
from __future__ import print_function
17+
import pickle
18+
import os.path
1719
from googleapiclient.discovery import build
18-
from httplib2 import Http
19-
from oauth2client import file, client, tools
20+
from google_auth_oauthlib.flow import InstalledAppFlow
21+
from google.auth.transport.requests import Request
2022

21-
# If modifying these scopes, delete the file token.json.
22-
SCOPES = 'https://www.googleapis.com/auth/apps.order'
23+
# If modifying these scopes, delete the file token.pickle.
24+
SCOPES = ['https://www.googleapis.com/auth/apps.order']
2325

2426
def main():
2527
"""Calls the Admin SDK Reseller API. Prints the customer ID, SKU ID,
2628
and plan name of the first 10 subscriptions managed by the domain.
2729
"""
28-
# The file token.json stores the user's access and refresh tokens, and is
30+
creds = None
31+
# The file token.pickle stores the user's access and refresh tokens, and is
2932
# created automatically when the authorization flow completes for the first
3033
# time.
31-
store = file.Storage('token.json')
32-
creds = store.get()
33-
if not creds or creds.invalid:
34-
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
35-
creds = tools.run_flow(flow, store)
36-
service = build('reseller', 'v1', http=creds.authorize(Http()))
34+
if os.path.exists('token.pickle'):
35+
with open('token.pickle', 'rb') as token:
36+
creds = pickle.load(token)
37+
# If there are no (valid) credentials available, let the user log in.
38+
if not creds or not creds.valid:
39+
if creds and creds.expired and creds.refresh_token:
40+
creds.refresh(Request())
41+
else:
42+
flow = InstalledAppFlow.from_client_secrets_file(
43+
'credentials.json', SCOPES)
44+
creds = flow.run_local_server(port=0)
45+
# Save the credentials for the next run
46+
with open('token.pickle', 'wb') as token:
47+
pickle.dump(creds, token)
48+
49+
service = build('reseller', 'v1', credentials=creds)
3750

3851
# Call the Admin SDK Reseller API
3952
print('Getting the first 10 subscriptions')

0 commit comments

Comments
 (0)