使用Youtube Data API V3和Google API客户端PHP将视频上传到Youtube——获得401(未


Uploading video to Youtube using Youtube Data API V3 and Google API Client PHP -- getting 401 (unauthorized) message

我必须实现一种将视频从我们的网站上传到youtube的方法。我已经在谷歌云中注册了该应用程序,并获得了所有必要的客户端ID、客户端密码、浏览器密钥、重定向uri和服务器密钥。我还打开了Youtube Data API V3、Google+API、Freebase API和Youtube Analytics API,正如各个网站所建议的那样。

下面是我的代码:

    require_once 'google-api-php-client/src/Google_Client.php';
    require_once 'google-api-php-client/src/contrib/Google_YouTubeService.php';
    $client = new Google_Client();
    $client->setApplicationName('Application Name');
    $client->setClientId('CLIENT_ID');
    $client->setClientSecret('CLIENT_SECRET_CODE');
    $client->setRedirectUri('REDIRECT_URI');
    $client->setDeveloperKey('DEVELOPER_KEY');
    $youtube = new Google_YouTubeService($client);
    if (isset($_GET['code'])) {
        $client->authenticate();
        $_SESSION['token'] = $client->getAccessToken();
        $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
        header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
    }
    if (isset($_SESSION['token'])){
        $client->setAccessToken($_SESSION['token']);
    }
    if ($client->getAccessToken()){
        $path_to_video_to_upload = $_FILES["video"]["tmp_name"];
        $mime_type = $_FILES["video"]["type"];
        $snippet = new Google_VideoSnippet();
        $snippet->setTitle('Highlights');
        $snippet->setDescription('Description Of Video');
        $snippet->setTags(array('training', 'Soccer'));
        $snippet->setCategoryId(17);
        $status = new Google_VideoStatus();
        $status->privacyStatus = "private";
        $video = new Google_Video();
        $video->setSnippet($snippet);
        $video->setStatus($status);
        try {
            $obj = $youtube->videos->insert("status,snippet", $video,
            array("data"=>file_get_contents($path_to_video_to_upload), 
            "mimeType" => $mime_type));
            } catch(Google_ServiceException $e) {
            print "Caught Google service Exception ".$e->getCode(). " message is ".$e->getMessage()." <br>";
            }
    $_SESSION['token'] = $client->getAccessToken();
    }
    else{
            $authUrl = $client->createAuthUrl();
            print "<a href='$authUrl'>Connect Me!</a>";
    }

我引用了以下代码:使用Youtube API V3和PHP 将视频上传到Youtube

http://www.dreu.info/blog/uploading-a-video-to-youtube-through-api-version-3-in-php/

我第一次运行这个代码时,它让我连接到youtube帐户(屏幕上有一个要求让谷歌管理我的YT帐户的屏幕),然后在第二次尝试时,我收到了这个401消息响应。

以下是来自youtube服务器的回复

捕获谷歌服务异常401消息是调用POST时出错https://www.googleapis.com/upload/youtube/v3/videos?part=status%2Csnippet&uploadType=多部分&key=AIzaSyCCySI5cToH3sICTmhCEFHW7QkIDptsjXg:(401)未经授权的

我试着寻找各种解决方案,但似乎找不到。感谢您的帮助!

后续问题:开发者密钥是浏览器密钥还是服务器密钥?我很困惑我应该在代码中使用哪一个。我试过换钥匙,但也没用。

我遇到了完全相同的问题。

检查您的youtube帐户。你创建了一个频道吗?如果你不这样做,你将无法添加视频(通过API甚至在youtube)。

在youtube上创建频道后,它就工作了。不需要新代码或新密钥。

对于那些遇到Access Not Configured. Please use Google Developers Console to activate the API for your project问题的人,

justine1234的代码运行得非常好。只需删除

$client->setDeveloperKey('DEVELOPER_KEY');

并运行它。

非常感谢代码@justine1234。

问候