2013-10-09 41 views
1

我不能夠使用該模塊contextIO因爲這頁解釋....故障相對於Python代碼爲NULL

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='[email protected]') 

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

爲什麼我的代碼不會返回任何東西,即使我提供了正確的憑據?

+0

'NoneType'是一個'None'對象,它的Python等價於'NULL' – SpliFF

回答

2

發生此錯誤是因爲Context.IO lib期待JSON響應,但它沒有得到它。這是它炸燬:

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

這可能與該服務的問題,返回無效JSON體或無效的頭,但最有可能,您的請求失敗某些其他原因和庫不準備處理錯誤。

嘗試進入上面的行,在那裏設置一箇中斷點,並檢查response.content的值是多少。如果這是一個錯誤消息,它會給你提示什麼是錯誤的,以及爲什麼庫失敗。如果它是有效的JSON,請檢查Content-Type標題並確保其爲application/json