Skip to content

Commit 43fc5bc

Browse files
authored
Merge pull request googleworkspace#189 from nickmooney/master
Use the JSON serialization of Credentials instead of pickle
2 parents c804044 + 41f82a7 commit 43fc5bc

File tree

16 files changed

+112
-128
lines changed

16 files changed

+112
-128
lines changed

admin_sdk/directory/quickstart.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,25 @@
1414

1515
# [START admin_sdk_directory_quickstart]
1616
from __future__ import print_function
17-
import pickle
1817
import os.path
1918
from googleapiclient.discovery import build
2019
from google_auth_oauthlib.flow import InstalledAppFlow
2120
from google.auth.transport.requests import Request
21+
from google.oauth2.credentials import Credentials
2222

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

2626
def main():
2727
"""Shows basic usage of the Admin SDK Directory API.
2828
Prints the emails and names of the first 10 users in the domain.
2929
"""
3030
creds = None
31-
# The file token.pickle stores the user's access and refresh tokens, and is
31+
# The file token.json stores the user's access and refresh tokens, and is
3232
# created automatically when the authorization flow completes for the first
3333
# time.
34-
if os.path.exists('token.pickle'):
35-
with open('token.pickle', 'rb') as token:
36-
creds = pickle.load(token)
34+
if os.path.exists('token.json'):
35+
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
3736
# If there are no (valid) credentials available, let the user log in.
3837
if not creds or not creds.valid:
3938
if creds and creds.expired and creds.refresh_token:
@@ -43,8 +42,8 @@ def main():
4342
'credentials.json', SCOPES)
4443
creds = flow.run_local_server(port=0)
4544
# Save the credentials for the next run
46-
with open('token.pickle', 'wb') as token:
47-
pickle.dump(creds, token)
45+
with open('token.json', 'w') as token:
46+
token.write(creds.to_json())
4847

4948
service = build('admin', 'directory_v1', credentials=creds)
5049

admin_sdk/reports/quickstart.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,25 @@
1414

1515
# [START admin_sdk_reports_quickstart]
1616
from __future__ import print_function
17-
import pickle
1817
import os.path
1918
from googleapiclient.discovery import build
2019
from google_auth_oauthlib.flow import InstalledAppFlow
2120
from google.auth.transport.requests import Request
21+
from google.oauth2.credentials import Credentials
2222

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

2626
def main():
2727
"""Shows basic usage of the Admin SDK Reports API.
2828
Prints the time, email, and name of the last 10 login events in the domain.
2929
"""
3030
creds = None
31-
# The file token.pickle stores the user's access and refresh tokens, and is
31+
# The file token.json stores the user's access and refresh tokens, and is
3232
# created automatically when the authorization flow completes for the first
3333
# time.
34-
if os.path.exists('token.pickle'):
35-
with open('token.pickle', 'rb') as token:
36-
creds = pickle.load(token)
34+
if os.path.exists('token.json'):
35+
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
3736
# If there are no (valid) credentials available, let the user log in.
3837
if not creds or not creds.valid:
3938
if creds and creds.expired and creds.refresh_token:
@@ -43,8 +42,8 @@ def main():
4342
'credentials.json', SCOPES)
4443
creds = flow.run_local_server(port=0)
4544
# Save the credentials for the next run
46-
with open('token.pickle', 'wb') as token:
47-
pickle.dump(creds, token)
45+
with open('token.json', 'w') as token:
46+
token.write(creds.to_json())
4847

4948
service = build('admin', 'reports_v1', credentials=creds)
5049

admin_sdk/reseller/quickstart.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,25 @@
1414

1515
# [START admin_sdk_reseller_quickstart]
1616
from __future__ import print_function
17-
import pickle
1817
import os.path
1918
from googleapiclient.discovery import build
2019
from google_auth_oauthlib.flow import InstalledAppFlow
2120
from google.auth.transport.requests import Request
21+
from google.oauth2.credentials import Credentials
2222

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

2626
def main():
2727
"""Calls the Admin SDK Reseller API. Prints the customer ID, SKU ID,
2828
and plan name of the first 10 subscriptions managed by the domain.
2929
"""
3030
creds = None
31-
# The file token.pickle stores the user's access and refresh tokens, and is
31+
# The file token.json stores the user's access and refresh tokens, and is
3232
# created automatically when the authorization flow completes for the first
3333
# time.
34-
if os.path.exists('token.pickle'):
35-
with open('token.pickle', 'rb') as token:
36-
creds = pickle.load(token)
34+
if os.path.exists('token.json'):
35+
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
3736
# If there are no (valid) credentials available, let the user log in.
3837
if not creds or not creds.valid:
3938
if creds and creds.expired and creds.refresh_token:
@@ -43,8 +42,8 @@ def main():
4342
'credentials.json', SCOPES)
4443
creds = flow.run_local_server(port=0)
4544
# Save the credentials for the next run
46-
with open('token.pickle', 'wb') as token:
47-
pickle.dump(creds, token)
45+
with open('token.json', 'w') as token:
46+
token.write(creds.to_json())
4847

