Frage

I am trying to upload videos to youtube using Google_Service_YouTube.

IIS7.5 , php5.5

Return value with below code :

" I got token!! I got file!! Caught exception: HTTP Error: Unable to connect "

I also tried same code and file on Linux server.And it worked well without any error.

Any help ?

thanks

$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->setScopes('https://www.googleapis.com/auth/youtube');
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],     FILTER_SANITIZE_URL);

$client->setRedirectUri($redirect);

$youtube = new Google_Service_YouTube($client);


if (isset($_GET['code'])) {

  if (strval($_SESSION['state']) !== strval($_GET['state'])) {

    die('The session state did not match.' . strval($_SESSION['state']) ."  ::  " . strval($_GET['state'])  );

  }

  $client->authenticate($_GET['code']);
  $_SESSION['token'] = $client->getAccessToken();
  header('Location: ' . $redirect);

}


if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
}


if ($client->getAccessToken()) {
        $htmlBody =""; 


if($client->isAccessTokenExpired()) {
    $authUrl = $client->createAuthUrl();
    header('Location: ' . filter_var($authUrl, FILTER_SANITIZE_URL));
}

try{
    echo "I got token!! <br>";
    $videoPath = "./youTubeUpload/gizmo.mp4";  
    if(file_exists($videoPath)){
        echo "I got file!! <br>";
    }

    $snippet = new Google_Service_YouTube_VideoSnippet();
    $snippet->setTitle("Test title");
    $snippet->setDescription("Test description");
    $snippet->setTags(array("tag1", "tag2"));



    $snippet->setCategoryId("22");


    $status = new Google_Service_YouTube_VideoStatus();
    $status->privacyStatus = "public";

    $video = new Google_Service_YouTube_Video();
    $video->setSnippet($snippet);
    $video->setStatus($status);

    $chunkSizeBytes = 1 * 1024 * 1024;
    $client->setDefer(true);

    $insertRequest = $youtube->videos->insert("status,snippet", $video);


    $media = new Google_Http_MediaFileUpload(
        $client,
        $insertRequest,
        'video/*',
        null,
        true,
        $chunkSizeBytes
    );
    $media->setFileSize(filesize($videoPath));

    $status = false;
    $handle = fopen($videoPath, "rb");


    while (!$status && !feof($handle)) {

      $chunk = fread($handle, $chunkSizeBytes);

     try{
        $status = $media->nextChunk($chunk);    //  @!@ Here is Error point  
     } catch(Exception $e){
        echo 'Caught exception: ',  $e->getMessage(), "\n";
     }

    }

    fclose($handle);

    $client->setDefer(false);

    $htmlBody .= "<h3>Video Uploaded</h3><ul>";
    $htmlBody .= sprintf('<li>%s (%s)</li>',
        $status['snippet']['title'],
        $status['id']);

    $htmlBody .= '</ul>';

} catch (Google_ServiceException $e) {
  $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',
      htmlspecialchars($e->getMessage()));
} catch (Google_Exception $e) {
  $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>',
      htmlspecialchars($e->getMessage()));
}

$_SESSION['token'] = $client->getAccessToken();
} else {
$state = mt_rand();
$client->setState($state);
$_SESSION['state'] = $state;

$authUrl = $client->createAuthUrl();
$htmlBody = <<<END
<h3>Authorization Required</h3>
<p>You need to <a href="$authUrl">authorize access</a> before proceeding.<p>
END;
}
?>

Keine korrekte Lösung

Andere Tipps

Got to Google Api Console and Get the Credentials to your project.

CLIENT_ID CLIENT_SECRET

$client->setClientId($OAUTH2_CLIENT_ID); $client->setClientSecret($OAUTH2_CLIENT_SECRET);

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top