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

2024/9/20 5:59:25

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 sysclient_id = '[...]'
client_secret = '[...]'
redirect_uri = 'https://mycallback.com'
scope = ''
user_id = '1034466'  # starbucksapi = 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_uricode = 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_tokenapi = 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
Answer

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)  
https://en.xdnf.cn/q/72381.html

Related Q&A

Using jinja to send data to Javascript

I have Python code, in which Im using jinja to send data to a template in Flask. I can access the code just find in HTML, but when I try displaying the data in Javascript, it doesnt work. For example, …

celery: Substantial drift from

I have quite a problem with celery on my distribted system. I have couple of machines among different localizations and Ive got a lot of warnings in my log files like:"Substantial drift from celer…

jinja2 link to static files

I am trying to understand how to create a link to static files in jinja2.Everything I look up relates to Flask whereas I am using just webapp2 at this stage.My main.py file looks as follows:import os i…

How to upgrade tensorflow with GPU on google colaboratory

Currently google colaboratory uses tensorflow 1.4.1. I want to upgrade it to 1.5.0 version. Each time when i executed !pip install --upgrade tensorflow command, notebook instance succesfully upgrades t…

How to print warnings and errors when using setuptools (pip)

I am using setuptools to package code such that it can be easily installed using cd project_name && pip install .During the setup process, I want to warn the user about pre-existing config file…

TypeError: not supported between instances of State and State PYTHON 3

I am trying to utilize a PriorityQueue from the queue class. However, im having issues putting custom objects into my PQ. I have implemented the __cmp__ function below:def __cmp__(self, other):return (…

Threading and information passing -- how to

To reframe from confusion i have edited the question:one.pyimport threading count = 5 dev = threading.Thread(name=dev, target=dev,args=(workQueue,count,)) dev.setDaemon(True) dev.start() workQueue = Qu…

How to rename a node with Python LXML?

How do I rename a node using LXML? Specifically, how to rename a parent node i.e. a <body> tag while preserving all the underlying structure? I am parsing using the lxml.html module but suppos…

In python, selenium, how do I set the error messages for a wait?

I have the following code:WebDriverWait(self.driver, 20).until(expected_conditions.element_to_be_clickable(click))Now this sometimes fails and I know why it fails. But the error gives me TimeoutExcept…

Python: the mechanism behind list comprehension

When using list comprehension or the in keyword in a for loop context, i.e:for o in X:do_something_with(o)orl=[o for o in X]How does the mechanism behind in works? Which functions\methods within X do…