2014-12-05 189 views
2

我在我的網站上有一個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); 
+0

撕開有時它發生時,如果你的類型的接入網絡,並應處於脫機狀態。在這裏檢查https://developers.google.com/accounts/docs/OAuth2WebServer關鍵字access_type – 2014-12-05 14:06:39

回答

2

您應該考慮使用服務帳戶進行此操作。 A service account將允許您的應用訪問您的Google日曆數據,而不會提示用戶進行訪問。

當您創建服務帳戶時,請使用它提供給您的電子郵件地址,並將其添加到您的Google日曆中。該腳本將可以訪問它。

<?php 
session_start();   
require_once 'Google/Client.php'; 
require_once 'Google/Service/Calendar.php';  
/************************************************ 
The following 3 values an befound in the setting 
for the application you created on Google  
Developers console.   Developers console. 
The Key file should be placed in a location  
that is not accessable from the web. outside of 
web root. 

In order to access your GA account you must  
Add the Email address as a user at the  
ACCOUNT Level in the GA admin.   
************************************************/ 
$client_id = '1046123799103-nk421gjc2v8mlr2qnmmqaak04ntb1dbp.apps.googleusercontent.com'; 
$Email_address = '[email protected]eaccount.com';  
$key_file_location = '629751513db09cd21a941399389f33e5abd633c9-privatekey.p12';  
$client = new Google_Client();  
$client->setApplicationName("Client_Library_Examples"); 
$key = file_get_contents($key_file_location);  
// seproate additional scopes with a comma 
$scopes ="https://www.googleapis.com/auth/calendar.readonly"; 
$cred = new Google_Auth_AssertionCredentials( 
    $Email_address,  
    array($scopes),  
    $key   
    );  
$client->setAssertionCredentials($cred); 
if($client->getAuth()->isAccessTokenExpired()) {   
    $client->getAuth()->refreshTokenWithAssertion($cred);  
}  
$service = new Google_Service_Calendar($client);  

?> 

<html><body> 

<?php 
$calendarList = $service->calendarList->listCalendarList(); 
print_r($calendarList); 
while(true) { 
    foreach ($calendarList->getItems() as $calendarListEntry) { 
     echo "<a href='Oauth2.php?type=event&id=".$calendarListEntry->id." '>".$calendarListEntry->getSummary()."</a><br>\n"; 
    } 
    $pageToken = $calendarList->getNextPageToken(); 
    if ($pageToken) { 
     $optParams = array('pageToken' => $pageToken); 
     $calendarList = $service->calendarList->listCalendarList($optParams); 
    } else { 
     break; 
    } 
}  

?> 
</html> 

代碼來自教程Google Calendar API with PHP – Service Account

+1

工作。完善! – t3living 2014-12-05 17:09:45

+0

新教程,致力於這篇文章。 – DaImTo 2014-12-05 19:07:17

+0

真的很有幫助..非常感謝..浪費了近10個小時..我發現這個,它的作品完美..! – 2015-01-09 09:43:16