簡體   English   中英

對Yelp API進行身份驗證時,我在本地Python服務器上收到內部服務器錯誤

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

我正在嘗試通過Yelp API進行身份驗證,這就是我得到的:

{“錯誤”:{“說明”:“找不到client_id或client_secret參數。請確保在主體中為application_x-www-form-urlencoded內容類型提供client_id和client_secret”,“代碼”:“ 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作為參數,它屬於請求標頭
最終代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM