Python Instagram API - API请求失败

时间:2015-04-22 15:39:39

标签: python python-2.7 instagram

我正在学习如何通过python-instagram软件包使用Instagram API。 我已经设置了客户端,拥有客户端ID和客户端密钥。我在python-instagram中使用get_access_token.py脚本获得了访问令牌,但出于某种原因我总是收到此错误:

Traceback (most recent call last):

  File "<ipython-input-38-6440f86cbefd>", line 1, in <module>
    runfile('F:/PythonProjects/minefield/instagram test.py', wdir='F:/PythonProjects/minefield')

  File "C:\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 580, in runfile
    execfile(filename, namespace)

  File "F:/PythonProjects/minefield/instagram test.py", line 13, in <module>
    api.user_search(q='samsungmobile')

  File "C:\Anaconda\lib\site-packages\instagram\bind.py", line 196, in _call
    return method.execute()

  File "C:\Anaconda\lib\site-packages\instagram\bind.py", line 182, in execute
    include_secret=self.include_secret)

  File "C:\Anaconda\lib\site-packages\instagram\oauth2.py", line 224, in prepare_request
    url = self._full_url_with_params(path, params, include_secret)

  File "C:\Anaconda\lib\site-packages\instagram\oauth2.py", line 148, in _full_url_with_params
    self._full_query_with_params(params) +

  File "C:\Anaconda\lib\site-packages\instagram\oauth2.py", line 144, in _full_url
    self._signed_request(path, {}, include_signed_request, include_secret))

  File "C:\Anaconda\lib\site-packages\instagram\oauth2.py", line 172, in _signed_request
    return "&sig=%s" % self._generate_sig(path, params, self.api.client_secret)

  File "C:\Anaconda\lib\site-packages\instagram\oauth2.py", line 127, in _generate_sig
    return  hmac.new(secret, sig, sha256).hexdigest()

  File "C:\Anaconda\lib\hmac.py", line 136, in new
    return HMAC(key, msg, digestmod)

  File "C:\Anaconda\lib\hmac.py", line 71, in __init__
    if len(key) > blocksize:

TypeError: object of type 'NoneType' has no len()

这是我的代码:

from instagram.client import InstagramAPI
access_token = '1********4.0*****5.fb1c**********39***7365*********'
api = InstagramAPI(access_token=access_token)
api.user_search(q='samsungmobile')

调用最后一行api.user_search(q =&#39; samsungmobile&#39;)时脚本失败 我不知道这个TypeError是如何引发的,但我怀疑它可能与访问令牌有关。

我确定我使用的是正确的。 get_access_token.py脚本返回长度为2的元组,第一个索引包含作为字符串的访问令牌,第二个索引包含一个字典,其中包含有关谁有权使用该应用程序的用户信息。

1 个答案:

答案 0 :(得分:2)

我有这个问题。你需要使用最新版本的代码oauth2.py,它位于github中。特别是,请参阅此更改列表:

https://github.com/Instagram/python-instagram/commit/d46e0eb96dcf5a3aad687da4081b91441b3392e5

特别是,这是oauth2.py中的旧行和新行:

-        if include_signed_request:
+        if include_signed_request and self.api.client_secret is not None:

在改变之后一切正常。