4948
service = build('reseller', 'v1', credentials=creds)
5049

apps_script/quickstart/quickstart.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919
project, and log the script's URL to the user.
2020
"""
2121
from __future__ import print_function
22-
import pickle
2322
import os.path
2423
from googleapiclient import errors
2524
from googleapiclient.discovery import build
2625
from google_auth_oauthlib.flow import InstalledAppFlow
2726
from google.auth.transport.requests import Request
27+
from google.oauth2.credentials import Credentials
2828

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

3232
SAMPLE_CODE = '''
@@ -46,12 +46,11 @@ def main():
4646
"""Calls the Apps Script API.
4747
"""
4848
creds = None
49-
# The file token.pickle stores the user's access and refresh tokens, and is
49+
# The file token.json stores the user's access and refresh tokens, and is
5050
# created automatically when the authorization flow completes for the first
5151
# time.
52-
if os.path.exists('token.pickle'):
53-
with open('token.pickle', 'rb') as token:
54-
creds = pickle.load(token)
52+
if os.path.exists('token.json'):
53+
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
5554
# If there are no (valid) credentials available, let the user log in.
5655
if not creds or not creds.valid:
5756
if creds and creds.expired and creds.refresh_token:
@@ -61,8 +60,8 @@ def main():
6160
'credentials.json', SCOPES)
6261
creds = flow.run_local_server(port=0)
6362
# Save the credentials for the next run
64-
with open('token.pickle', 'wb') as token:
65-
pickle.dump(creds, token)
63+
with open('token.json', 'w') as token:
64+
token.write(creds.to_json())
6665

6766
service = build('script', 'v1', credentials=creds)
6867

calendar/quickstart/quickstart.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
# [START calendar_quickstart]
1616
from __future__ import print_function
1717
import datetime
18-
import pickle
1918
import os.path
2019
from googleapiclient.discovery import build
2120
from google_auth_oauthlib.flow import InstalledAppFlow
2221
from google.auth.transport.requests import Request
22+
from google.oauth2.credentials import Credentials
2323

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

2727

@@ -30,12 +30,11 @@ def main():
3030
Prints the start and name of the next 10 events on the user's calendar.
3131
"""
3232
creds = None
33-
# The file token.pickle stores the user's access and refresh tokens, and is
33+
# The file token.json stores the user's access and refresh tokens, and is
3434
# created automatically when the authorization flow completes for the first
3535
# time.
36-
if os.path.exists('token.pickle'):
37-
with open('token.pickle', 'rb') as token:
38-
creds = pickle.load(token)
36+
if os.path.exists('token.json'):
37+
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
3938
# If there are no (valid) credentials available, let the user log in.
4039
if not creds or not creds.valid:
4140
if creds and creds.expired and creds.refresh_token:
@@ -45,8 +44,8 @@ def main():
4544
'credentials.json', SCOPES)
4645
creds = flow.run_local_server(port=0)
4746
# Save the credentials for the next run
48-
with open('token.pickle', 'wb') as token:
49-
pickle.dump(creds, token)
47+
with open('token.json', 'w') as token:
48+
token.write(creds.to_json())
5049

5150
service = build('calendar', 'v3', credentials=creds)
5251

classroom/quickstart/quickstart.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,25 @@
1414

1515
# [START classroom_quickstart]
1616
from __future__ import print_function
17-
import pickle
1817
import os.path
1918
from googleapiclient.discovery import build
2019
from google_auth_oauthlib.flow import InstalledAppFlow
2120
from google.auth.transport.requests import Request
21+
from google.oauth2.credentials import Credentials
2222

23-
# If modifying these scopes, delete the file token.pickle.
23+
# If modifying these scopes, delete the file token.json.
2424
SCOPES = ['https://www.googleapis.com/auth/classroom.courses.readonly']
2525

2626
def main():
2727
"""Shows basic usage of the Classroom API.
2828
Prints the names of the first 10 courses the user has access to.
2929
"""
3030
creds = None
31-
# The file token.pickle stores the user's access and refresh tokens, and is
31+
# The file token.json stores the user's access and refresh tokens, and is
3232
# created automatically when the authorization flow completes for the first
3333
# time.
34-
if os.path.exists('token.pickle'):
35-
with open('token.pickle', 'rb') as token:
36-
creds = pickle.load(token)
34+
if os.path.exists('token.json'):
35+
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
3736
# If there are no (valid) credentials available, let the user log in.
3837
if not creds or not creds.valid:
3938
if creds and creds.expired and creds.refresh_token:
@@ -43,8 +42,8 @@ def main():
4342
'credentials.json', SCOPES)
4443
creds = flow.run_local_server(port=0)
4544
# Save the credentials for the next run
46-
with open('token.pickle', 'wb') as token:
47-
pickle.dump(creds, token)
45+
with open('token.json', 'w') as token:
46+
token.write(creds.to_json())
4847

4948
service = build('classroom', 'v1', credentials=creds)
5049

docs/quickstart/quickstart.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414

