在对yelpapi进行身份验证时,我的本地Python服务器上出现了一个内部服务器错误

2024-04-25 16:45:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试通过Yelp API进行身份验证,这是我得到的:

{“error”:{“description”:“找不到客户端id或客户端密钥参数。确保在主体中使用application/x-www-form-urlencoded内容类型“,”code“:“VALIDATION_ERROR”}}在主体中提供client_id和client_secret

这是我在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)

Tags: runformclientapiidjsonurl客户端
2条回答

我遵循@destiner的方法,并将内容类型添加到头文件中,它起作用了。以下是生成的代码:

@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)

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

@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)

相关问题 更多 >