让自己成为一个简单的Instagram客户端,对其API进行身份验证请求。但是,运行它会不断抛出以下错误
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.
好的,所以答案不是JSON(尽管事实上我希望它是Instagram的doc'd响应格式),但为什么呢?代码是python-instagram的一个非常基本的实现 - 遵循他们的文档,并尝试将响应转换为JSON,但仍然得到相同的错误。这是代码:
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
答案 0 :(得分:4)
这一行:
access_token = api.exchange_code_for_access_token(code)
最好这样做:
access_token, user_info = api.exchange_code_for_access_token(code)
这将从用户信息中分离出访问令牌。这样你可以保持这条线相同:
client.InstagramAPI(access_token=access_token)
答案 1 :(得分:2)
想出来。 <{1}}应api = client.InstagramAPI(access_token=access_token)
传递访问令牌,而不是完整对象。