Neumayer Neumayer - 12 days ago 28
JSON Question

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

Answer

How to Send an Ajax request to Yelp using the Yelp API 2.0 with JavaScript?

The reason I'm answering my own question is because Yelp fusion didn't work for my application and 2.0 did. There isn't a lot of good information on the topic and I don't want new developers to be as frustrated as I was.

You need the following for the code to work.

Yelp API 2.0 Access

JQuery Library

https://jquery.com/

-OAuth-Signature-js Library

https://github.com/bettiolo/oauth-signature-js/tree/master/dist

```

function nonce_generate() {
  return (Math.floor(Math.random() * 1e12).toString());
}

var yelp_url = 'https://api.yelp.com/v2/' + 'search/';

    var parameters = {
      oauth_consumer_key: 'x', //consumer key
      oauth_token: 'x', // Token
      oauth_nonce: nonce_generate(),
      oauth_timestamp: Math.floor(Date.now()/1000),
      oauth_signature_method: 'HMAC-SHA1',
      oauth_version : '1.0',
      callback: 'cb',
      term:  'tacos',
      location: 'Wheeling,Il'          // This is crucial to include for jsonp implementation in AJAX or else the oauth-signature will be wrong.
    };
                                                                                    //Consumer Secret    //Token Secret
    var encodedSignature = oauthSignature.generate('GET',yelp_url, parameters, 'x', 'x');
    parameters.oauth_signature = encodedSignature;

    var settings = {
      url: yelp_url,
      data: parameters,
      cache: true,                // This is crucial to include as well to prevent jQuery from adding on a cache-buster parameter "_=23489489749837", invalidating our oauth-signature
      dataType: 'jsonp',
      success: function(results) {
        console.log(results);
      },
      error: function() {
        // Do stuff on fail
      }
    };
enter code here
    // Send AJAX query via jQuery library.
    $.ajax(settings);

```

TIP!!!

Script order matters when embedding links in the HTML.

This script order worked for this code.

<script src="lib/jquery-3.1.1.min.js"></script>
<script src="lib/oauth-signature.js"></script>
<script src="app.js"></script>
Comments