使用简单的 API 键进行 Public API 调用 - Youtube 分析 API
Making Public API Calls with a simple API Key - Youtube Analytics API
我正在尝试使用一个简单的键 public 调用 Youtube 分析 api。这是我的 PHP 代码:
<?
set_include_path('YoutubeAPI/src/Google');
require_once 'autoload.php';
session_start();
$client = new Google_Client();
$client->setApplicationName("xxxxxxxxxxxxxxxxxxxx");
$client->setDeveloperKey('xxxxxxxxxxxxxxxxxxxxxxx');
$youtube = new Google_Service_YouTubeAnalytics($client);
$resp = $youtube->reports->query('channel==UCww2zZWg4Cf5xcRKG-ThmXQ','2014-09-01','2014-09-05','views');
?>
我收到这个错误:
[03-Jul-2015 11:29:26 Europe/Berlin] PHP Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling GET https://www.googleapis.com//youtube/analytics/v1/reports?ids=channel%3D%3DUCww2zZWg4Cf5xcRKG-ThmXQ&start-date=2014-09-01&end-date=2014-09-05&metrics=views&key=xxxxxxxxxxxxxxxxx: (401) Login Required' in /home/optimaje/public_html/statstn/YoutubeAPI/src/Google/Http/REST.php:110
Stack trace:
#0 /home/optimaje/public_html/statstn/YoutubeAPI/src/Google/Http/REST.php(62): Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request), Object(Google_Client))
#1 [internal function]: Google_Http_REST::doExecute(Object(Google_Client), Object(Google_Http_Request))
#2 /home/optimaje/public_html/statstn/YoutubeAPI/src/Google/Task/Runner.php(174): call_user_func_array(Array, Array)
#3 /home/optimaje/public_html/statstn/YoutubeAPI/src/Google/Http/REST.php(46): Google_Task_Runner->run()
#4 /home/optimaje/public_html/statstn/YoutubeAPI/src/Google/Client.php(593): Google_Http_REST::execute(Object(Google_Client), Object(Google in /home/optimaje/public_html/statstn/YoutubeAPI/src/Google/Http/REST.php on line 110
一周以来我一直在寻找解决方案,但我仍然迷路了。错误在哪里?有人可以给我一个有效的示例代码吗?
PS:不想用Oauth2登录
使用 public API 密钥访问 Google APIs 与使用 Oauth2 类似。请记住,这仅适用于不需要身份验证的调用,如果您需要进行身份验证,则需要使用 Oauth2,因为 YouTube API 不支持服务帐户身份验证。
中窃取的代码
$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$apiKey = "<YOUR_API_KEY>"; // Change this line.
// Warn if the API key isn't changed.
if (strpos($apiKey, "<") !== false) {
echo missingApiKeyWarning();
exit;
}
$client->setDeveloperKey($apiKey);
我正在尝试使用一个简单的键 public 调用 Youtube 分析 api。这是我的 PHP 代码:
<?
set_include_path('YoutubeAPI/src/Google');
require_once 'autoload.php';
session_start();
$client = new Google_Client();
$client->setApplicationName("xxxxxxxxxxxxxxxxxxxx");
$client->setDeveloperKey('xxxxxxxxxxxxxxxxxxxxxxx');
$youtube = new Google_Service_YouTubeAnalytics($client);
$resp = $youtube->reports->query('channel==UCww2zZWg4Cf5xcRKG-ThmXQ','2014-09-01','2014-09-05','views');
?>
我收到这个错误:
[03-Jul-2015 11:29:26 Europe/Berlin] PHP Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling GET https://www.googleapis.com//youtube/analytics/v1/reports?ids=channel%3D%3DUCww2zZWg4Cf5xcRKG-ThmXQ&start-date=2014-09-01&end-date=2014-09-05&metrics=views&key=xxxxxxxxxxxxxxxxx: (401) Login Required' in /home/optimaje/public_html/statstn/YoutubeAPI/src/Google/Http/REST.php:110
Stack trace:
#0 /home/optimaje/public_html/statstn/YoutubeAPI/src/Google/Http/REST.php(62): Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request), Object(Google_Client))
#1 [internal function]: Google_Http_REST::doExecute(Object(Google_Client), Object(Google_Http_Request))
#2 /home/optimaje/public_html/statstn/YoutubeAPI/src/Google/Task/Runner.php(174): call_user_func_array(Array, Array)
#3 /home/optimaje/public_html/statstn/YoutubeAPI/src/Google/Http/REST.php(46): Google_Task_Runner->run()
#4 /home/optimaje/public_html/statstn/YoutubeAPI/src/Google/Client.php(593): Google_Http_REST::execute(Object(Google_Client), Object(Google in /home/optimaje/public_html/statstn/YoutubeAPI/src/Google/Http/REST.php on line 110
一周以来我一直在寻找解决方案,但我仍然迷路了。错误在哪里?有人可以给我一个有效的示例代码吗?
PS:不想用Oauth2登录
使用 public API 密钥访问 Google APIs 与使用 Oauth2 类似。请记住,这仅适用于不需要身份验证的调用,如果您需要进行身份验证,则需要使用 Oauth2,因为 YouTube API 不支持服务帐户身份验证。
中窃取的代码$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$apiKey = "<YOUR_API_KEY>"; // Change this line.
// Warn if the API key isn't changed.
if (strpos($apiKey, "<") !== false) {
echo missingApiKeyWarning();
exit;
}
$client->setDeveloperKey($apiKey);