在对Yelp API进行身份验证时,我在本地Python服务器上收到内部服务器错误

时间:2017-04-02 00:25:54

标签: python python-requests yelp

我正在尝试对Yelp API进行身份验证,这就是我所获得的:

{"错误":{"说明":"找不到client_id或client_secret参数。确保在body中提供client_id和client_secret,其中包含application / x-www-form-urlencoded content-type"," code":" VALIDATION_ERROR" }}

这是我在Python中定义的方法,我已经安装了Python Flask。在此之前我从未使用过API:

@app.route("/run_post")
def run_post():
   url = "https://api.yelp.com/oauth2/token"
   data = {'grant_type': 'client_credentials',
    'client_id': CLIENT_ID,
    'client_secret': CLIENT_SECRET,
    'Content-type': 'application/x-www-form-urlencoded'}

   body = requests.post(url, data=json.dumps(data))

   return json.dumps(body.json(), indent=4)

2 个答案:

答案 0 :(得分:1)

数据应作为application/x-www-form-urlencoded传递,因此您不应序列化请求参数。您也不应将Content-Type指定为参数,它属于请求标题 最终代码:

@app.route("/run_post")
def run_post():
   url = "https://api.yelp.com/oauth2/token"
   data = {'grant_type': 'client_credentials',
    'client_id': CLIENT_ID,
    'client_secret': CLIENT_SECRET}

   body = requests.post(url, data=data)

   return json.dumps(body.json(), indent=4)

答案 1 :(得分:1)

我关注了@destiner's并将内容类型添加到标题中并且它有效。这是结果代码:

@app.route("/run_post")
def run_post():
  url = "https://api.yelp.com/oauth2/token"
  data = {'grant_type': 'client_credentials',
    'client_id': CLIENT_ID,
    'client_secret': CLIENT_SECRET}
  headers = {'Content-type': 'application/x-www-form-urlencoded'}

body = requests.post(url, data=data, headers=headers)

return json.dumps(body.json(), indent=4)