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
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With