1

如本页所述,我无法使用模块 contextIO ....

https://github.com/contextio/Python-ContextIO

我在希望看到电子邮件内容的地方收到错误消息。

import contextio as c

CONSUMER_KEY = 'dummy_key'
CONSUMER_SECRET = 'dummy_secret'

context_io = c.ContextIO(
    consumer_key=CONSUMER_KEY, 
    consumer_secret=CONSUMER_SECRET
)

accounts = context_io.get_accounts(email='dummy.email@gmail.com')

params = {
    'id': 'dummy_id'
}

account = c.Account(context_io, params) 

params = {
    'message_id': 'dummy_message_id'
}

message = c.Message(account, params)

message.get()

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "contextio/__init__.py", line 1721, in get
    self.__init__(self.parent, self._request_uri('', params=params))
  File "contextio/__init__.py", line 481, in _request_uri
    uri, method=method, params=params, headers=headers, body=body
  File "contextio/__init__.py", line 481, in _request_uri
    uri, method=method, params=params, headers=headers, body=body
  File "contextio/__init__.py", line 150, in _request_uri
    self._handle_request_error(response)
  File "contextio/__init__.py", line 185, in _handle_request_error
    response_json = response.json()
  File "/usr/local/lib/python2.7/dist-packages/requests/models.py", line 638, in json
    return json.loads(self.text or self.content, **kwargs)
  File "/usr/lib/python2.7/dist-packages/simplejson/__init__.py", line 413, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/dist-packages/simplejson/decoder.py", line 402, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python2.7/dist-packages/simplejson/decoder.py", line 420, in raw_decode
    raise JSONDecodeError("No JSON object could be decoded", s, idx)
simplejson.decoder.JSONDecodeError: No JSON object could be decoded: line 1 column 0 (char 0)

我怎么知道这个错误是否是由于作为消息对象返回的 NULL 引起的?什么是 NoneType?

>>> type(message)
<class 'contextio.Message'>
>>> dir(message)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_request_uri', '_uri_for', 'addresses', 'base_uri', 'body', 'date', 'date_indexed', 'delete', 'email_message_id', 'facebook_headers', 'files', 'flags', 'folders', 'get', 'get_body', 'get_flags', 'get_folders', 'get_headers', 'get_source', 'get_thread', 'gmail_message_id', 'gmail_thread_id', 'headers', 'keys', 'list_headers', 'message_id', 'parent', 'person_info', 'post', 'post_flag', 'post_folder', 'put_folders', 'sanitize_params', 'source', 'sources', 'subject', 'thread']

>>> type(message.subject)
<type 'NoneType'>

为什么即使我提供了正确的凭据,我的代码也没有返回任何内容?

4

1 回答 1

2

发生此错误是因为 Context.IO 库需要 JSON 响应,但它没有得到它。这是它爆炸的地方:

File "contextio/__init__.py", line 185, in _handle_request_error
  response_json = response.json()

这可能是服务的问题,返回了无效的 JSON 正文或无效的标头,但很可能,您的请求由于某些其他原因而失败,并且库没有准备好处理该错误。

尝试进入上面的行,在那里设置一个中断并检查response.content. 如果这是一条错误消息,它将提示您出了什么问题,以及库失败的原因。如果它是有效的 JSON,请检查Content-Type标头并确保它是application/json.

于 2013-11-10T20:15:30.780 回答