Php Youtube API-上传视频

Php Youtube API-上传视频,php,youtube-api,youtube-data-api,google-oauth,Php,Youtube Api,Youtube Data Api,Google Oauth,我正试图上传一个视频到Youtube,我需要它上传没有征得用户同意,因为所有视频将上传到我自己的频道未列出 所以我找到了“服务帐户”,它应该允许我这样做,因为它们允许服务器到服务器的通信。我找到了一个例子,我正在努力让它发挥作用: $client_email = 'project-name@project-name.iam.gserviceaccount.com'; $scopes = array( 'https://www.googleap

我正试图上传一个视频到Youtube,我需要它上传没有征得用户同意,因为所有视频将上传到我自己的频道未列出

所以我找到了“服务帐户”,它应该允许我这样做,因为它们允许服务器到服务器的通信。我找到了一个例子,我正在努力让它发挥作用:

        $client_email = 'project-name@project-name.iam.gserviceaccount.com';

        $scopes = array(
          'https://www.googleapis.com/auth/youtube',
          'https://www.googleapis.com/auth/youtube.upload',
          'https://www.googleapis.com/auth/youtubepartner', 
          'https://www.googleapis.com/auth/youtube.force-ssl'
        );

        $user_to_impersonate = 'email@youtube-account.com';

        $private_key = file_get_contents($credentialsPath);

        if (isset($user_to_impersonate) && $user_to_impersonate != "") {
            $credentials = new Google_Auth_AssertionCredentials(
                $client_email,
                $scopes,
                $private_key,
                'notasecret',                                 // Default P12 password
                'http://oauth.net/grant_type/jwt/1.0/bearer', // Default grant type
                $user_to_impersonate
            );
        } else {
            $credentials = new Google_Auth_AssertionCredentials(
                $client_email,
                $scopes,
                $private_key
            );
        }

        $googleClient = new Google_Client();

        $googleClient->setAccessType('offline');
        $googleClient->setApprovalPrompt('None');

        try {
            $googleClient->setAssertionCredentials($credentials);
        } catch (Google_Auth_Exception $e) {
            die("false:Unable to acquire credentials.");
        }

        if ($googleClient->getAuth()->isAccessTokenExpired()) {
            try {
                $googleClient->getAuth()->refreshTokenWithAssertion();
            } catch (Google_Auth_Exception $e)                        //<---- The code fails at this exception when the impersonate code is used
            {
                var_dump($e->getMessage(), "../error.log");
                die("false:Token has expired. Unable to refresh.");
            }
        }

$client->setApprovalPrompt('None');为了避免同意,@AhmedSunny我添加了你的建议,但没有运气,同样的错误。(我也更新了我的示例)您错过了范围,我不确定是哪一个,但尝试添加这两个@AhmedSunny我也添加了范围,但错误相同:(
"Error refreshing the OAuth2 token, message: '{
  "error": "unauthorized_client",
  "error_description": "Client is unauthorized to retrieve access tokens using this method, or client not authorized for any of the scopes requested."
}'"