使用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

查看:123
本文介绍了使用Youtube Data API V3和Google API Client PHP将视频上传到Youtube-收到401(未经授权)消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须实现一种将视频从我们的网站上传到youtube的方法.我已经在Google Cloud中注册了该应用,并获得了所有必需的客户端ID,客户端密码,浏览器密钥,重定向uri和服务器密钥.我也确实打开了各个网站建议的YouTube数据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帐户(屏幕要求让Google管理我的YT帐户),然后在第二次尝试中,我收到此401消息响应.

这是youtube服务器的回复

捕获到的Google服务异常401消息错误,原因是调用POST https://www.googleapis.com/upload/youtube/v3/videos?part=status%2Csnippet&uploadType=multipart&key=AIzaSyCCySI5cToH3sICTmhCEFHW7QkIDptsjXg :(401)未经授权>

我试图搜索各种解决方案,但无济于事,我似乎找不到它. 感谢您的任何帮助,谢谢!

后续问题:开发人员密钥是浏览器密钥还是服务器密钥?我对应该在代码中使用哪个感到困惑.我尝试过切换键,但是也没用.

解决方案

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

问题的人 @ justine1234的

代码工作正常.只需删除

$client->setDeveloperKey('DEVELOPER_KEY');

并运行它.

非常感谢代码@ justine1234.

致谢

I have to implement a way to upload video from our site to youtube. I already registered the app in Google Cloud and got all the necessary client ID, client secret code, browser key, redirect uri and server key. I also did turn ON the Youtube Data API V3, Google+ API, Freebase API and YouTube Analytics API as suggessted by various sites.

Here is my code below:

    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>";
    }

I was referencing these codes from: Upload video to Youtube using Youtube API V3 and PHP

and

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

First time I ran this code, it had me connect to the youtube account (with a screen asking to let google manage my YT account) and then on the second try, I got this 401 Message response.

Here's the response from youtube server

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

I tried to search for various solutions but to no avail I cant seem to find it. Any help is appreciated, thanks!

follow-up question: Is the developer key the browser key or the server key? I am confused as to which should I use in my code. I've tried switching the keys and it also didn't work.

解决方案

For Those who are getting the problem of Access Not Configured. Please use Google Developers Console to activate the API for your project,

code by @justine1234 is working perfectly fine. Just remove

$client->setDeveloperKey('DEVELOPER_KEY');

and run it.

Thanks a lot for the code @justine1234.

Regards

这篇关于使用Youtube Data API V3和Google API Client PHP将视频上传到Youtube-收到401(未经授权)消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