我正在学习如何通过 python-instagram 包使用 Instagram API。 我已经设置了客户端,有客户端 ID 和客户端密码。我在 python-instagram 中使用 get_access_token.py 脚本获得了一个访问 token ,但由于某种原因,我总是收到此错误:
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='samsungmobile') 时脚本失败 我不知道这个 TypeError 是如何引发的,但我怀疑它可能与访问 token 有关。
我确定我使用的是正确的。 get_access_token.py 脚本返回一个长度为 2 的元组,第一个索引包含作为字符串的访问 token ,第二个索引包含一个字典,其中包含有关谁有权使用该应用程序的用户信息。
最佳答案
我有这个问题。您需要使用 github 中 oauth2.py 的最新版本代码。特别是,请参阅此更改列表:
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:
更改后一切正常。
关于Python Instagram API - API 请求失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29802489/