2016-10-05 97 views
-2

我試圖讓YouTube視頻的字幕,但我得到這個錯誤的YouTube API錯誤需要登錄PHP

Google_Service_Exception在REST.php行118:需要登錄

這是我的代碼

function getCaptions($app, $developKey, $OAUTH2_CLIENT_ID, $OAUTH2_CLIENT_SECRET){ 

    $client = new Google_Client(); 
    $client->setApplicationName($app); 
    $client->setDeveloperKey($developKey); 
    $client->setClientId($OAUTH2_CLIENT_ID); 
    $client->setClientSecret($OAUTH2_CLIENT_SECRET); 
    $client->setScopes('https://www.googleapis.com/auth/youtube.force-ssl'); 
    $client->setScopes('https://www.googleapis.com/auth/youtube'); 

    $redirect = filter_var('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'], 
     FILTER_SANITIZE_URL); 
    $client->setRedirectUri($redirect); 

    // Define an object that will be used to make all API requests. 
    $youtube = new Google_Service_YouTube($client); 

    $captions = $youtube->captions->listCaptions('snippet',$video); 

    if (empty($captions)) { 
     $htmlBody .= "<h3>Can't get video caption tracks.</h3>"; 
    } else { 
     $firstCaptionId = $captions[0]['id']; 
     $this->downloadCaption($youtube, $firstCaptionId, $htmlBody); 
    } 

    return response()->json($searchResponse->items[0]->snippet); 
} 

function downloadCaption(Google_Service_YouTube $youtube, $captionId, &$htmlBody) { 
    // Call the YouTube Data API's captions.download method to download an existing caption. 
    $captionResouce = $youtube->captions->download($captionId, array(
     'tfmt' => "srt", 
     'alt' => "media" 
)); 

$htmlBody .= "<h2>Downloaded caption track</h2><ul>"; 
$htmlBody .= sprintf('<li>%s</li>', 
    $captionResouce); 
$htmlBody .= '</ul>'; 
} 

我知道我需要一個代碼,做認證($client->authentication($code);),但我不知道在哪裏可以得到這些代碼。

+0

關於這方面的更新? – noogui

回答

0

請確保您有$client->setAuthConfig($oauth_credentials);對OAuth的部分:

$client = new Google_Client(); 
$client->setAuthConfig($oauth_credentials); 
$client->setRedirectUri($redirect_uri); 
$client->addScope("https://www.googleapis.com/auth/drive"); 
$service = new Google_Service_Drive($client); 

這是做這個由谷歌在PHP github samples呈現的方式。您會看到setAuthConfig總是包含在php示例中。