如何使用Yelp API从v2迁移到v3?

问题描述 投票:4回答:2

我有一个与v2配合得很好的脚本,但是当它过期并转移到v3时就破了。

我试图解决它,但显然还有更多,然后只是将v2改为v3。显然他们已经弃用了秘密令牌。

这就是我现在所拥有的:

// 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>';

向正确的方向推动将受到高度赞赏。


我收到一个错误:

stdClass对象([error] => stdClass对象([code] => TOKEN_MISSING [描述] =>必须提供访问令牌才能使用此端点。))

我是否需要为v3重新生成我的API凭据?

php api yelp yelp-fusion-api
2个回答
4
投票

来自您的问题的Citate:我是否需要为v3重新生成我的API凭据?

没有!您无需重新生成API凭据,因为您不再需要它们。但是你需要生成一个新的API密钥。

来自Yelp API v3 documentation:的Citate ...从2018年3月1日开始,API不再将OAuth 2.0用于请求,而是仅移至API密钥。

使用API​​密钥,进行身份验证的过程是:

  • Manage App page获取您的API密钥。
  • 将API密钥放在请求标头中作为"Authorization: Bearer <YOUR API KEY>"

就是这样!您不再需要向令牌端点发出请求以获取访问令牌。您的API密钥不会像过去使用的访问令一样过期,因此您无需担心生成新密钥。

但请注意,在开始API Key生成之前(请参阅上面的最后一个链接):

  1. 您必须由yelp.com登录。如果有人在那里没有帐户,那么他必须在那里注册并确认他的电子邮件地址。
  2. 必须启用浏览器中的JavaScript。在其他情况下,您将被重定向到非常奇怪的异常页面。

来自你的赏金描述的Citate:需要一个Yelp API v3的工作示例,通过电话返回搜索业务的结果。

Yelp API v3通过电话返回搜索业务结果的示例

<?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>";
?>

我测试了它,它的工作原理。


2
投票

看起来您正在使用OAuth,根据V3的yelp开发人员文档,他们转移到基于API密钥的身份验证。

在2017年12月7日之前,API使用OAuth 2.0来验证对API的请求。为了简化身份验证,从2018年3月1日开始,API不再将OAuth 2.0用于请求,而是仅移至API密钥。

您可以在https://www.yelp.com/developers/documentation/v3/authentication#where-is-my-client-secret-going找到身份验证详细信息

© www.soinside.com 2019 - 2024. All rights reserved.