php - 使用 Youtube Data API V3 和 Google API Client PHP 将视频上传到 Youtube - 获取 401(未经授权)消息

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

我必须实现一种将视频从我们的网站上传到 youtube 的方法。我已经在 Google Cloud 中注册了该应用程序,并获得了所有必要的客户端 ID、客户端密码、浏览器 key 、重定向 uri 和服务器 key 。我还按照各个网站的建议打开了 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>";
    }

我从以下位置引用这些代码:
Upload video to Youtube using Youtube API V3 and PHP



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

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

这是来自 youtube 服务器的响应

捕获的 Google 服务异常 401 消息是 Error calling POST https://www.googleapis.com/upload/youtube/v3/videos?part=status%2Csnippet&uploadType=multipart&key=AIzaSyCCySI5cToH3sICTmhCEFHW7QkIDptsjXg : (401) 未经授权

我试图搜索各种解决方案,但无济于事,我似乎找不到它。
任何帮助表示赞赏,谢谢!

追问:开发者 key 是浏览器 key 还是服务器 key ?我很困惑我应该在我的代码中使用哪个。我试过切换键,它也没有工作。

最佳答案

我有同样的问题。

检查您的 YouTube 帐户。您是否创建了 channel ?如果不这样做,您将无法添加视频(通过 API 甚至在 youtube 中)。

在 youtube 中创建 channel 后,它起作用了。无需新代码或新 key 。

关于php - 使用 Youtube Data API V3 和 Google API Client PHP 将视频上传到 Youtube - 获取 401(未经授权)消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20189688/

相关文章:

javascript - 如果我的搜索查询也是对 INDEX 的 GET,Laravel 5 如何编写 GET 路由到 INDEX?

php - 是否可以在 yii2 的 gridview 的数据列中设置相关模型的标签值

php - 使用 PHP CURL 解析负载较重的站点?

matlab - webread给出的结果与网页的 'View Page Source'不同

iphone - 当 iPhone 未静音时,Youtube 嵌入视频只会有音频

jquery - 怎么办,当YouTube的视频剪辑完成后,像在begin中一样显示开始剪辑?

youtube - YouTube API设置尺寸

php - PHP PEAR Mail->发送成功/失败结果?

iframe - 是否可以替换Youtube的iframe视频广告

ios - YouTube-Player-iOS-Helper 不能使用 YTPlayerView 类