【问题标题】:How to migrate with Yelp API from v2 to v3?如何使用 Yelp API 从 v2 迁移到 v3?
【发布时间】:2019-02-05 21:23:35
【问题描述】:

我有一个脚本在 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 Object ([error] => stdClass Object ([code] => TOKEN_MISSING [description] => 必须提供访问令牌才能使用此端点。))

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

【问题讨论】:

  • 怎么了?您是否遇到特定错误?
  • 是的,我得到:stdClass Object ( [error] => stdClass Object ( [code] => TOKEN_MISSING [description] => 必须提供访问令牌才能使用此端点。 ) ) 我需要为 v3 重新生成我的 API 凭据吗?
  • 你能说出错误是在哪一行引发的/什么命令触发它?

标签: php api yelp yelp-fusion-api


【解决方案1】:

您似乎正在使用 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找到身份验证详细信息

【讨论】:

  • 我意识到 Yelp 已经改变了他们的身份验证程序。上面的代码显示了我的 API v2 代码。 Yelp 文档的问题在于,他们写的内容发生了变化,却没有包含一行代码来演示。对于不经常处理 API 的人来说,这非常令人困惑。
  • 文档很简单。尝试刷新您的密钥。然后再试一次。它可以通过删除代码的 Oauth 部分来工作
【解决方案2】:

引用您的问题: 我需要为 v3 重新生成我的 API 凭据吗?

不!您不需要重新-生成您的 API 凭据,因为您不再需要它们。但是你需要生成一个新的——API Key。

引用Yelp API v3 documentation:

... 从 2018 年 3 月 1 日开始,API 不再使用 OAuth 2.0 请求并移至 API Keys

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

  • Manage App page 获取您的 API 密钥。
  • 将API Key作为"Authorization: Bearer &lt;YOUR API KEY&gt;"放在请求头中。

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

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

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

引用您的赏金描述: 需要一个 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>";
?>

我已经测试过了,它可以工作。

【讨论】:

    猜你喜欢
    • 2015-07-06
    • 1970-01-01
    • 1970-01-01
    • 2011-04-10
    • 1970-01-01
    • 1970-01-01
    • 2015-09-27
    • 2022-10-20
    • 2013-12-30
    相关资源
    最近更新 更多