Instagram:签名不匹配

Instagram: Signature did not match

我正在尝试开始使用 Instagram API,但我什至不能打一个简单的电话,因为我收到一个错误

{"code": 403, "error_type": "OAuthForbiddenException", "error_message": "Invalid signed-request: Signature does not match"}

我生成了 access_token,范围为 likes+comments

这是我的URL: https://api.instagram.com/v1/media/search?lat=48.858844&lng=2.294351&access_token=ACCESS-TOKEN&sig=SIG

我使用来自 Instagram 开发者网站的这个脚本生成了签名,因为它最初给了我

"Invalid signed-request: Missing required parameter 'sig'"

这是脚本:

    # -*- coding: UTF-8 -*-
import hmac
from hashlib import sha256

def generate_sig(endpoint, params, secret):
    sig = endpoint
    for key in sorted(params.keys()):
        sig += '|%s=%s' % (key, params[key])
    return hmac.new(secret, sig, sha256).hexdigest()

endpoint = 'media/search'
params = {
    'access_token': _______________,
    'count': 10,
}
secret = ______________________
sig = generate_sig(endpoint, params, secret)
print "sig is",sig

感谢任何帮助! 谢谢

来自文档:

When enabled, Instagram will check for the sig parameter of each request and verify that the value matches a hash computed using your Client Secret. The expected value is a HMAC using the SHA256 hash algorithm with all your request parameters and your Client Secret.

你的签名生成器函数没问题,但它没有包含所有参数。应该是:

params = {
    'access_token': _______________,
    'count': 10,
    'lat':  "<lat value>",
    'lng': "<long value>",
}

应该可以正常使用。 此外,这里还有一些有用的附加指南:
http://instagram-api.tumblr.com/post/120586735719/instagram-secure-api-requests

编辑:

这里和我做的一模一样:

# coding: utf-8

# My client_id, secret, access_token, etc...
import settings
from urllib import urlencode
import hmac
from hashlib import sha256

def generate_sig(endpoint, params, secret):
    sig = endpoint
    for key in sorted(params.keys()):
        sig += '|%s=%s' % (key, params[key])
    return hmac.new(secret, sig, sha256).hexdigest()


endpoint = '/media/search'
params = {
    'access_token': settings.ACCESS_TOKEN,
    'lat': '48.858844',
    'lng': '2.294351',
    'count': 10,
}
params.update({'sig': generate_sig(endpoint, params, settings.CLIENT_SECRET)})
url = 'https://api.instagram.com/v1' + endpoint + '?' + urlencode(params)

# Success!!!
print url

它应该给我们一个 URL 这样的格式:

https://api.instagram.com/v1/media/search?access_token=XXX&lat=XXX&lng=XXX&sig=XXX&count=XXX