2014-05-16 54 views
-1

我試圖使用Google_Service_YouTube將視頻上傳到YouTube。Youtube API:HTTP錯誤:無法連接

IIS7.5,php5.5

與下面的代碼返回值:

「 我得到令牌! 我得到的文件! 捕捉到異常:HTTP錯誤:無法連接 」

我也在Linux服務器上試過相同的代碼和文件,並且運行良好,沒有任何錯誤。

任何幫助?

感謝

$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); // @[email protected] 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; 
} 
?> 

回答

0

得到了谷歌的API控制檯並獲取證書到您的項目。

CLIENT_ID CLIENT_SECRET

$客戶機 - > SETCLIENTID($ OAUTH2_CLIENT_ID); $ client-> setClientSecret($ OAUTH2_CLIENT_SECRET);