尝试使用YouTube API时需要登录才会出错

时间:2016-04-19 18:35:52

标签: php youtube youtube-api google-api-php-client youtube-data-api

我正在尝试设置一个可以通过YouTube API创建实时事件的脚本,我已经从developer console中取出了示例并尝试修改它以使用API​​密钥,因为它将转到我们的自己的帐户,而不是用户使用自己的帐户。

另外,仅供参考我正在使用Google API PHP Client V1

我有以下代码:

require_once 'includes/Google/autoload.php';
require_once 'includes/Google/Client.php';
require_once 'includes/Google/Service/YouTube.php';

$client = new Google_Client();
$client->setApplicationName("My Title");
$client->setDeveloperKey("xxxxxxxxxxxxxx");

$client->setScopes('https://www.googleapis.com/auth/youtube');

// Define an object that will be used to make all API requests.
$youtube = new Google_Service_YouTube($client);

try {
    // Create an object for the liveBroadcast resource's snippet. Specify values
    // for the snippet's title, scheduled start time, and scheduled end time.
    $broadcastSnippet = new Google_Service_YouTube_LiveBroadcastSnippet();
    $broadcastSnippet->setTitle('Test Broadcast');
    $broadcastSnippet->setScheduledStartTime('2017-01-30T00:00:00.000Z');
    $broadcastSnippet->setScheduledEndTime('2017-01-31T00:00:00.000Z');

    // Create an object for the liveBroadcast resource's status, and set the
    // broadcast's status to "private".
    $status = new Google_Service_YouTube_LiveBroadcastStatus();
    $status->setPrivacyStatus('unlisted');

    // Create the API request that inserts the liveBroadcast resource.
    $broadcastInsert = new Google_Service_YouTube_LiveBroadcast();
    $broadcastInsert->setSnippet($broadcastSnippet);
    $broadcastInsert->setStatus($status);
    $broadcastInsert->setKind('youtube#liveBroadcast');

    // Execute the request and return an object that contains information
    // about the new broadcast.
    $broadcastsResponse = $youtube->liveBroadcasts->insert('snippet,status', $broadcastInsert, array());

    // Create an object for the liveStream resource's snippet. Specify a value
    // for the snippet's title.
    $streamSnippet = new Google_Service_YouTube_LiveStreamSnippet();
    $streamSnippet->setTitle('Test Stream');

    // Create an object for content distribution network details for the live
    // stream and specify the stream's format and ingestion type.
    $cdn = new Google_Service_YouTube_CdnSettings();
    $cdn->setFormat("1080p");
    $cdn->setIngestionType('rtmp');

    // Create the API request that inserts the liveStream resource.
    $streamInsert = new Google_Service_YouTube_LiveStream();
    $streamInsert->setSnippet($streamSnippet);
    $streamInsert->setCdn($cdn);
    $streamInsert->setKind('youtube#liveStream');

    // Execute the request and return an object that contains information
    // about the new stream.
    $streamsResponse = $youtube->liveStreams->insert('snippet,cdn', $streamInsert, array());

    // Bind the broadcast to the live stream.
    $bindBroadcastResponse = $youtube->liveBroadcasts->bind(
        $broadcastsResponse['id'], 'id,contentDetails',
        array(
            'streamId' => $streamsResponse['id'],
        ));

    $htmlBody .= "<h3>Added Broadcast</h3><ul>";
    $htmlBody .= sprintf('<li>%s published at %s (%s)</li>',
                         $broadcastsResponse['snippet']['title'],
                         $broadcastsResponse['snippet']['publishedAt'],
                         $broadcastsResponse['id']);
    $htmlBody .= '</ul>';

    $htmlBody .= "<h3>Added Stream</h3><ul>";
    $htmlBody .= sprintf('<li>%s (%s)</li>',
                         $streamsResponse['snippet']['title'],
                         $streamsResponse['id']);
    $htmlBody .= '</ul>';

    $htmlBody .= "<h3>Bound Broadcast</h3><ul>";
    $htmlBody .= sprintf('<li>Broadcast (%s) was bound to stream (%s).</li>',
                         $bindBroadcastResponse['id'],
                         $bindBroadcastResponse['contentDetails']['boundStreamId']);
    $htmlBody .= '</ul>';

} catch (Google_Service_Exception $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>Bound Live Broadcast</title>
</head>
<body>
<?= $htmlBody ?>
</body>
</html>

但是当我运行它时,我收到错误:

A service error occurred: Error calling POST https://www.googleapis.com/youtube/v3/liveBroadcasts?part=snippet%2Cstatus&key=xxxxxxxxxxxxxxxx: (401) Login Required

我在这里做错了什么?

2 个答案:

答案 0 :(得分:1)

此操作只能由通过OAuth2进行身份验证的用户执行,您无法仅使用开发人员密钥创建广播。在这种情况下,您希望使用自己的帐户,因此您需要获取对该帐户有效的访问令牌,最好是&#34;离线&#34;一,所以你可以在它过期后自动刷新它,这样你只需要手动浏览一次同意屏幕。以下是关于OAuth 2流程的更多内容:

https://developers.google.com/youtube/v3/guides/auth/server-side-web-apps

编辑:更确切地说,流程应该是这样的:

  1. 在单独的脚本中,会完成获取离线访问令牌的过程 - 点击同意屏幕,并代表您的帐户授予您的应用访问API的权限。
  2. 存储获取的令牌供以后使用,例如将其保存在平面文件或数据库中。您只需要执行前两个步骤,稍后您的令牌可以自动刷新。
  3. 用户进入该站点,您的脚本使用先前获取的令牌执行API操作。一切都发生在服务器端,没有用户的输入。

答案 1 :(得分:0)

您需要使用Oauth2服务进行身份验证,您可以使用此示例阅读更多代码:https://developers.google.com/youtube/v3/code_samples/php

试试这个:     

// Call set_include_path() as needed to point to your client library.
require_once 'includes/Google/autoload.php';
require_once 'includes/Google/Client.php';
require_once 'includes/Google/Service/YouTube.php';
session_start();

/*
 * You can acquire an OAuth 2.0 client ID and client secret from the
 * Google Developers Console <https://console.developers.google.com/>
 * For more information about using OAuth 2.0 to access Google APIs, please see:
 * <https://developers.google.com/youtube/v3/guides/authentication>
 * Please ensure that you have enabled the YouTube Data API for your project.
 */
$OAUTH2_CLIENT_ID = 'REPLACE_ME';
$OAUTH2_CLIENT_SECRET = 'REPLACE_ME';

$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->setScopes('https://www.googleapis.com/auth/youtube');
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
    FILTER_SANITIZE_URL);
