向 Yelp API 进行身份验证时,我的本地 Python 服务器出现内部服务器错误

I am getting an internal server error on my local Python server when authenticating to Yelp API

我正在尝试向 Yelp API 进行身份验证,这就是我得到的结果:

{ "error": { "description": "client_id or client_secret parameters not found. Make sure to provide client_id and client_secret in the body with the 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)

数据应该作为application/x-www-form-urlencoded传递,所以你不应该序列化请求参数。你也不应该指定 Content-Type 作为参数,它属于请求 headers.
最终代码:

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

我按照@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)