2012-10-24 82 views
1

我有一個網站,用戶希望有他們的谷歌日曆 所有的例子似乎有日曆的所有者每次驗證。 有沒有辦法驗證我的應用程序來獲取用戶日曆數據並顯示它?從谷歌日曆檢索事件使用PHP API脫機

我試圖挽救車主的access_token他們接受後,我的應用程序,但過了一會兒,我得到了以下錯誤:

The OAuth 2.0 access token has expired, and a refresh token is not available. Refresh tokens are not returned for responses that were auto-approved. 

這是我想要的代碼(BTW的config.php文件有所有的API東西填寫)

$client = new Google_Client(); 
$client->setUseObjects(true); 
$client->setApplicationName("My Unit Calendar"); 
$client->setAccessType('offline'); 
$client->setAccessToken($row['access_token']); //from the DB 

$calService = new Google_CalendarService($client); 

$events = $calService->events->listEvents($row['google_cal_id']); //from the DB 

echo "events--><pre>".print_r($events,true)."</pre>"; 

,但我得到以下異常:

Google_AuthException-->The OAuth 2.0 access token has expired, and a refresh token is not available. Refresh tokens are not returned for responses that were auto-approved. 

感謝您的任何幫助

回答

1

在從Google獲取/設置某些內容之前,您必須檢查訪問令牌是否已過期,並使用刷新令牌刷新它,您首次獲得授權後會返回(由$client->getAccessToken()返回)。我想,你想要做的事,這樣的:

$client = new Google_Client(); 
$client->setUseObjects(true); 
$client->setApplicationName("My Unit Calendar"); 
$client->setAccessType('offline'); 
$client->setAccessToken($row['access_token']); //from the DB 

$calService = new Google_CalendarService($client); 

checkToken($client, $row['refresh_token']); // Added function 

$events = $calService->events->listEvents($row['google_cal_id']); 

// Check if Access token is expired and get new one using Refresh token 
function checkToken($client) { 
    if($client->isAccessTokenExpired()) { 
     $client->refreshToken($refresh_token); 
    } 
} 

您必須在數據庫中更新存儲器令牌你有當第一次授權,並檢查是否訪問令牌沒有過期,如果過期 - 刷新(例如,在您的google_callback.php file):

// Initialize access to Google 
$client = new Google_Client(); 
$client->setClientId(***); 
$client->setClientSecret(***); 
$client->setRedirectUri(***); 
$client->setAccessType(***); 

// Initialize access to Calendar as service 
$service = new Google_CalendarService($client); 

// If isset code - set into session 
if (isset($_GET['code'])) { 
    $client->authenticate($_GET['code']); 
    $_SESSION['google-api']['access_token'] = $client->getAccessToken(); 
    header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']); 
} 


// If in session is set token 
if (isset($_SESSION['google-api']['access_token'])) { 
    $client->setAccessToken($_SESSION['google-api']['access_token']); 
} 


// If Access Token Expired (uses Google_OAuth2 class), refresh access token by refresh token 
if($client->isAccessTokenExpired()) { 
    $client->refreshToken(/* Get refresh token from DB */); 
} 


// If client got access token successfuly - perform operations 
$access_tokens = json_decode($client->getAccessToken()); 

if ($access_tokens) { 
    // Update refreshToken and save data if refresh token is received (logged in now) 
    if(isset($access_tokens->refresh_token)) { 
     // Store in DB refresh token - $access_tokens->refresh_token 
    } 
}