1

在查看了几篇在线文章、StackOverflow 和 Yelp Google Group 之后,我一直无法弄清楚Invalid Signature我的 Yelp API 请求所产生的错误的问题。

这是确切的错误:

{'error': {'text': 'Signature was invalid', 'description': 'Invalid signature. Expected signature base string: [some text here with keys]}}

以及我编写的代码:

import rauth
import time

def get_results():

    #Obtain these from Yelp's manage access page
    consumer_key = ''
    consumer_secret = ''
    token = ''
    token_secret = ''

    session = rauth.OAuth1Session(
            consumer_key = consumer_key
            ,consumer_secret = consumer_secret
            ,access_token = token
            ,access_token_secret = token_secret)

    request = session.get("http://api.yelp.com/v2/search?location=Boston&term=food")

    #Transforms the JSON API response into a Python dictionary
    data = request.json()
    print(data)
    session.close()

    return data

if __name__=="__main__":
    print(get_results())

那么究竟是什么导致了这个错误呢?在此尝试之前我已经做了一些修改,而我之前的尝试也遇到了类似的错误;除了有一次我只收到“无效签名”错误,没有“期望签名基础字符串”消息

4

2 回答 2

3

根据文档,身份验证有更多步骤

提出要求

每个请求必须包含以下 OAuth 协议参数:

OAuth Parameter Value
oauth_consumer_key  Your OAuth consumer key (from Manage API Access).
oauth_token The access token obtained (from Manage API Access).
oauth_signature_method  hmac-sha1
oauth_signature The generated request signature, signed with the oauth_token_secret obtained (from Manage API Access).
oauth_timestamp Timestamp for the request in seconds since the Unix epoch.
oauth_nonce A unique string randomly generated per request.

这些参数可以作为 URL 查询键在 HTTP(授权)标头中传递,也可以在 POST 数据中传递。通过应用带有 oauth_token_secret 的 HMAC-SHA1 来生成 OAuth 签名。您可以在管理 API 访问中查看您的 OAuth 使用者密钥。OAuth 库可用于生成这些请求。

您没有传递oauth_timestamp所需的内容或应用 HMAC-SHA1,因此您会收到Invalid Signature错误,上面的文档中清楚地概述了您需要发送的内容。

您还可以使用一个实际的 python yelp api,但是要发出请求,您可以根据示例代码request中的函数使用下面的 示例:使用and发出请求:oauth2requests

import requests
import oauth2

def request(url, url_params=None):
    consumer_key = ""
    consumer_secret = ""
    token = ""
    token_secret =""
    url_params = url_params or {}
    consumer = oauth2.Consumer(consumer_key, consumer_secret)
    oauth_request = oauth2.Request(method="GET", url=url, parameters=url_params)

    oauth_request.update(
        {
            'oauth_nonce': oauth2.generate_nonce(),
            'oauth_timestamp': oauth2.generate_timestamp(),
            'oauth_token': token,
            'oauth_consumer_key': consumer_key
        }
    )
    token = oauth2.Token(token, token_secret)
    oauth_request.sign_request(oauth2.SignatureMethod_HMAC_SHA1(), consumer, token)
    signed_url = oauth_request.to_url()

    print(u'Querying {0} ...'.format(url))

    return requests.get(signed_url).json()

哪个使用您的 url 输出整个 json 负载,其开头是:

Querying http://api.yelp.com/v2/search?location=Boston&term=food ...
{'region': {'center': {'longitude': -71.05460875, 'latitude': 42.35028894954365}, 'span': {'latitude_delta': 0.0325510910039668, 'longitude_delta': 0.04668455000000904}}, 'total': 8351, 'businesses': [{'name': "Giacomo's Ristorante", 'url': 'http://www.yelp.com/biz/giacomos-ristorante-boston', 'mobile_url': 'http://m.yelp.com/biz/giacomos-ristorante-boston', 'rating_img_url_large': 'http://s3-media2.fl.yelpcdn.com/assets/2/www/img/ccf2b76faa2c/ico/stars/v1/stars_large_4.png', 'phone': 
...............................................................
...............................................................

我不确定 api 是否支持 python 3,但上面的代码是用 python3 和 python2 测试的,它工作正常,如果你没有安装它,安装oauth2你可以简单和相同的请求。pip install oauth2

于 2015-10-21T17:33:49.897 回答
0

另一个常见问题是服务器时间不同步。在linux上,可以运行

sudo ntpdate -s time.nist.gov
于 2016-04-24T03:46:02.930 回答