发帖请求时的Yelp API Validation_Error

时间:2016-11-06 17:47:49

标签: python api yelp

我试图从Yelp API获取访问令牌并阅读开发人员页面中的说明以及请求文档,当我尝试发出请求时,它会返回{"error": {"code": "VALIDATION_ERROR", "description": "/oauth2/token/"}}我在Yelp开发者页面上查找了错误代码,但没有找到此错误代码。

import json
from pip._vendor import requests
clientID= 'my id as a string'
clientSecret ='my secret as a string'
par = {'grant_type' : 'client_credentials', 'client_id':clientID,'client_secret':clientSecret}
content = requests.post('https://api.yelp.com/oauth2/token/',params=par)
print(content.text)

有人可以帮我看看有什么不对吗?提前谢谢。

1 个答案:

答案 0 :(得分:1)

请参阅以下代码。这绝对是正确的,并且正在运行示例代码。

import requests

app_id = 'client_id'
app_secret = 'client_secret'
data = {'grant_type': 'client_credentials',
        'client_id': app_id,
        'client_secret': app_secret}
token = requests.post('https://api.yelp.com/oauth2/token', data=data)
access_token = token.json()['access_token']
url = 'https://api.yelp.com/v3/businesses/search'
headers = {'Authorization': 'bearer %s' % access_token}
params = {'location': 'San Bruno',
          'term': 'Japanese Restaurant',
          'pricing_filter': '1, 2',
          'sort_by': 'rating'
         }

resp = requests.get(url=url, params=params, headers=headers)

import pprint
pprint.pprint(resp.json()['businesses'])