【问题标题】:Retrieving videos via the YouTube API returns nothing通过 YouTube API 检索视频不会返回任何内容
【发布时间】:2014-04-12 17:58:22
【问题描述】:

我正在尝试检索一些 YouTube 视频,我正在使用来自 http://www.w3resource.com/API/youtube/tutorial.php 的脚本

我已将推荐人设置为

mydomain.com/

https://console.developers.google.com/project

表单显示,但只要我搜索什么,什么都没有返回,只是一个空白页。

是否应将引荐来源网址设置为脚本的完整路径?

    <?php
if ($_GET['q'] && $_GET['maxResults']) {
  // Call set_include_path() as needed to point to your client library.
  require_once ('Google_Client.php');
  require_once ('Google_YouTubeService.php');

  /* Set $DEVELOPER_KEY to the "API key" value from the "Access" tab of the
  Google APIs Console <http://code.google.com/apis/console#access>
  Please ensure that you have enabled the YouTube Data API for your project. */
  $DEVELOPER_KEY = '*****';

  $client = new Google_Client();
  $client->setDeveloperKey($DEVELOPER_KEY);

  $youtube = new Google_YoutubeService($client);

  try {
    $searchResponse = $youtube->search->listSearch('id,snippet', array(
      'q' => $_GET['q'],
      'maxResults' => $_GET['maxResults'],
    ));

    $videos = '';
    $channels = '';

    foreach ($searchResponse['items'] as $searchResult) {
      switch ($searchResult['id']['kind']) {
        case 'youtube#video':
          $videos .= sprintf('<li>%s (%s)</li>', $searchResult['snippet']['title'],
            $searchResult['id']['videoId']."<a href=http://www.youtube.com/watch?v=".$searchResult['id']['videoId']." target=_blank>   Watch This Video</a>");
          break;
        case 'youtube#channel':
          $channels .= sprintf('<li>%s (%s)</li>', $searchResult['snippet']['title'],
            $searchResult['id']['channelId']);
          break;
       }
    }

   } catch (Google_ServiceException $e) {
    $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',
      htmlspecialchars($e->getMessage()));
  } catch (Google_Exception $e) {
    $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>',
      htmlspecialchars($e->getMessage()));
  }
}
?>

<!doctype html>
<html>
  <head>
    <title>YouTube Search</title>
<link href="//www.w3resource.com/includes/bootstrap.css" rel="stylesheet">
<style type="text/css">
body{margin-top: 50px; margin-left: 50px}
</style>
  </head>
  <body>
    <form method="GET">
  <div>
    Search Term: <input type="search" id="q" name="q" placeholder="Enter Search Term">
  </div>
  <div>
    Max Results: <input type="number" id="maxResults" name="maxResults" min="1" max="50" step="1" value="25">
  </div>
  <input type="submit" value="Search">
</form>
<h3>Videos</h3>
    <ul><?php echo $videos; ?></ul>
    <h3>Channels</h3>
    <ul><?php echo $channels; ?></ul>
</body>
</html>

我的 API 密钥来自:

【问题讨论】:

    标签: php youtube-api


    【解决方案1】:

    您从哪里获得“API 密钥”?我在开发者控制台中没有看到“访问”选项卡。 https://developers.google.com/youtube 的所有示例都显示了设置客户端 ID、客户端密码和范围。

    【讨论】:

    • 嗨,我已经编辑了我的原始帖子以显示我从哪里获取 API 密钥。
    【解决方案2】:

    Stack Overflow 不允许我编辑我的答案,所以我正在创建一个新答案。本教程似乎在使用 OAuth 客户端 ID/秘密和 API 密钥之间来回切换。 AFAIK,您需要一个客户 ID/密码才能访问您登录 YouTube 所需的信息。您使用 API 密钥访问公共信息。对于您使用的示例,它仅列出公共信息,因此它使用 API 密钥。

    要创建 API 密钥,请单击公共 API 访问下的创建新密钥按钮,选择服务器密钥并输入您的 Web 服务器的 IP 地址。这为您提供了一个 API 密钥,您在脚本中将其设置为 DEVELOPER_KEY。我遵循了该程序,示例脚本运行良好。

    【讨论】:

      猜你喜欢
      • 2015-04-17
      • 2016-10-21
      • 1970-01-01
      • 1970-01-01
      • 2013-09-11
      • 1970-01-01
      • 2012-02-10
      • 2015-07-23
      • 2022-11-07
      相关资源
      最近更新 更多