Upload video to YouTube with PHP asks verification code

37 Views Asked by At

Am building an API and need to have a method that allows uploading videos to YouTube.

The front end is handled by someone else, but PHP looks like this from what I could google:

require_once __DIR__ . '/vendor/autoload.php';

$client = new Google_Client();
$client->setApplicationName('API code samples');
$client->setScopes([
    'https://www.googleapis.com/auth/youtube.upload',
]);

$client->setAuthConfig('client_secret.json');
$client->setAccessType('offline');

// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open this link in your browser:\n%s\n", $authUrl);
print('Enter verification code: ');
$authCode = trim(fgets(STDIN));

// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
$client->setAccessToken($accessToken);

// Define service object for making API requests.
$service = new Google_Service_YouTube($client);

// Define the $video object, which will be uploaded as the request body.
$video = new Google_Service_YouTube_Video();

$response = $service->videos->insert(
  '',
  $video,
  array(
    'data' => file_get_contents("video.mov"),
    'mimeType' => 'application/octet-stream',
    'uploadType' => 'multipart'
  )
);
print_r($response);

Problem: This requires a verification code via the prompt which I don't even know where to get from.

Question: Hoping that someone has experience with this. How can I upload a video to YouTube using the API?

Note: The idea is to be able to have a script where I just pass the path to the video, title, description, have it uploaded, and get the response back.

0

There are 0 best solutions below