加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 谷歌Api Auth Http模块错误

发布时间:2020-12-16 21:58:16 所属栏目:Python 来源:网络整理
导读:我正在制作我的第一个应用程序,使用Google Api for Calendar.我在https://developers.google.com/google-apps/calendar/instantiate阅读了Google示例 我第一次运行下面的程序是成功的.我允许我的应用访问我的Google帐户,应用程序在我的app-directory中创建了

我正在制作我的第一个应用程序,使用Google Api for Calendar.我在https://developers.google.com/google-apps/calendar/instantiate阅读了Google示例

我第一次运行下面的程序是成功的.我允许我的应用访问我的Google帐户,应用程序在我的app-directory中创建了一个带有auth信息的calendar.dat文件.在我重命名该文件后,代码在auth停止工作.我已经完全删除了文件并从头开始重新创建,但错误仍然存??在.

我仍然可以获得Google身份验证页面,但仍然可以确认访问权限,之后我收到一条消息,说明身份验证流程已完成.

这是代码(我用我的应用详细信息填写的标准Google示例):

import gflags
import httplib2

from apiclient.discovery import build
from oauth2client.file import Storage
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.tools import run

FLAGS = gflags.FLAGS

# Set up a Flow object to be used if we need to authenticate. This
# sample uses OAuth 2.0,and we set up the OAuth2WebServerFlow with
# the information it needs to authenticate. Note that it is called
# the Web Server Flow,but it can also handle the flow for native
# applications
# The client_id and client_secret are copied from the API Access tab on
# the Google APIs Console
FLOW = OAuth2WebServerFlow(
    client_id='YOUR_CLIENT_ID',client_secret='YOUR_CLIENT_SECRET',scope='https://www.googleapis.com/auth/calendar',user_agent='YOUR_APPLICATION_NAME/YOUR_APPLICATION_VERSION')

# To disable the local server feature,uncomment the following line:
# FLAGS.auth_local_webserver = False

# If the Credentials don't exist or are invalid,run through the native client
# flow. The Storage object will ensure that if successful the good
# Credentials will get written back to a file.
storage = Storage('calendar.dat')
credentials = storage.get()
if credentials is None or credentials.invalid == True:
  credentials = run(FLOW,storage)

# Create an httplib2.Http object to handle our HTTP requests and authorize it
# with our good Credentials.
http = httplib2.Http()
http = credentials.authorize(http)

# Build a service object for interacting with the API. Visit
# the Google APIs Console
# to get a developerKey for your own application.
service = build(serviceName='calendar',version='v3',http=http,developerKey='YOUR_DEVELOPER_KEY')

这是输出:

Your browser has been opened to visit:

    https://accounts.google.com/o/oauth2/auth? (auth url shortened)

If your browser is on a different machine then exit and re-run this
application with the command-line parameter 

  --noauth_local_webserver

Traceback (most recent call last):
  File "C:UsersDesktopGoogle DriveCodePythonRoostercalendar.py",line 2,in 
最佳答案
问题是在运行目录中有一个名为calendar.py的文件.当Google的httplib2想要导入标准日历模块时,它会获得本地日历模块.在本地,它执行它来执行导入.但由于httplib2尚未完全导入,因此calendar.py代码无法正常工作.
只需将calendar.py重命名为myCalendar.py.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读