1919project, and log the script's URL to the user.
2020"""
2121from __future__ import print_function
22- import pickle
2322import os .path
2423from googleapiclient import errors
2524from googleapiclient .discovery import build
2625from google_auth_oauthlib .flow import InstalledAppFlow
2726from 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 .
3030SCOPES = ['https://www.googleapis.com/auth/script.projects' ]
3131
3232SAMPLE_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
0 commit comments