Neumayer
Neumayer

Reputation: 11

How to get a Yelp Fusion API access token?

I'm using the Yelp's Fusion API.

All the documentation is here and seems straight forward but still doesn't seem to work.

https://www.yelp.com/developers/documentation/v3/get_started

Here is my request for a token.

https://api.yelp.com/oauth2/token?grant_type= OAuth2&client_secret= SECRET&client_id=ID

I receive this response.

{
  "error": {
    "description": "Bad Request",
    "code": "CLIENT_ERROR"
  }
}

I reread the documentation and it says

"To get an access token, make a POST call to the following endpoint using the client id and secret obtained from the former step. Then get the access token from the response body."

I submitted this

https://api.yelp.com/oauth2/token?client_id=ID&client_secret= SECRET

I got the same error.

What am I missing?

Thank you in advance

Upvotes: 1

Views: 4781

Answers (2)

Jack Lyons
Jack Lyons

Reputation: 11

I used POSTMAN to get the data required, you can download it here - https://www.getpostman.com/

In order to obtain your access token the parameters should be in the request body, not the request URL.


In POSTMAN you need to provide the following as key/value pairs:

grant_type : client_credentials is supported.
client_id : The client id for you app with Yelp.
client_secret : The client secret for you app with Yelp.

To do this, you enter them in the Body. Also click the x-www-form-urlencoded radio button.
See the screenshot link below:
How to configure POSTMAN to get your Yelp Access Token


You can then play around over at RapidAPI Yelp API to test some endpoints and parameters.

Upvotes: 1

Patrick Evans
Patrick Evans

Reputation: 42736

You have three problems that are causing you to get the bad request

  1. You have spaces in your parameter values

    ?grant_type= OAuth2
    
  2. You are using the wrong grant_type. On the documentation page they specify that only client_credentials is supported, meaning you have to use that as the value

    grant_type string The OAuth2 grant type to use. Right now, only client_credentials is supported.

  3. You are using the wrong request method, it has to be a POST not a GET request

    These parameters should be sent in application/x-www-form-urlencoded format in the POST call.

    Note that https://api.yelp.com/oauth2/token?client_id=ID&client_secret=SECRET is still sending your parameters as GET parameters as they are in the url query string. You have to pass the parameters as POST fields, and the syntax for doing so differs on the server side script language you use.

Also your request needs to be done server side as the /oauth2/token endpoint does not send a Access-Control-Allow-Origin header meaning you cannot use an ajax request to get the data.

So if say you were using PHP server side, you could use CURL to get the token

$postData = "grant_type=client_credentials&".
            "client_id=YOURCLIENTID&".
            "client_secret=SECRET";
$ch = curl_init();

//set the url
curl_setopt($ch,CURLOPT_URL, "https://api.yelp.com/oauth2/token");
//tell curl we are doing a post
curl_setopt($ch,CURLOPT_POST, TRUE);
//set post fields
curl_setopt($ch,CURLOPT_POSTFIELDS, $postData);
//tell curl we want the returned data
curl_setopt($ch,CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);

//close connection
curl_close($ch);

if($result){
   $data = json_decode($result);
   echo "Token: ".$data->access_token;
}

Upvotes: 1

Related Questions