2016-12-16 66 views
0

我使用谷歌oAuth获取youtube最新流媒体。它适用于3600秒。但随后停止工作。在对stackoverflow进行一番研究之后,许多人写到使用“SetAccessType”:“offline”。谷歌oAuth 3600秒后不工作

我做到了,但结果相同。

这是我的代码片段。

<?php 

/** 
* Library Requirements 
* 
* 1. Install composer (https://getcomposer.org) 
* 2. On the command line, change to this directory (api-samples/php) 
* 3. Require the google/apiclient library 
* $ composer require google/apiclient:~2.0 
*/ 

$stream_id = ""; 

if (!file_exists(__DIR__ . '/vendor/autoload.php')) { 
    throw new \Exception('please run "composer require google/apiclient:~2.0" in "' . __DIR__ .'"'); 
} 

require_once __DIR__ . '/vendor/autoload.php'; 
session_start(); 


$OAUTH2_CLIENT_ID = '972289696318-q037nr25oti8gs5h7hcj5lfkl7erklh6.apps.googleusercontent.com'; 
$OAUTH2_CLIENT_SECRET = 'cbmQyfeXWGb93RkN7KSHLQKB'; 

$client = new Google_Client(); 
$client->setAccessType('online'); 
$client->setClientId($OAUTH2_CLIENT_ID); 
//$client->setExpires_in('10000000'); 
$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); 




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

//print_r($youtube); 

// Check if an auth token exists for the required scopes 
$tokenSessionKey = 'token-' . $client->prepareScopes(); 



if (isset($_GET['code'])) { 
    if (strval($_SESSION['state']) !== strval($_GET['state'])) { 
    die('The session state did not match.'); 
    } 



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

if (isset($_SESSION[$tokenSessionKey])) { 
    $client->setAccessToken($_SESSION[$tokenSessionKey]); 
} 
/* 
if ($client->isAccessTokenExpired()) { 
    $refreshToken = $client->getRefreshToken(); 
    //print_r($refreshToken); 
    // $client->refreshToken($refreshToken); 
    $newAccessToken = $client->getAccessToken(); 
    $newAccessToken['refresh_token'] = $refreshToken; 
    file_put_contents($credentialsPath, json_encode($newAccessToken)); 

} 
*/ 

// Check to ensure that the access token was successfully acquired. 
if ($client->getAccessToken()) { 


    try { 
    // Execute an API request that lists broadcasts owned by the user who 
    // authorized the request. 
    //print_r($youtube); 
    $broadcastsResponse = $youtube->liveBroadcasts->listLiveBroadcasts(
     'id,snippet', 
     array(
      'mine' => 'true', 
     )); 

    //print_r($broadcastsResponse); 

    $htmlBody .= "<h3>Live Broadcasts</h3><ul>"; 
    $count = 0; 
    foreach ($broadcastsResponse['items'] as $broadcastItem) { 
    // print_r($count+1); 
     $count = $count+1; 
     if($count == 1) { 
     $htmlBody .= sprintf('<li>%s (%s)</li>', $broadcastItem['snippet']['title'], 
      $broadcastItem['id']); 

     $stream_id = $broadcastItem['id']; 
     } 
    // print_r($broadcastItem); 
    } 
    $htmlBody .= '</ul>'; 

    } catch (Google_Service_Exception $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[$tokenSessionKey] = $client->getAccessToken(); 
} elseif ($OAUTH2_CLIENT_ID == 'Replace_me') { 
    $htmlBody = <<<END 
    <h3>Client Credentials Required</h3> 
    <p> 
    You need to set <code>\$OAUTH2_CLIENT_ID</code> and 
    <code>\$OAUTH2_CLIENT_ID</code> before proceeding. 
    <p> 
END; 
} else { 
    // If the user hasn't authorized the app, initiate the OAuth flow 
    $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; 
} 
?> 

<!doctype html> 
<html> 
<head> 
<title>My Live Broadcasts</title> 
</head> 
<body> 
    <?php echo $stream_id; ?> 
</body> 
</html> 

任何想法我能做些什么?

注:我的目的是,任何人都可以在这里进行身份验证,并可以获得最新的YouTube流媒体ID。但是我在3600秒后与oAuth结构。

:(

+0

在这里它看起来像你只使用访问令牌。你也需要刷新令牌。 –

回答

0

你的访问令牌的到期时间。到期通常为半小时(1800秒)的,虽然这可能会有所不同。该值由认证服务器设置,到期时间被发送到你的访问令牌一起。

后访问令牌到期时,服务器将不再接受它,你必须要求使用刷新令牌一个新的。你应该已经与第一访问令牌一起得到它。

如果您没有刷新令牌,则需要重新登录。