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

Posted

技术标签:

【中文标题】使用 Youtube Data API V3 和 Google API Client PHP 将视频上传到 Youtube - 获取 401(未经授权)消息【英文标题】:Uploading video to Youtube using Youtube Data API V3 and Google API Client PHP -- getting 401 (unauthorized) message 【发布时间】:2013-12-09 23:30:17 【问题描述】:

我必须实现一种将视频从我们的网站上传到 youtube 的方法。我已经在 Google Cloud 中注册了该应用程序,并获得了所有必要的客户端 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>";
    

我从以下位置引用这些代码: 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 服务器的响应

Caught Google service Exception 401 message is Error calling POST https://www.googleapis.com/upload/youtube/v3/videos?part=status%2Csnippet&uploadType=multipart&key=AIzaSyCCySI5cToH3sICTmhCEFHW7QkIDptsjXg: (401) Unauthorized

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

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

【问题讨论】:

您的 youtube 帐户必须与您的客户 google 帐户相关联。您可以使用您的客户凭据登录 youtube 吗? 我遇到了类似的问题。在将用户重定向到 Google 授权 URL ($client-&gt;createAuthUrl()) 时,我必须添加适当的范围以使我能够代表客户上传要上传的视频。 $client-&gt;setScope(['enter-scope-here']) 为我做了。我曾经使用 Google API Playground 来发现我需要的范围。 【参考方案1】:

我也遇到了同样的问题。

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

在 youtube 中创建频道后,它起作用了。无需新代码或新密钥。

【讨论】:

你能告诉我,如何在youtube中创建频道以及如何在php代码中使用【参考方案2】:

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

@justine1234 的代码运行良好。只需删除

$client->setDeveloperKey('DEVELOPER_KEY');

并运行它。

非常感谢@justine1234 的代码。

问候

【讨论】:

以上是关于使用 Youtube Data API V3 和 Google API Client PHP 将视频上传到 Youtube - 获取 401(未经授权)消息的主要内容,如果未能解决你的问题,请参考以下文章