Youtube API - Загрузить видео - PullRequest
0 голосов
/ 11 марта 2020

Я пытаюсь загрузить видео на 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.");
            }
        }

Я получаю следующую ошибку:

"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."
}'"
...