Skip to content

Commit 192ee1b

Browse files
author
Eric Koleda
authored
Merge pull request googleworkspace#60 from slokhorst/update-auth
[WIP] Switch sheets/quickstart to from oauth2client to google-auth
2 parents c22a9b1 + f390f87 commit 192ee1b

File tree

28 files changed

+358
-163
lines changed

28 files changed

+358
-163
lines changed

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 --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib pickle
1212
```
1313

1414
## Run

admin_sdk/directory/quickstart.py

Lines changed: 23 additions & 10 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.
23+
# If modifying these scopes, delete the file token.pickle.
2224
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()
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')

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 --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib pickle
1212
```
1313

1414
## Run

admin_sdk/reports/quickstart.py

Lines changed: 23 additions & 10 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.
23+
# If modifying these scopes, delete the file token.pickle.
2224
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()
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/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 --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib pickle
1212
```
1313

1414
## Run

admin_sdk/reseller/quickstart.py

Lines changed: 23 additions & 10 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.
23+
# If modifying these scopes, delete the file token.pickle.
2224
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()
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')

apps_script/quickstart/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 Apps Script API.
88
## Install
99

1010
```
11-
pip install --upgrade google-api-python-client oauth2client
11+
pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib pickle
1212
```
1313

1414
## Run

apps_script/quickstart/quickstart.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
project, and log the script's URL to the user.
2020
"""
2121
from __future__ import print_function
22+
import pickle
23+
import os.path
2224
from googleapiclient import errors
2325
from googleapiclient.discovery import build
24-
from httplib2 import Http
25-
from oauth2client import file, client, tools
26+
from google_auth_oauthlib.flow import InstalledAppFlow
27+
from google.auth.transport.requests import Request
2628

27-
# If modifying these scopes, delete the file token.json.
29+
# If modifying these scopes, delete the file token.pickle.
2830
SCOPES = 'https://www.googleapis.com/auth/script.projects'
2931

3032
SAMPLE_CODE = '''
@@ -43,15 +45,26 @@
4345
def main():
4446
"""Calls the Apps Script API.
4547
"""
46-
# The file token.json stores the user's access and refresh tokens, and is
48+
creds = None
49+
# The file token.pickle stores the user's access and refresh tokens, and is
4750
# created automatically when the authorization flow completes for the first
4851
# time.
49-
store = file.Storage('token.json')
50-
creds = store.get()
51-
if not creds or creds.invalid:
52-
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
53-
creds = tools.run_flow(flow, store)
54-
service = build('script', 'v1', http=creds.authorize(Http()))
52+
if os.path.exists('token.pickle'):
53+
with open('token.pickle', 'rb') as token:
54+
creds = pickle.load(token)
55+
# If there are no (valid) credentials available, let the user log in.
56+
if not creds or not creds.valid:
57+
if creds and creds.expired and creds.refresh_token:
58+
creds.refresh(Request())
59+
else:
60+
flow = InstalledAppFlow.from_client_secrets_file(
61+
'credentials.json', SCOPES)
62+
creds = flow.run_local_server()
63+
# Save the credentials for the next run
64+
with open('token.pickle', 'wb') as token:
65+
pickle.dump(creds, token)
66+
67+
service = build('script', 'v1', credentials=creds)
5568

5669
# Call the Apps Script API
5770
try:

calendar/quickstart/README.md

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

1010
```
11-
pip install --upgrade google-api-python-client oauth2client
11+
pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib pickle
1212
```
1313

1414
## Run

calendar/quickstart/quickstart.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,39 @@
1515
# [START calendar_quickstart]
1616
from __future__ import print_function
1717
import datetime
18+
import pickle
19+
import os.path
1820
from googleapiclient.discovery import build
19-
from httplib2 import Http
20-
from oauth2client import file, client, tools
21+
from google_auth_oauthlib.flow import InstalledAppFlow
22+
from google.auth.transport.requests import Request
2123

22-
# If modifying these scopes, delete the file token.json.
24+
# If modifying these scopes, delete the file token.pickle.
2325
SCOPES = 'https://www.googleapis.com/auth/calendar.readonly'
2426

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

3952
# Call the Calendar API
4053
now = datetime.datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time

0 commit comments

Comments
 (0)