2014-01-22 191 views
6

使我自己成爲一個簡單的Instagram客戶端,可以對其API進行身份驗證請求。然而,運行它不斷拋出下面的錯誤instagram.bind.InstagramClientError:無法解析響應,無效JSON

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 

回答

2

想通了。 api = client.InstagramAPI(access_token=access_token)應該是api = client.InstagramAPI(access_token=access_token[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) 
+3

我還要指出,我收到了同樣的錯誤,但意識到這是因爲我嘗試使用的用戶名,而不是用戶ID(可根據檢索通過user_search方法在用戶名上:user = api.user_search('username')[0] .id')。 – bshur2008

+0

bshur2008是完全正確的 - 我發送的用戶名不是用戶標識 – LittleBobbyTables