Google Api Auth Http模块错误 [英] Google Api Auth Http Module Error

查看:296
本文介绍了Google Api Auth Http模块错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理我的第一个应用程序,以便使用Google Api for Calendar。我已阅读Google示例: https://developers.google.com/google- apps / calendar / instantiate



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



我仍然可以访问Google身份验证页,并且仍然可以确认访问权限,之后我获取消息,认证流程已完成。



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

  import gflags 
从apiclient.discovery导入httplib2

从oauth2client.file导入构建
import Storage $来自oauth2client.client的b $ b从oauth2client.tools导入OAuth2WebServerFlow
导入运行

FLAGS = gflags.FLAGS

#设置要使用的Flow对象if我们需要认证。此
#示例使用OAuth 2.0,并且我们使用
#设置了OAuth2WebServerFlow它需要进行身份验证的信息。请注意,它被称为
#Web服务器流程,但它也可以处理原生
#应用程序的流程
#client_id和client_secret从$ b $上的API访问选项卡复制b#Google API控制台
FLOW = OAuth2WebServerFlow(
client_id ='YOUR_CLIENT_ID',
client_secret ='YOUR_CLIENT_SECRET',$ b $ scope ='https://www.googleapis.com / auth / calendar',
user_agent ='YOUR_APPLICATION_NAME / YOUR_APPLICATION_VERSION')

#要禁用本地服务器功能,请取消注释以下行:
#FLAGS.auth_local_webserver = False

#如果凭证不存在或无效,请运行本地客户端
#流程。存储对象将确保如果成功的话,好的
#Credentials将被写回到一个文件中。
storage = Storage('calendar.dat')
credentials = storage.get()
如果凭证是None或credentials.invalid == True:
credentials = run(FLOW ,存储)

#创建一个httplib2.Http对象来处理我们的HTTP请求,并使用我们良好的凭证授权
#。
http = httplib2.Http()
http = credentials.authorize(http)

#构建一个服务对象来与API进行交互。请访问
#Google API控制台
#以获取适用于您自己应用程序的developerKey。
service = build(serviceName ='calendar',version ='v3',http = http,
developerKey ='YOUR_DEVELOPER_KEY')

这就是输出:

 您的浏览器已被打开访问:

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

如果您的浏览器位于不同的计算机上,请使用命令行参数

退出并重新运行此
应用程序--noauth_local_webserver

Traceback(最近一次调用最后一次):
文件C:\ Users \Desktop\Google Drive\Code\Python\Rooster\calendar.py,第2行,在< module>
导入httplib2
文件C:\Python27\lib\site-packages\httplib2-0.7.6-py2.7.egg\httplib2\__init __。py,第42行,在< module>
导入日历
文件C:\ Users \Desktop\Google Drive\Code\Python\Rooster\calendar.py,第33行,位于< module>
credentials = run(FLOW,storage)
文件C:\Python27\lib\site-packages\google_api_python_client-1.0-py2.7.egg\oauth2client\util.py ,第120行,在locations_wrapper
返回包装(* args,** kwargs)
文件C:\Python27\lib\site-packages\google_api_python_client-1.0-py2.7。在运行
credential = flow.step2_exchange(code,http = http)
文件C:\Python27\lib\site- packages'\\google_api_python_client-1.0-py2.7.egg\oauth2client\util.py,第120行,位于位置包装程序
返回包装(* args,** kwargs)
文件C:\\ \\ Python27 \lib\site-packages\google_api_python_client-1.0-py2.7.egg\oauth2client\client.py,第1128行,在step2_exchange
http = httplib2.Http()
AttributeError:'module'object has no attribute'Http'


解决方案只需将calendar.py重命名为myCalendar.py。


I'm working on my first app ever to use Google Api for Calendar. I've read the Google examples at: https://developers.google.com/google-apps/calendar/instantiate

The first time I ran the program below it was successful. I allowed my app to access my Google account and the application made a calendar.dat file with the auth info in my app-directory. After I renamed the filed the code was in the auth stopped working. I have already deleted the file entirely and recreated it from scratch, but the error persists.

I do still get the Google authentication page and can still confirm access, after which I get a message that the authentication flow was completed.

This is the code (standard Google example which I fill in with my app details):

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')

And this is the output:

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'

解决方案

The problem is that in your run directory you have the file named calendar.py. When Google's httplib2 wants to import a standard calendar module it gets the local one instead. In the local one it executes it to perform the import. But because httplib2 is not yet fully imported the calendar.py code is not working properly. Just rename the calendar.py to something like myCalendar.py.

这篇关于Google Api Auth Http模块错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