我在我的網站上有一個php應用程序,它允許我的用戶使用我的谷歌日曆安排/重新安排日曆事件,所以我不需要用戶使用Google進行身份驗證。我已經通過獲取令牌並存儲了刷新令牌,但現在,當我嘗試訪問日曆時,出現一條說明我的令牌已過期的錯誤。輸出是使用刷新令牌的Google Calendar API
創建客戶端 發現訪問令牌= { 「的access_token」: 「長...憑證...串」, 「token_type」: 「承載」, 「expires_in」:3600} 出錯:(0)OAuth 2.0訪問令牌已過期,並且刷新令牌不可用。刷新令牌不會返回自動批准的響應。
不知道爲什麼我得到這個錯誤。
function getAccessToken(){
$tokenURL = 'https://accounts.google.com/o/oauth2/token';
$postData = array(
'client_secret'=>'My-Secret',
'grant_type'=>'refresh_token',
'approval_promt'=> 'force',
'refresh_token'=>'My-Refresh-Token',
'client_id'=>'My-Client-ID'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $tokenURL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$tokenReturn = curl_exec($ch);
return $tokenReturn;
}
function outputCalendarByDateRange($client, $startDate='2007-05-01', $endDate='2007-08-01'){
date_default_timezone_set("America/Chicago");
$client->addScope('https://www.googleapis.com/auth/calendar');
try {
$service = new Google_Service_Calendar($client);
}catch(Google_ServiceException $e){
print "Error code :" . $e->getCode() . "\n";
print "Error message: " . $e->getMessage() . "\n";
} catch (Google_Exception $e) {
print "An error occurred: (" . $e->getCode() . ") " . $e->getMessage() . "\n";
}
$optParams = array(
'orderBy'=>'starttime',
'singleEvents'=>False,
'timeMax'=>$endDate,
'timeMin'=>$startDate,
'timeZone'=>'America/Chicago',
'maxResults'=>1000
);
try{
$events = $service->events->listEvents('primary',$optParams);
} catch (Google_ServiceException $e) {
print "Error code :" . $e->getCode() . "\n";
print "Error message: " . $e->getMessage() . "\n";
} catch (Google_Exception $e) {
print "An error occurred: (" . $e->getCode() . ") " . $e->getMessage() . "\n";
}
foreach ($events->getItems() as $event) {
echo $event->getSummary();
}
}
echo "creating a client<br>";
$client = new Google_Client();
$accessToken = getAccessToken();
echo "found access token = ".$accessToken."<br>";
try{
$client->setAccessToken($accessToken);
}
catch (Google_ServiceException $e) {
print "Error code :" . $e->getCode() . "\n";
print "Error message: " . $e->getMessage() . "\n";
} catch (Google_Exception $e) {
print "An error occurred: (" . $e->getCode() . ") " . $e->getMessage() . "\n";
}
outputCalendarByDateRange($client, $today, $tomorrow);
撕開有時它發生時,如果你的類型的接入網絡,並應處於脫機狀態。在這裏檢查https://developers.google.com/accounts/docs/OAuth2WebServer關鍵字access_type – 2014-12-05 14:06:39