【问题标题】:How can i upload video on specific channel using YouTube API in PHP?如何使用 PHP 中的 YouTube API 在特定频道上上传视频?
【发布时间】:2017-02-12 04:19:18
【问题描述】:

我想让用户无需身份验证即可在我的频道上上传视频(如果需要)。有可能吗?

请帮帮我。

谢谢 嗯

【问题讨论】:

    标签: php youtube access-token youtube-data-api


    【解决方案1】:

    是的,Omprakash,这是可能的

    1. 您需要适用于 PHP 的 Google API 客户端库。
    2. 您还需要在 https://console.developers.google.com/ 并获取凭据(即 客户端密码和客户端 ID)。
    3. 最后,您需要为特定频道生成访问令牌。 请看一下这个链接 (https://youtube-eng.googleblog.com/2013/06/google-page-identities-and-youtube-api_24.html) 生成访问令牌。
    4. 一旦你准备好所有这些东西,你就可以使用现成的了 Google API 客户端库中可供 PHP 上传的示例代码 YouTube 上的视频。

    注意:这不是详细的过程。在stack-overflow上不可能详细解释所有的过程。但是,一旦您接近解决方案,您可以重新发布或发表评论以获得进一步的帮助。

    这是在 YouTube 上上传视频的示例代码。希望对你有帮助

    /*include google libraries */
    require_once '../api/src/Google/autoload.php';
    require_once '../api/src/Google/Client.php';
    require_once '../api/src/Google/Service/YouTube.php';
    
    $application_name = 'Your application/project name created on google developer console'; 
    $client_secret = 'Your client secret';
    $client_id = 'Your client id';
    $scope = array('https://www.googleapis.com/auth/youtube.upload', 'https://www.googleapis.com/auth/youtube', 'https://www.googleapis.com/auth/youtubepartner');
    
    
        try{ 
            $key = file_get_contents('the_key.txt'); //it stores access token obtained in step 3
            $videoPath = 'video path on your server goes here';
            $videoTitle = 'video title';
            $videoDescription = 'video description';
            $videoCategory = "22"; //please take a look at youtube video categories for videoCategory.Not so important for our example
            $videoTags = array('tag1', 'tag2','tag3');  
    
            // Client init
            $client = new Google_Client();
            $client->setApplicationName($application_name);
            $client->setClientId($client_id);
            $client->setAccessType('offline');
            $client->setAccessToken($key);
            $client->setScopes($scope);
            $client->setClientSecret($client_secret);
    
            if ($client->getAccessToken()) {
                /**
                 * Check to see if our access token has expired. If so, get a new one and save it to file for future use.
                 */
                if($client->isAccessTokenExpired()) {
                    $newToken = json_decode($client->getAccessToken());
                    $client->refreshToken($newToken->refresh_token);
                    file_put_contents('the_key.txt', $client->getAccessToken());
                }
    
                $youtube = new Google_Service_YouTube($client);
    
    
    
                // Create a snipet with title, description, tags and category id
                $snippet = new Google_Service_YouTube_VideoSnippet();
                $snippet->setTitle($videoTitle);
                $snippet->setDescription($videoDescription);
                $snippet->setCategoryId($videoCategory);
                $snippet->setTags($videoTags);
    
                // Create a video status with privacy status. Options are "public", "private" and "unlisted".
                $status = new Google_Service_YouTube_VideoStatus();
                $status->setPrivacyStatus('public');
    
                // Create a YouTube video with snippet and status
                $video = new Google_Service_YouTube_Video();
                $video->setSnippet($snippet);
                $video->setStatus($status);
    
                // Size of each chunk of data in bytes. Setting it higher leads faster upload (less chunks,
                // for reliable connections). Setting it lower leads better recovery (fine-grained chunks)
                $chunkSizeBytes = 1 * 1024 * 1024;
    
                // Setting the defer flag to true tells the client to return a request which can be called
                // with ->execute(); instead of making the API call immediately.
                $client->setDefer(true);
    
                // Create a request for the API's videos.insert method to create and upload the video.
                $insertRequest = $youtube->videos->insert("status,snippet", $video);
    
                // Create a MediaFileUpload object for resumable uploads.
                $media = new Google_Http_MediaFileUpload(
                    $client,
                    $insertRequest,
                    'video/*',
                    null,
                    true,
                    $chunkSizeBytes
                );
                $media->setFileSize(filesize($videoPath));
    
    
                // Read the media file and upload it chunk by chunk.
                $status = false;
                $handle = fopen($videoPath, "rb");
                while (!$status && !feof($handle)) {
                    $chunk = fread($handle, $chunkSizeBytes);
                    $status = $media->nextChunk($chunk);
                }
    
                fclose($handle);
    
                /**
                 * Video has successfully been upload, now lets perform some cleanup functions for this video
                 */
                if ($status->status['uploadStatus'] == 'uploaded') {
                    $youtube_id = $status->id; //you got here youtube video id 
                } else {
                    // handle failere here
                }
    
                // If you want to make other calls after the file upload, set setDefer back to false
                $client->setDefer(true);
    
            } else{
                // @TODO Log error
                echo 'Problems creating the client';
            }
    
        } catch(Google_Service_Exception $e) {
            echo  "\r\n Caught Google service Exception ".$e->getCode(). " message is ".$e->getMessage();
            echo "\r\n Stack trace is ".$e->getTraceAsString(); 
        } catch (Exception $e) {
            echo  "\r\n Caught Google service Exception ".$e->getCode(). " message is ".$e->getMessage();
            echo  "\r\n Stack trace is ".$e->getTraceAsString(); 
        }
    

    【讨论】:

    • 我已经完成了您提到的以下步骤:- 1) 2) 3) 4) 我的视频已成功上传到 Youtube,但我想上传到特定频道或特定帐户。现在用户在他们的 youtube 帐户上上传视频。
    • 第 3 步中提到了访问令牌。在 YouTube 上上传视频需要访问令牌。您正在从用户那里收集此访问令牌。这就是视频被上传到他们频道的原因。相反,您应该创建自己的访问令牌,如步骤 3 中提供的链接中所述。此访问令牌会在一段时间后(即 1 小时)过期。您需要定期刷新它。
    • 非常感谢 Dinesh,让我按照步骤 3 中的给定链接尝试
    • @Dinesh Belakare 您在此处指定将视频上传到特定频道。就像一个用户有多个频道,并且用户想在频道一上上传视频。怎么可能?
    • @Ranjit.. 在上述过程中,频道所有者访问令牌(ontime)是手动生成的,并存储在单独的文件(“the_key.txt”)中。在生成此访问令牌时,您必须选择特定的通道。因此,代码不处理频道选择,它是“访问令牌”,它负责在上传视频时选择哪个频道。如果您有任何疑问,请告诉我。
    猜你喜欢
    • 2013-02-03
    • 2020-11-08
    • 1970-01-01
    • 2017-08-08
    • 2015-01-10
    • 1970-01-01
    • 2015-08-01
    • 2015-11-19
    • 2017-05-16
    相关资源
    最近更新 更多