dongqiaolong9034 2014-03-26 09:50
浏览 50

Youtube上传API的身份验证错误

I'm doing an application that uploads videos from a web form into my youtube account. I want to use the server authentication.

I have configured my app in the Google Cloud Console and I have downloaded the private key into my server.

This is the code:

const SERVICE_ACCOUNT_EMAIL = 'MY_ACCOUNT_EMAIL';
    const CLIENT_ID = 'MY_CLIENT_ID';
    const KEY_FILE = './MY_PRIVATE_KEY.p12';

    if ( !isset($_FILES['video']) ) {

        echo '<form method="post" enctype="multipart/form-data">';
        echo '<input type="file" name="video">';
        echo '<input type="submit" value="Submit">';        
        echo '</form>';

    }
    else {
        require_once 'Google/Client.php';
        require_once 'Google/Service/YouTube.php';


        $client = new Google_Client();
        $client->setApplicationName('My Application Name');
        $client->setClientId(CLIENT_ID);

        $oauthClient = new Google_Auth_OAuth2($client);

        // Set your cached access token. Remember to replace $_SESSION with a
        // real database or memcached.
        session_start();
        if (isset($_SESSION['token'])) {
            $oauthClient->setAccessToken($_SESSION['token']);
        } else {
            $key = file_get_contents(KEY_FILE);
            $oauthClient->refreshTokenWithAssertion(new Google_Auth_AssertionCredentials(SERVICE_ACCOUNT_EMAIL, array('https://www.googleapis.com/auth/youtube'), $key));
        }

        if ($oauthClient->getAccessToken()) {

            echo '<p>Uploading ...</p>';
            $youtube = new Google_Service_YouTube($client);

            $videoPath = $_FILES['video']['tmp_name'];

            // Create a snippet with title, description, tags and category ID
            $snippet = new Google_Service_YouTube_VideoSnippet();
            $snippet->setTitle("Test video ".date('H:i'));
            $snippet->setDescription("Test description ".date('H:i'));
            $snippet->setTags(array("test", "oauth"));

            // Set the video's status to "public".
            $status = new Google_Service_YouTube_VideoStatus();
            $status->privacyStatus = "public";

            // Associate the snippet and status objects with a new video resource.
            $video = new Google_Service_YouTube_Video();
            $video->setSnippet($snippet);
            $video->setStatus($status);

            // Create a request for the API's videos.insert method to create and upload the video.
            $insertRequest = $youtube->videos->insert("status,snippet", $video, array("data"=>file_get_contents($videoPath), "uploadType" => "media", "mimeType" => "video/mp4"));

            echo '<h3>Upload complete</h3>';
            echo '<p><a href="https://www.youtube.com/watch?v='.$status['id'].'" target="_blank">Link to the video</a></p>';

        } 
        else {
            $authUrl = $client->createAuthUrl();
            print "<a href='$authUrl'>Authorize this application</a>";
        }
    }

It doesn't work. I have a Fatal Error:

Error calling POST https://www.googleapis.com/upload/youtube/v3/videos?part=status%2Csnippet&uploadType=multipart: (401) Login Required

What am I doing wrong?

  • 写回答

1条回答 默认 最新

  • douyong4623 2014-03-26 19:29
    关注

    You shouldn't use that OAuth2 class, just call the setAssertionCredentials method of the Google_Client passing a Google_Auth_AssertionCredentials instance.

    Take a look at this example (it's using the old PHP lib version, but the authentication process is the same).

    评论

报告相同问题?

悬赏问题

  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划