google-api-python-client 示例代码出现 '模块对象没有属性 socket

0 投票
1 回答
1602 浏览
提问于 2025-04-17 07:04
from apiclient.ext.file import Storage
from apiclient.discovery import build
from apiclient.ext.authtools import run
from oauth2client.client import OAuth2WebServerFlow

FLOW = OAuth2WebServerFlow(
    client_id='SOME_CLIENT_ID',
    client_secret='SOME_CLIENT_SECRET',
    scope='https://www.googleapis.com/auth/latitude.current.best',
    user_agent='lati-go/1.0')

storage = Storage('moderator.dat')
credentials = storage.get()
if credentials is None or credentials.invalid == True:
    credentials = run(FLOW, storage)
http = httplib2.Http(cache=".cache")
http = credentials.authorize(http)
service = build("latitude", "v1", http = http)
data = service.currentLocation().get()

在尝试运行来自Latitude示例的示例代码时,我遇到了一个错误:"_webapp25.py:464] 'module' object has no attribute 'socket'"。

出错的代码是 credentials = run(FLOW, storage)

错误的详细信息是:

Traceback (most recent call last):
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\webapp\_webapp25.py", line 701, in __call__
  handler.get(*groups)
File "H:\cproj\workspace\latitudeReminder\src\lati.py", line 31, in get
  credentials = run(FLOW, storage)
File "H:\cproj\workspace\latitudeReminder\src\apiclient\ext\authtools.py", line 116, in run
  ClientRedirectHandler)
File "C:\Python27\lib\SocketServer.py", line 405, in __init__
  self.socket = socket.socket(self.address_family,
AttributeError: 'module' object has no attribute 'socket'

我哪里做错了呢?

1 个回答

0

如果能把错误信息贴出来就好了,这总是很有帮助。

我猜你的问题是你贴的例子是给命令行程序用的,但你却想用它来做网页服务器的认证流程。试试从 oauth2client.tools 导入 run 吧。

撰写回答