1515
# [START docs_quickstart]
1616
from __future__ import print_function
17-
import pickle
1817
import os.path
1918
from googleapiclient.discovery import build
2019
from google_auth_oauthlib.flow import InstalledAppFlow
2120
from google.auth.transport.requests import Request
21+
from google.oauth2.credentials import Credentials
2222

23-
# If modifying these scopes, delete the file token.pickle.
23+
# If modifying these scopes, delete the file token.json.
2424
SCOPES = ['https://www.googleapis.com/auth/documents.readonly']
2525

2626
# The ID of a sample document.
@@ -31,12 +31,11 @@ def main():
3131
Prints the title of a sample document.
3232
"""
3333
creds = None
34-
# The file token.pickle stores the user's access and refresh tokens, and is
34+
# The file token.json stores the user's access and refresh tokens, and is
3535
# created automatically when the authorization flow completes for the first
3636
# time.
37-
if os.path.exists('token.pickle'):
38-
with open('token.pickle', 'rb') as token:
39-
creds = pickle.load(token)
37+
if os.path.exists('token.json'):
38+
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
4039
# If there are no (valid) credentials available, let the user log in.
4140
if not creds or not creds.valid:
4241
if creds and creds.expired and creds.refresh_token:
@@ -46,8 +45,8 @@ def main():
4645
'credentials.json', SCOPES)
4746
creds = flow.run_local_server(port=0)
4847
# Save the credentials for the next run
49-
with open('token.pickle', 'wb') as token:
50-
pickle.dump(creds, token)
48+
with open('token.json', 'w') as token:
49+
token.write(creds.to_json())
5150

5251
service = build('docs', 'v1', credentials=creds)
5352

drive/activity-v2/quickstart.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414

1515
# [START drive_activity_v2_quickstart]
1616
from __future__ import print_function
17-
import pickle
1817
import os.path
1918
from googleapiclient.discovery import build
2019
from google_auth_oauthlib.flow import InstalledAppFlow
2120
from google.auth.transport.requests import Request
21+
from google.oauth2.credentials import Credentials
2222

23-
# If modifying these scopes, delete the file token.pickle.
23+
# If modifying these scopes, delete the file token.json.
2424
SCOPES = ['https://www.googleapis.com/auth/drive.activity.readonly']
2525

2626

@@ -30,12 +30,11 @@ def main():
3030
Prints information about the last 10 events that occured the user's Drive.
3131
"""
3232
creds = None
33-
# The file token.pickle stores the user's access and refresh tokens, and is
33+
# The file token.json stores the user's access and refresh tokens, and is
3434
# created automatically when the authorization flow completes for the first
3535
# time.
36-
if os.path.exists('token.pickle'):
37-
with open('token.pickle', 'rb') as token:
38-
creds = pickle.load(token)
36+
if os.path.exists('token.json'):
37+
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
3938
# If there are no (valid) credentials available, let the user log in.
4039
if not creds or not creds.valid:
4140
if creds and creds.expired and creds.refresh_token:
@@ -45,8 +44,8 @@ def main():
4544
'credentials.json', SCOPES)
4645
creds = flow.run_local_server(port=0)
4746
# Save the credentials for the next run
48-
with open('token.pickle', 'wb') as token:
49-
pickle.dump(creds, token)
47+
with open('token.json', 'w') as token:
48+
token.write(creds.to_json())
5049

5150
service = build('driveactivity', 'v2', credentials=creds)
5251

drive/activity/quickstart.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,25 @@
1515
# [START drive_activity_quickstart]
1616
from __future__ import print_function
1717
import datetime
18-
import pickle
1918
import os.path
2019
from googleapiclient.discovery import build
2120
from google_auth_oauthlib.flow import InstalledAppFlow
2221
from google.auth.transport.requests import Request
22+
from google.oauth2.credentials import Credentials
2323

24-
# If modifying these scopes, delete the file token.pickle.
24+
# If modifying these scopes, delete the file token.json.
2525
SCOPES = ['https://www.googleapis.com/auth/activity']
2626

2727
def main():
2828
"""Shows basic usage of the Drive Activity API.
2929
Prints information about the last 10 events that occured the user's Drive.
3030
"""
3131
creds = None
32-
# The file token.pickle stores the user's access and refresh tokens, and is
32+
# The file token.json stores the user's access and refresh tokens, and is
3333
# created automatically when the authorization flow completes for the first
3434
# time.
35-
if os.path.exists('token.pickle'):
36-
with open('token.pickle', 'rb') as token:
37-
creds = pickle.load(token)
35+
if os.path.exists('token.json'):
36+
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
3837
# If there are no (valid) credentials available, let the user log in.
3938
if not creds or not creds.valid:
4039
if creds and creds.expired and creds.refresh_token:
@@ -44,8 +43,8 @@ def main():
4443
'credentials.json', SCOPES)
4544
creds = flow.run_local_server(port=0)
4645
# Save the credentials for the next run
47-
with open('token.pickle', 'wb') as token:
48-
pickle.dump(creds, token)
46+
with open('token.json', 'w') as token:
47+
token.write(creds.to_json())
4948

5049
service = build('appsactivity', 'v1', credentials=creds)
5150

0 commit comments

Comments
 (0)