2016-11-06 80 views
1

我修改了谷歌日曆API快速入門作出PHP - 谷歌日曆API事件插入返回403沒有足夠的權限

$myEvent = $service->events->insert($calendarId, $myEvent); 

,並得到了一個錯誤:

PHP Fatal error: Uncaught Google_Service_Exception: { 
"error": { 
    "errors": [ 
    { 
    "domain": "global", 
    "reason": "insufficientPermissions", 
    "message": "Insufficient Permission" 
    } 
    ], 
    "code": 403, 
    "message": "Insufficient Permission" 
} 
} 

這裏我的代碼:

<?php 
require_once __DIR__ . '/vendor/autoload.php'; 


define('APPLICATION_NAME', 'gCal2phposteo'); 
define('CREDENTIALS_PATH', '~/.credentials/altesGCal.json'); 
define('CLIENT_SECRET_PATH', __DIR__ . '/client_secret.json'); 
// If modifying these scopes, delete your previously saved credentials 
// at ~/.credentials/altesGCal.json 
define('SCOPES', implode(' ', array(
    Google_Service_Calendar::CALENDAR) 
)); 

if (php_sapi_name() != 'cli') { 
    throw new Exception('This application must be run on the command line.'); 
} 

/** 
* Returns an authorized API client. 
* @return Google_Client the authorized client object 
*/ 
function getClient() { 
    $client = new Google_Client(); 
    $client->setApplicationName(APPLICATION_NAME); 
    $client->setScopes(SCOPES); 
    $client->setAuthConfig(CLIENT_SECRET_PATH); 
    $client->setAccessType('offline'); 
    //pk, from StackOverflow https://stackoverflow.com/questions/10827920/not-receiving-google-oauth-refresh-token 
    // $client->setApprovalPrompt('force');  // needed if you loose the refreshToken 

    // Load previously authorized credentials from a file. 
    $credentialsPath = expandHomeDirectory(CREDENTIALS_PATH); 
    if (file_exists($credentialsPath)) { 
    $accessToken = json_decode(file_get_contents($credentialsPath), true); 
    } else { 
    // Request authorization from the user. 
    $authUrl = $client->createAuthUrl(); 
    printf("Open the following link in your browser:\n%s\n", $authUrl); 
    print 'Enter verification code: '; 
    $authCode = trim(fgets(STDIN)); 

    // Exchange authorization code for an access token. 
    $accessToken = $client->fetchAccessTokenWithAuthCode($authCode); 

    // Store the credentials to disk. 
    if(!file_exists(dirname($credentialsPath))) { 
     mkdir(dirname($credentialsPath), 0700, true); 
    } 
    file_put_contents($credentialsPath, json_encode($accessToken)); 
    printf("Credentials saved to %s\n", $credentialsPath); 
    } 
    $client->setAccessToken($accessToken); 

    // Refresh the token if it's expired. 
    //pk, BUG: refresh token is not saved 
    if ($client->isAccessTokenExpired()) { 
    $refreshToken = $client->getRefreshToken(); 
    $client->fetchAccessTokenWithRefreshToken($refreshToken);  // loosing the refresh token here ! 
    $myAccess = $client->getAccessToken(); 
    $myAccess['refresh_token'] = $refreshToken; 
    file_put_contents($credentialsPath, json_encode($myAccess)); 
    } 
    return $client; 
} 

/** 
* Expands the home directory alias '~' to the full path. 
* @param string $path the path to expand. 
* @return string the expanded path. 
*/ 
function expandHomeDirectory($path) { 
    $homeDirectory = getenv('HOME'); 
    if (empty($homeDirectory)) { 
    $homeDirectory = getenv('HOMEDRIVE') . getenv('HOMEPATH'); 
    } 
    return str_replace('~', realpath($homeDirectory), $path); 
} 

// Get the API client and construct the service object. 
$client = getClient(); 
$service = new Google_Service_Calendar($client); 

$calendarId = 'primary'; 

$myEvent = new Google_Service_Calendar_Event(
    array(
    'summary'  => 'pk4test Summary', 
    'location' => 'Village-Neuf, 1 rue des alouettes', 
    'description' => 'Consultation d\'ostéopathie.', 
    'start'  => array(
     'dateTime' => '2016-11-07T18:15:00.000+01:00', 
     'timeZone' => 'Europe/Paris', 
    ), 
    'end'   => array(
     'dateTime' => '2016-11-07T19:00:00.000+01:00', 
     'timeZone' => 'Europe/Paris', 
    ), 
    'attendees' => array(
     array(
     'email'  => '[email protected]', 
     'organizer' => true 
    ), 
     # array('email' => '[email protected]', 'resource' => true), 
    ), 
    'creator'  => array(
     'email'  => '[email protected]', 
     'displayName' => 'Cabinet d\'ostéopathie Kienner Mireille', 
     'self'  => true 
    ), 
    'guestsCanInviteOthers' => false, 
    'guestsCanModify'   => false, 
    'guestsCanSeeOtherGuests' => false, 
) 
); 

$myEvent = $service->events->insert($calendarId, $myEvent); 

printf('Event created: %s', $myEvent->htmlLink); 

我看到3篇文章說我應該分享我的日曆與自動創建的服務帳戶:

POST 1: 403 Forbidden message when calling the v3 Google Calendar API using a Service Account via OAuth 2.0

POST 2: Edit Google calendar events from Google service account: 403

我發現2個創建的帳戶,即使我想我與[email protected]分享,我也分享了與[email protected]@appspot.gserviceaccount.com

任何一個想法,什麼是錯的日曆?

回答

2

中列出的步驟嘗試刪除任何存儲的憑證。

在你的情況'〜/ .credentials/altesGCal.json'。

+0

感謝您的提示。我正在使用與快速入門(只讀訪問)相同的憑證文件。刪除憑證文件=>我被要求提供新的授權,這顯然是不同的,並且也可以作爲寫入訪問。 –

0

確保您還授予從Google Developer Console創建的客戶端電子郵件地址的相應權限。我建議按照OAuth guide.