Php Youtube上载API的身份验证错误

Php Youtube上载API的身份验证错误,php,oauth,youtube-api,google-oauth,Php,Oauth,Youtube Api,Google Oauth,我正在做一个应用程序,将视频从web表单上传到我的youtube帐户。我想使用服务器身份验证 我已经在谷歌云控制台中配置了我的应用程序,并将私钥下载到了我的服务器中 代码如下: const SERVICE_ACCOUNT_EMAIL = 'MY_ACCOUNT_EMAIL'; const CLIENT_ID = 'MY_CLIENT_ID'; const KEY_FILE = './MY_PRIVATE_KEY.p12'; if ( !isset($_FILES['vi

我正在做一个应用程序,将视频从web表单上传到我的youtube帐户。我想使用服务器身份验证

我已经在谷歌云控制台中配置了我的应用程序,并将私钥下载到了我的服务器中

代码如下:

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>";
        }
    }
const SERVICE_ACCOUNT_EMAIL='MY_ACCOUNT_EMAIL';
const CLIENT_ID='MY_CLIENT_ID';
const KEY_FILE='./MY_PRIVATE_KEY.p12';
如果(!isset($\u文件['video'])){
回声';
回声';
回声';
回声';
}
否则{
需要一次“Google/Client.php”;
需要_once“Google/Service/YouTube.php”;
$client=新的Google_客户端();
$client->setApplicationName(“我的应用程序名”);
$client->setClientId(客户端ID);
$oauthClient=新的Google_Auth_OAuth2($client);
//设置缓存的访问令牌。请记住将$\u会话替换为
//真实数据库或memcached。
会话_start();
如果(isset($\u会话['token'])){
$oauthClient->setAccessToken($_会话['token']);
}否则{
$key=file\u get\u内容(key\u文件);
$oauthClient->refreshTokenWithAssertion(新的谷歌认证断言属性(服务、帐户、电子邮件、数组)https://www.googleapis.com/auth/youtube“),$key”);
}
如果($oauthClient->getAccessToken()){
回声'上传…

'; $youtube=新的谷歌服务\ youtube($client); $videoPath=$_文件['video']['tmp_名称']; //创建具有标题、说明、标记和类别ID的代码段 $snippet=新的Google_服务_YouTube_视频片段(); $snippet->setTitle(“测试视频”.date('H:i')); $snippet->setDescription(“测试描述”.date('H:i')); $snippet->setTags(数组(“test”、“oauth”); //将视频的状态设置为“公共”。 $status=新的Google_服务_YouTube_VideoStatus(); $status->privacyStatus=“public”; //将代码段和状态对象与新的视频资源关联。 $video=新的Google_服务_YouTube_视频(); $video->setSnippet($snippet); $video->setStatus($status); //创建API视频的请求。插入方法以创建和上载视频。 $insertRequest=$youtube->videos->insert(“状态,片段”,“视频”,“数组”(“数据”=>文件获取内容($videoPath),“上传类型”=>“媒体”,“mimeType”=>“视频/mp4”); echo“上传完成”; 回声“

”; } 否则{ $authUrl=$client->createAuthUrl(); 打印“”; } }
它不起作用。我有一个致命的错误:

调用POST时出错:(401)需要登录


我做错了什么?

您不应该使用OAuth2类,只需调用
Google\u客户端的
setAssertionCredentials
方法,传递
Google\u Auth\u AssertionCredentials
实例

看一看(它使用的是旧的PHP库版本,但身份验证过程是相同的)