我无法弄清楚如何用Google.auth替换oauth2client:
import datetimeimport httplib2import osfrom apiclient import discoveryimport oauth2clientfrom oauth2client import clIEntfrom oauth2client import tools
…
credentials = get_credentials()http = credentials.authorize(httplib2.http())service = discovery.build('calendar','v3',http=http)解决方法 根据 oauth2client deprecation notes,用于管理Google用户凭据的替代品是 google-auth-oauthlib.在我的PC上工作的片段(python 3.6)下方.
由于文档突出显示新库不保存凭据,这就是我使用pickle保存它们的原因.也许,根据您的应用程序要求,您希望拥有更强大的解决方案(如数据库).
import osimport picklefrom Googleapiclient.discovery import buildfrom Google_auth_oauthlib.flow import InstalledAppFlowfrom Google.auth.transport.requests import RequestScopES = ['https://www.GoogleAPIs.com/auth/calendar.Readonly',]# we check if the file to store the credentials existsif not os.path.exists('credentials.dat'): flow = InstalledAppFlow.from_clIEnt_secrets_file('clIEnt_ID.Json',ScopES) credentials = flow.run_local_server() with open('credentials.dat','wb') as credentials_dat: pickle.dump(credentials,credentials_dat)else: with open('credentials.dat','rb') as credentials_dat: credentials = pickle.load(credentials_dat)if credentials.expired: credentials.refresh(Request())calendar_sdk = build('calendar',credentials=credentials)calendars_get_params = { 'calendarID': 'primary',}test = calendar_sdk.calendars().get(**calendars_get_params).execute()print(test)总结
以上是内存溢出为你收集整理的如何在Python中使用google.auth而不是oauth2client来访问我的Google日历全部内容,希望文章能够帮你解决如何在Python中使用google.auth而不是oauth2client来访问我的Google日历所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)