2012-10-24 177 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 
    } 
}