Instagram.bind.InstagramClientError: cannot parse response, invalid JSON

Made himself a simple Instagram client to make authenticated requests to their API. However, at startup, it continues to throw 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 answer is not JSON (despite the fact that I expect it to be provided with the response format in Instagram doc'd format), but why? The code - a fairly simple python-instagram implementation - followed their docs and tried to convert the response to JSON, but still got the same error. Here is the 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 
+6
source share
2 answers

This line:

 access_token = api.exchange_code_for_access_token(code) 

Better put this way:

 access_token, user_info = api.exchange_code_for_access_token(code) 

This would split the access token from user information. This way you can keep this line the same:

 client.InstagramAPI(access_token=access_token) 
+4
source

Figured it out. api = client.InstagramAPI(access_token=access_token) must be api = client.InstagramAPI(access_token=access_token[0]) in order to pass the access token, not the complete object.

+2
source

All Articles