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

落花浮王杯 提交于 2019-12-22 05:12:16

问题


Made myself a simple Instagram client to make authenticated requests to their API. However, running it keeps throwing the following error

Traceback (most recent call last):
  File "request-ig-data.py", line 24, in <module>
   recent_media = api.user_recent_media(user_id=user_id, count=50)
  File "/lib/python2.7/site-packages/instagram/bind.py", line 151, in _call
return method.execute()
  File "/lib/python2.7/site-packages/instagram/bind.py", line 143, in execute
content, next = self._do_api_request(url, method, body, headers)
  File "/lib/python2.7/site-packages/instagram/bind.py", line 99, in _do_api_request
raise InstagramClientError('Unable to parse response, not valid JSON.')
instagram.bind.InstagramClientError: Unable to parse response, not valid JSON.

Ok, so the response isn't JSON (despite the fact that I'd expect it to be given that's Instagram's doc'd reponse format), but why? Code is a pretty basic implementation of python-instagram - followed their docs and have tried converting response to JSON but still getting same err. Here's code:

from instagram.client import InstagramAPI
import httplib2
import json
import sys

client_id = '[...]'
client_secret = '[...]'
redirect_uri = 'https://mycallback.com'
scope = ''
user_id = '1034466'  # starbucks

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:\n", redirect_uri

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

access_token = api.exchange_code_for_access_token(code)

print "access token:\n", access_token

api = InstagramAPI(access_token=access_token)
recent_media, next = api.user_recent_media(user_id=user_id, count=50)
for media in recent_media:
   print media.text

回答1:


This line:

access_token = api.exchange_code_for_access_token(code)

Is better put in this way:

access_token, user_info = api.exchange_code_for_access_token(code)

That would split out the access token from the user info. That way you can keep this line the same:

client.InstagramAPI(access_token=access_token)  



回答2:


Figured it out. api = client.InstagramAPI(access_token=access_token) should be api = client.InstagramAPI(access_token=access_token[0]) to pass the access token and not the full object.



来源:https://stackoverflow.com/questions/21292558/instagram-bind-instagramclienterror-unable-to-parse-response-not-valid-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!