【问题标题】:Google Api Auth Http Module ErrorGoogle Api Auth Http 模块错误
【发布时间】:2023-03-26 07:55:01
【问题描述】:

我正在开发我的第一个使用 Google Api for Calendar 的应用程序。我在以下位置阅读了 Google 示例:https://developers.google.com/google-apps/calendar/instantiate

我第一次运行下面的程序是成功的。我允许我的应用程序访问我的 Google 帐户,并且该应用程序在我的应用程序目录中创建了一个包含身份验证信息的 calendar.dat 文件。在我重命名文件后,验证码中的代码停止工作。我已经完全删除了该文件并从头开始重新创建它,但错误仍然存​​在。

我仍然可以看到 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:\Users\Desktop\Google Drive\Code\Python\Rooster\calendar.py", line 2, in <module>
    import httplib2
  File "C:\Python27\lib\site-packages\httplib2-0.7.6-py2.7.egg\httplib2\__init__.py", line 42, in <module>
    import calendar
  File "C:\Users\Desktop\Google Drive\Code\Python\Rooster\calendar.py", line 33, in <module>
    credentials = run(FLOW, storage)
  File "C:\Python27\lib\site-packages\google_api_python_client-1.0-py2.7.egg\oauth2client\util.py", line 120, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "C:\Python27\lib\site-packages\google_api_python_client-1.0-py2.7.egg\oauth2client\tools.py", line 169, in run
    credential = flow.step2_exchange(code, http=http)
  File "C:\Python27\lib\site-packages\google_api_python_client-1.0-py2.7.egg\oauth2client\util.py", line 120, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "C:\Python27\lib\site-packages\google_api_python_client-1.0-py2.7.egg\oauth2client\client.py", line 1128, in step2_exchange
    http = httplib2.Http()
AttributeError: 'module' object has no attribute 'Http'

【问题讨论】:

    标签: python google-api google-calendar-api google-api-python-client


    【解决方案1】:

    问题是在你的运行目录中有一个名为 calendar.py 的文件。当 Google 的 httplib2 想要导入标准日历模块时,它会改为使用本地日历模块。在本地它执行它来执行导入。但是由于 httplib2 尚未完全导入,因此 calendar.py 代码无法正常工作。 只需将 calendar.py 重命名为 myCalendar.py 之类的名称即可。

    【讨论】:

    • 非常感谢,我已经尝试过了,因为我怀疑这是问题所在,但是它不起作用。我在 Aptana3 中编码并在程序中重命名了文件。但是,在阅读了您的评论并检查了目录后,似乎 Aptana 也生成了编译后的 python 文件,因此目录中仍然有一个额外的 calendar.py。
    • 那不是 Aptana,而是我想的 python。 Python 解释器默认创建编译版本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-27
    • 1970-01-01
    • 1970-01-01
    • 2014-06-08
    • 2018-04-14
    • 1970-01-01
    相关资源
    最近更新 更多