【问题标题】:YouTube Data API failed to open stream: HTTP request failed! HTTP/1.0 403 ForbiddenYouTube 数据 API 无法打开流:HTTP 请求失败! HTTP/1.0 403 禁止
【发布时间】:2013-09-03 13:17:45
【问题描述】:

已设置 API KEY,将我的域添加到允许的引荐来源,并尝试使用 PHP 调用 YouTube V3 API,如下所示:

file_get_contents("https://www.googleapis.com/youtube/v3/search?part=snippet&q=my_search_query&type=video&key=my_application_key") 

但是

打开流失败:HTTP 请求失败! HTTP/1.0 403 禁止

我是否遗漏了一些明显的东西?

【问题讨论】:

    标签: php youtube youtube-api


    【解决方案1】:

    发现可以生成多个不同的 API 密钥。默认是“浏览器 api 密钥”,但我需要在我的服务器上运行 PHP(当然)是一个“服务器 api 密钥”。

    我进行了设置,并将我的服务器 IP 列入白名单。

    问题解决了。

    【讨论】:

      【解决方案2】:

      您的网络服务器是否支持file_get_contents?由于安全风险,许多 Web 服务器不支持此功能。您是否考虑过使用 curl 代替?

      可能是这样的:

      $option = array(
              'part' => 'snippet', 
              'q' => 'search_query',
              'type' => 'video',
              'key' => 'your_key'
          );
      $url = "https://www.googleapis.com/youtube/v3/search?".http_build_query($option, 'a', '&');
      $curl = curl_init($url);
      
      curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  
      curl_setopt($curl, CURLOPT_HTTPHEADER, false);
      
      $json_response = curl_exec($curl);
      
      curl_close($curl);
      
      $responseObj = json_decode($json_response);
      

      【讨论】:

      • 有一个 ?在应用程序的查询字符串中。似乎没有正确粘贴到 StackOverflow 中。所以还是有同样的问题。
      • 我没有错过 ?在查询字符串中。我还是有同样的问题,?呈现并正确。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-09
      • 1970-01-01
      • 2020-08-06
      • 2014-12-14
      相关资源
      最近更新 更多