4

I have a script that worked great with v2 but broke when it expired and shifted to v3.

I've attempted to fix it but clearly there's more to it then just changing v2 to v3. Apparently they've deprecated secret token.

Here's what I have at the moment:

// Enter the path that the oauth library is in relation to the php file
require_once ('../lib/OAuth.php');

// For example, request business with id 'the-waterboy-sacramento'
 $unsigned_url = "https://api.yelp.com/v3/businesses/search?term=niks-italian-kitchen-bar-austin";

// Set your keys here
$consumer_key = "xxxxxxx";
$consumer_secret = "xxxxxxxxx";
$token = "xxxxxxxx";
$token_secret = "xxxxxxxxxxx";


// Token object built using the OAuth library
$token = new OAuthToken($token, $token_secret);

// Consumer object built using the OAuth library
$consumer = new OAuthConsumer($consumer_key, $consumer_secret);

// Yelp uses HMAC SHA1 encoding
$signature_method = new OAuthSignatureMethod_HMAC_SHA1();

// Build OAuth Request using the OAuth PHP library. Uses the consumer and token object created above.
$oauthrequest = OAuthRequest::from_consumer_and_token($consumer, $token, 'GET', $unsigned_url);

// Sign the request
$oauthrequest->sign_request($signature_method, $consumer, $token);

// Get the signed URL
$signed_url = $oauthrequest->to_url();

echo $signed_url;

// Send Yelp API Call
$ch = curl_init($signed_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch); // Yelp response
curl_close($ch);

// Handle Yelp response data
$response = json_decode($data);

// Print it for debugging
echo '<pre>';
print_r($response);
echo '</pre>';

A nudge in the right direction would be highly appreciated.


I'm getting an error:

stdClass Object ( [error] => stdClass Object ( [code] => TOKEN_MISSING [description] => An access token must be supplied in order to use this endpoint. ) )

Do I need to re-generate my API credentials for v3?

brooksrelyt
  • 3,925
  • 5
  • 31
  • 54
santa
  • 12,234
  • 49
  • 155
  • 255

2 Answers2

4

Citate from your question: Do I need to re-generate my API credentials for v3?

No! You do not need to re-generate your API credentials, because you do not need they anymore. But you need to generate a new one – an API Key.

Citate from Yelp API v3 documentation:

... starting March 1, 2018 the API no longer uses OAuth 2.0 for requests and moved over to only API Keys.

With API Keys the process to authenticate is:

  • Get your API Key from the Manage App page.
  • Put the API Key in the request header as "Authorization: Bearer <YOUR API KEY>".

And that is it! You no longer need to make a request to the token endpoint to get an access token. Your API Key does not expire like the access tokens used to, so you don't need to worry about generating new ones.

But note that before you start with API Key generating (see the last link above):

  1. You must be logged in by yelp.com. If someone does not have an account there then he has to register there and to confirm his email adress.
  2. The JavaScript in your browser must be enabled. In other case you will be redirected to very weird exception page.

Citate from your bounty description: Need a working example of Yelp API v3 returning result of search business by phone.


Example of Yelp API v3 returning result of search business by phone

<?php

// request business by phone number
$request_url = "https://api.yelp.com/v3/businesses/search/phone?phone=+14157492060";
/*
Search for businesses by phone number. It must start with + and include the country code, like +14157492060.
See also https://www.yelp.com/developers/documentation/v3/business_search_phone
Additionly you will see the response body example.
*/

// Your API key:
$api_key = "Your-API-key-GUID"; //replase this string with your API key.

// Send Yelp API call
$ch = curl_init($request_url);
curl_setopt($ch, CURLOPT_HTTPHEADER,
    array(
        "Content-Type: application/json",
        "Authorization: Bearer ".$api_key
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch); // Yelp response
curl_close($ch);

// Handle Yelp response data
$response = json_decode($data);


// Test: get a business on last index number
echo $response->businesses[$response->total - 1]->location->city;

// Print it
$pretty_response = json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
echo "<pre>".$pretty_response."</pre>";
?>

I have tested it and it works.

Bharata
  • 13,509
  • 6
  • 36
  • 50
2

Seems like you are using OAuth, according to the yelp developer documentation for V3 they moved to authentication based on API keys.

Prior to December 7, 2017 the API used OAuth 2.0 to authenticate requests to the API. In an effort to simplify authentication, starting March 1, 2018 the API no longer uses OAuth 2.0 for requests and moved over to only API Keys.

you can find the authentication details at https://www.yelp.com/developers/documentation/v3/authentication#where-is-my-client-secret-going

Christlin Panneer
  • 1,599
  • 2
  • 19
  • 31
  • 1
    I realize that Yelp has changed they authentication procedure. The code above shows my API v2 code. The problem with Yelp's documentation is that they write about what has changed without including a line of code to demonstrate. This is VERY confusing to someone not dealing with APIs frequently. – santa Sep 03 '18 at 13:44
  • 1
    the documentation is straight forward. Try to refresh your key. And try again. It will work by removing the the Oauth part of your code – Christlin Panneer Sep 03 '18 at 13:48