使用PHP api离线从谷歌日历事件中检索

时间:2012-10-24 19:39:29

标签: php google-api google-calendar-api

我有一个用户希望拥有自己的Google日历的网站 所有示例似乎每次都要对日历的所有者进行身份验证。 有没有办法验证我的应用程序以获取用户日历数据并显示它?

我尝试在接受我的应用后保存所有者的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.

这是我正在尝试的代码(顺便说一下,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 个答案:

答案 0 :(得分: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文件中):

// 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
    }
}