Search code examples
pythonpython-requestsyelp

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


I am trying to authenticate to the Yelp API and this is what I'm getting:

{ "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" } }

This is the method I have defined in Python and I am have installed Python Flask. I have never worked with an API before this:

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

Solution

  • The data should be passed as application/x-www-form-urlencoded, so you should not serialize request parameters. You also should not specify Content-Type as parameter, it belongs to request headers.
    The final code:

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