$client->setRedirectUri($redirect);

// Define an object that will be used to make all API requests.
$youtube = new Google_Service_YouTube($client);

if (isset($_GET['code'])) {
  if (strval($_SESSION['state']) !== strval($_GET['state'])) {
    die('The session state did not match.');
  }

  $client->authenticate($_GET['code']);
  $_SESSION['token'] = $client->getAccessToken();
  header('Location: ' . $redirect);
}

if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
}

// Check to ensure that the access token was successfully acquired.
if ($client->getAccessToken()) {
  try{

    // Create an object for the liveBroadcast resource's snippet. Specify values
    // for the snippet's title, scheduled start time, and scheduled end time.
    $broadcastSnippet = new Google_Service_YouTube_LiveBroadcastSnippet();
    $broadcastSnippet->setTitle('Test Broadcast');
    $broadcastSnippet->setScheduledStartTime('2017-01-30T00:00:00.000Z');
    $broadcastSnippet->setScheduledEndTime('2017-01-31T00:00:00.000Z');

    // Create an object for the liveBroadcast resource's status, and set the
    // broadcast's status to "private".
    $status = new Google_Service_YouTube_LiveBroadcastStatus();
    $status->setPrivacyStatus('unlisted');

    // Create the API request that inserts the liveBroadcast resource.
    $broadcastInsert = new Google_Service_YouTube_LiveBroadcast();
    $broadcastInsert->setSnippet($broadcastSnippet);
    $broadcastInsert->setStatus($status);
    $broadcastInsert->setKind('youtube#liveBroadcast');

    // Execute the request and return an object that contains information
    // about the new broadcast.
    $broadcastsResponse = $youtube->liveBroadcasts->insert('snippet,status', $broadcastInsert, array());

    // Create an object for the liveStream resource's snippet. Specify a value
    // for the snippet's title.
    $streamSnippet = new Google_Service_YouTube_LiveStreamSnippet();
    $streamSnippet->setTitle('Test Stream');

    // Create an object for content distribution network details for the live
    // stream and specify the stream's format and ingestion type.
    $cdn = new Google_Service_YouTube_CdnSettings();
    $cdn->setFormat("1080p");
    $cdn->setIngestionType('rtmp');

    // Create the API request that inserts the liveStream resource.
    $streamInsert = new Google_Service_YouTube_LiveStream();
    $streamInsert->setSnippet($streamSnippet);
    $streamInsert->setCdn($cdn);
    $streamInsert->setKind('youtube#liveStream');

    // Execute the request and return an object that contains information
    // about the new stream.
    $streamsResponse = $youtube->liveStreams->insert('snippet,cdn', $streamInsert, array());

    // Bind the broadcast to the live stream.
    $bindBroadcastResponse = $youtube->liveBroadcasts->bind(
        $broadcastsResponse['id'], 'id,contentDetails',
        array(
            'streamId' => $streamsResponse['id'],
        ));

    $htmlBody .= "<h3>Added Broadcast</h3><ul>";
    $htmlBody .= sprintf('<li>%s published at %s (%s)</li>',
                         $broadcastsResponse['snippet']['title'],
                         $broadcastsResponse['snippet']['publishedAt'],
                         $broadcastsResponse['id']);
    $htmlBody .= '</ul>';

    $htmlBody .= "<h3>Added Stream</h3><ul>";
    $htmlBody .= sprintf('<li>%s (%s)</li>',
                         $streamsResponse['snippet']['title'],
                         $streamsResponse['id']);
    $htmlBody .= '</ul>';

    $htmlBody .= "<h3>Bound Broadcast</h3><ul>";
    $htmlBody .= sprintf('<li>Broadcast (%s) was bound to stream (%s).</li>',
                         $bindBroadcastResponse['id'],
                         $bindBroadcastResponse['contentDetails']['boundStreamId']);
    $htmlBody .= '</ul>';
    } catch (Google_Service_Exception $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()));
    }

    $_SESSION['token'] = $client->getAccessToken();
  } else {
    // If the user hasn't authorized the app, initiate the OAuth flow
    $state = mt_rand();
    $client->setState($state);
    $_SESSION['state'] = $state;

    $authUrl = $client->createAuthUrl();
    $htmlBody = " <h3>Authorization Required</h3>
    <p>You need to <a href="$authUrl">authorize access</a> before proceeding.<p>
    "
    }
?>

<!doctype html>
<html>
<head>
<title>Banner Uploaded and Set</title>
</head>
<body>
  <?=$htmlBody?>
</body>
</html>