php - 无法使用Google PHP客户端库通过YouTube API设置水印

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

我正在使用来自GitHub的新Google API PHP客户端库。这是代码:

$client = new Google_Client();
$client->setClientId( 'CLIENTID' );
$client->setClientSecret( 'CLIENTSECRET' );
$client->refreshToken( 'REFRESHTOKEN' );
$youtube = new Google_Service_YouTube( $client );
$channelId = 'UC...';
$imagePath = "./image.jpeg";
$postBody = new Google_Service_YouTube_InvideoBranding();
$postBody->setImageBytes( filesize( $imagePath ) );
$postBody->setTargetChannelId( $channelId );
$imageData = file_get_contents( $imagePath );
$resource = array(
    'data' => $imageData,
    'mimeType' => 'image/jpeg,image/png,application/octet-stream',
    'uploadType' => 'media'
);
$youtube->watermarks->set( $channelId, $postBody, $resource );

但是我有以下异常(exception):
PHP Warning:  file_get_contents(/upload/youtube/v3/watermarks/set?channelId=UC...&uploadType=multipart): failed to open stream: No such file or directory in ....../src/Google/IO/Stream.php on line 109
PHP Fatal error:  Uncaught exception 'Google_IO_Exception' with message 'HTTP Error: Unable to connect' in ....../src/Google/IO/Stream.php:112
Stack trace:
#0 ....../src/Google/Http/REST.php(46): Google_IO_Stream->makeRequest(Object(Google_Http_Request))
#1 ....../src/Google/Client.php(492): Google_Http_REST::execute(Object(Google_Client), Object(Google_Http_Request))
#2 ....../src/Google/Service/Resource.php(194): Google_Client->execute(Object(Google_Http_Request))
#3 ....../src/Google/Service/YouTube.php(2687): Google_Service_Resource->call('set', Array)
#4 .../watermark.php(48): Google_Service_YouTube_Watermarks_Resource->set('UC...', Object(Google_Service_YouTube_InvideoBranding), Array)
#5 {main}
  thrown in ....../src/Google/IO/Stream.php on line 112

类似于“设置缩略图”。我在哪里做错了?

最佳答案

尝试使用Google_Http_MediaFileUpload上传图像文件。

这是一个示例:https://github.com/youtube/api-samples/blob/master/php/upload_thumbnail.php#L78

关于php - 无法使用Google PHP客户端库通过YouTube API设置水印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21004169/

相关文章:

javascript - YouTube API - 嵌入式 iframe 上的事件不起作用

php - 如何通过另一个网络上的浏览器打开laravel应用程序(使用公共(public)IP)

php - 如何将 JSON 编码的数组保存到 MySQL

安卓 : Open google play dialog from my app

javascript - 如何监听youtube iframe的音量变化?

jquery - 无法弄清楚如何在点击时暂停 YouTube 视频

PHP:单选按钮创建?

php - 从一组定义的国家代码中删除 WooCommerce 中的国家

android - 为什么YouTube视频无法在WebView android中播放

javascript - YouTube iframe 嵌入违规 : non-passive event listener to a scroll-blocking 'touchstart' event