gpt4 book ai didi

php - 尝试使用 YouTube API 时出现需要登录错误

转载 作者:行者123 更新时间:2023-12-03 05:29:09 27 4
gpt4 key购买 nike

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

另外,仅供引用,我正在使用 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

我在这里做错了什么?

最佳答案

此操作只能由通过 OAuth2 身份验证的用户执行,您不能仅使用您的开发人员 key 创建广播。在这种情况下,您想使用自己的帐户,因此您需要获取对该帐户有效的访问 token ,最好是“离线” token ,这样您就可以在过期后自动刷新它,这样您就可以只需手动通过同意屏幕一次。以下是有关 OAuth 2 流程的更多信息:

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

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

  • 在单独的脚本中,完成获取离线访问 token 的过程 - 单击同意屏幕并代表您的帐户授予您的应用程序访问 API 的权限。
  • 存储获得的 token 以供以后使用,例如将其保存在平面文件或数据库中。您只需要执行前两个步骤一次,稍后您的 token 可以自动刷新。
  • 用户进入站点,您的脚本使用之前获得的 token 执行 API 操作。一切都发生在服务器端,无需用户输入。
  • 关于php - 尝试使用 YouTube API 时出现需要登录错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36726734/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com