我正在尝试用Python实现对Instagram API的签名调用。目前我的标题看起来像这样:
user_agent = 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7'
headers = {
'User-Agent': user_agent,
"Content-type": "application/x-www-form-urlencoded"
}
我根据this page (Restrict API Requests @ instagram)提供的说明尝试了几种排列,包括 HMAC方法,并启用"强制签名标题" API设置页面。
但我不断收到headers not found
或403
错误。我只是想弄清楚如何正确编码X-Insta-Forwarded-For
你能帮忙解决如何在Python中传递带头的签名调用吗? 非常感谢...
答案 0 :(得分:1)
这应该为你做。你也需要Crypto python库。
import requests
from Crypto.Hash import HMAC, SHA256
#change these accordingly
client_secret = "mysecret"
client_ip = "127.0.0.1"
hmac = HMAC.new(client_secret, digestmod=SHA256)
hmac.update(client_ip)
signature = hmac.hexdigest()
header_string = "%s|%s" % (client_ip, signature)
headers = {
"X-Insta-Forwarded-For" : header_string,
#and the rest of your headers
}
#or use requests.post or del since that's the
#only time that this header is used...just
#conveying the concept
resp = requests.get(insta_url, headers=headers)
如果使用您列出的引用上给出的示例对其进行测试,则可以使用此方法验证是否获得了正确的哈希值
ip = "200.15.1.1"
secret = "6dc1787668c64c939929c17683d7cb74"
hmac = HMAC.new(secret, digestmod=SHA256)
hmac.update(ip)
signature = hmac.hexdigest()
# should be 7e3c45bc34f56fd8e762ee4590a53c8c2bbce27e967a85484712e5faa0191688
根据参考文档 - “要启用此设置,请编辑OAuth客户端配置并标记强制签名标题复选框。”所以一定要确保你也这样做了