无法解析响应,无效JSON

时间:2016-02-07 09:44:25

标签: python json instagram-api

尝试触发此行代码(https://github.com/Instagram/python-instagram

时收到此错误消息
recent_media, next_ = api.user_recent_media(user_id="userid", count=10)

完整源代码

from instagram.client import InstagramAPI
import sys

# get access token
if len(sys.argv) > 1 and sys.argv[1] == 'local':
    try:
        from test_settings import *

        InstagramAPI.host = test_host
        InstagramAPI.base_path = test_base_path
        InstagramAPI.access_token_field = "access_token"
        InstagramAPI.authorize_url = test_authorize_url
        InstagramAPI.access_token_url = test_access_token_url
        InstagramAPI.protocol = test_protocol
    except Exception:
        pass

# Fix Python 2.x.
try:
    import __builtin__
    input = getattr(__builtin__, 'raw_input')
except (ImportError, AttributeError):
    pass

client_id = input("Client ID: ").strip()
client_secret = input("Client Secret: ").strip()
redirect_uri = input("Redirect URI: ").strip()
raw_scope = input("Requested scope (separated by spaces, blank for just basic read): ").strip()
scope = raw_scope.split(' ')
# For basic, API seems to need to be set explicitly
if not scope or scope == [""]:
    scope = ["basic"]

api = InstagramAPI(client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri)
redirect_uri = api.get_authorize_login_url(scope = scope)

print ("Visit this page and authorize access in your browser: "+ redirect_uri)

code = (str(input("Paste in code in query string after redirect: ").strip()))

access_token = api.exchange_code_for_access_token(code)

api = InstagramAPI(access_token=access_token[0], client_secret=client_secret)
recent_media, next_ = api.user_recent_media(user_id="userid", count=10)
for media in recent_media:
   print media.caption.text

错误消息

Traceback (most recent call last):
  File "python-instagram-stream.py", line 44, in <module>
    recent_media, next_ = api.user_recent_media(user_id="userid", count=10)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/instagram/bind.py", line 197, in _call
    return method.execute()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/instagram/bind.py", line 189, in execute
    content, next = self._do_api_request(url, method, body, headers)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/instagram/bind.py", line 131, in _do_api_request
    raise InstagramClientError('Unable to parse response, not valid JSON.', status_code=response['status'])
instagram.bind.InstagramClientError: (400) Unable to parse response, not valid JSON.

我已经尝试过此解决方案,但仍然会产生相同的错误消息

instagram.bind.InstagramClientError: Unable to parse response, not valid JSON

我从

改变了它
api = InstagramAPI(access_token=access_token, client_secret=client_secret)

api = InstagramAPI(access_token=access_token[0], client_secret=client_secret)

1 个答案:

答案 0 :(得分:1)

最有可能的问题出在您api.user_recent_media的电话中,如下一行:

recent_media, next_ = api.user_recent_media(user_id="userid", count=10)

具体而言,我怀疑"userid"是否符合预期。

我认为使用通过调用新行获得的值:

user_id = api.user_search('username')[0].id

更有可能为您带来成功,username被相关实际用户所取代。