Hosein
Hosein

Reputation: 21

Fatal error: Uncaught Error: Call to undefined method Google\Client::fetchAuthToken() with Google API Client Library (PHP)

I'm trying to upload videos to YouTube using the YouTube Data API v3 and the Google API Client Library for PHP. I'm encountering a persistent error:

Fatal error: Uncaught Error: Call to undefined method Google\Client::fetchAuthToken() in /home3/public_html/a/a.php:20 Stack trace: #0 {main} thrown in /home3/public_html/a/a.php on line 20 This error occurs during the OAuth 2.0 authorization process, specifically when trying to exchange the authorization code for an access token.

Here's the relevant code snippet:

<?php
session_start();

if (file_exists('vendor/autoload.php')) {
    echo "Google API Client Library found.<br>";
    require_once 'vendor/autoload.php';
    if (class_exists('Google_Client')) {
        $reflection = new ReflectionClass('Google_Client');
        //$versionProperty = $reflection->getProperty('version');
        //$versionProperty->setAccessible(true);
        //$version = $versionProperty->getValue(new Google_Client());
        echo "Google API Client Library version: " . $version . "<br>";
    } else {
      echo "Google_Client class not found.  Check your installation.<br>";
    }
} else {
    echo "ERROR: Google API Client Library NOT found.  Run 'composer install' in your project directory.<br>";
    exit;
}


$client = new Google_Client();
$client->setApplicationName('My Project 1'); // Replace with your app name

// **IMPORTANT:** Replace these with your actual credentials from the Google Cloud Console.
$client->setClientId('69333****');
$client->setClientSecret('GOCSPX-xaX****');
$client->setRedirectUri('https://****.ir/a/a.php'); // Where Google redirects after auth
$client->setScopes([Google_Service_YouTube::YOUTUBE_UPLOAD, Google_Service_YouTube::YOUTUBE]);

if (isset($_SESSION['access_token'])) {
    $client->setAccessToken($_SESSION['access_token']);
} else if (isset($_GET['code'])) {
    try {
        $token = $client->fetchAuthToken(['code' => $_GET['code']]);
        $_SESSION['access_token'] = $token;
        $client->setAccessToken($token);
        header('Location: ' . basename(__FILE__));
        exit;
    } catch (Exception $e) {
        echo "Error fetching auth token: " . $e->getMessage();
        exit;
    }
} else {
    $authUrl = $client->createAuthUrl();
    echo '<a href="' . $authUrl . '">Authorize with Google</a>';
    exit;
}

if ($client->getAccessToken()) {
    $youtube = new Google_Service_YouTube($client);

    $video = new Google_Service_YouTube_Video();
    $videoSnippet = new Google_Service_YouTube_VideoSnippet();
    $videoSnippet->setTitle('My Test Video');
    $videoSnippet->setDescription('This is a test video upload.');
    $video->setSnippet($videoSnippet);

    $videoStatus = new Google_Service_YouTube_VideoStatus();
    $videoStatus->setPrivacyStatus('private');
    $video->setStatus($videoStatus);

    $videoPath = 'j.mp4';
    $mimeType = 'video/mp4';

    try {
        $fileHandle = fopen($videoPath, 'rb');

        $media = new Google_Http_MediaFileUpload(
            $client,
            "POST",
            "/youtube/v3/videos?part=snippet,status,contentDetails",
            $fileHandle,
            $mimeType,
            true
        );

        $media->setChunkSize(262144 * 5);

        $response = false;
        $uploadStatus = false;

        while (!$uploadStatus) {
            $status = $media->getProgress();
            if (!$status) {
                $response = $media->upload();
                if (is_object($response)) {
                    $uploadStatus = true;
                }
            } else {
                $response = $media->resume();
                if (is_object($response)) {
                    $uploadStatus = true;
                }
            }
        }

        fclose($fileHandle);

        if (is_object($response) && isset($response->id)) {
            echo "Video ID: " . $response->id . "\n";
        } else {
            echo "Error uploading video:\n";
            print_r($response);
        }

    } catch (Exception $e) {
        echo "Error during upload: " . $e->getMessage();
        exit;
    }
}
?>

Upvotes: 0

Views: 25

Answers (0)

Related Questions