2017-05-01 88 views
0

我可以使用API​​密鑰和OAuth 2.0客戶端ID將事件添加到我的Google日曆中,但是我想在沒有Google的授權屏幕的情況下執行此操作。google-api-php-client連接key.p12

我跟着這個帖子上找到的信息:

stackoverflow.com/questions/8995451/how-do-i-connect-to-the-google-calendar-api-without-the-oauth-authentication

爲此,我創建了一個'服務帳戶密鑰'。我去了我的項目憑證,然後選擇「創建證書>服務帳戶密鑰」

然後:

服務帳戶> my_project_name

主要類型> P12

我保存的密鑰文件key.p12

這裏是我的代碼:

<?php 
// display error, debbug 
ini_set('display_errors',1); 

session_start(); 

require_once './google-api-php-client/src/Google_Client.php'; 
require_once './google-api-php-client/src/contrib/Google_CalendarService.php'; 

// following values are taken from : console.developers.google.com/apis/credentials?project=projectname credentials 
// OAuth 2.0 client IDs : Client ID 
const CLIENT_ID = 'xxxx'; 
// Service account keys : Service account 
const SERVICE_ACCOUNT_NAME = 'my_project_name'; 

// key 
const KEY_FILE = './key.p12'; 

$client = new Google_Client(); 
$client->setApplicationName("Google Calendar API Quickstart"); 

if (isset($_SESSION['token'])) { 
    $client->setAccessToken($_SESSION['token']); 
} 

// Load the key in PKCS 12 format 
$key = file_get_contents(KEY_FILE); 

$client->setAssertionCredentials(new Google_AssertionCredentials(
    SERVICE_ACCOUNT_NAME, 
    array('https://www.googleapis.com/auth/calendar'), 
    $key) 
); 

$client->setClientId(CLIENT_ID); 
$cal = new Google_CalendarService($client); 

//Save token in session 
if ($client->getAccessToken()) { 
    $_SESSION['token'] = $client->getAccessToken(); 
} 

// my code here 
$event = new Google_Event(... 
$calendarId = '[email protected]'; 
$event = $cal->events->insert($calendarId, $event); 

?> 

這裏是錯誤消息:

Fatal error: Uncaught Google_AuthException: Error refreshing the OAuth2 token, message: '{ 
    'error' : 'invalid_client', 
    'error_description' : 'The OAuth client was not found.' 
}' in /home/www/google-api-php-client/src/auth/Google_OAuth2.php:288 
Stack trace: 
#0 /home/www/google-api-php-client/src/auth/Google_OAuth2.php(264): Google_OAuth2->refreshTokenRequest(Array) 
#1 /home/www/google-api-php-client/src/auth/Google_OAuth2.php(218): Google_OAuth2->refreshTokenWithAssertion() 
#2 /home/www/google-api-php-client/src/service/Google_ServiceResource.php(167): Google_OAuth2->sign(Object(Google_HttpRequest)) 
#3 /home/www/google-api-php-client/src/contrib/Google_CalendarService.php(469): Google_ServiceResource->__call('insert', Array) 
#4 /home/www/goog in /home/www/google-api-php-client/src/auth/Google_OAuth2.php on line 288 

看來,$客戶 - > getAccessToken()沒有設置,但我不知道爲什麼。

在此先感謝您的幫助。

回答

0

創建服務帳號密鑰並下載包含私鑰的* .json文件。將剛剛下載的* .json文件放入您選擇的目錄(示例中爲credentials.json文件)。然後轉到Google日曆 - >共享並在列表中添加服務帳戶ID。

putenv('GOOGLE_APPLICATION_CREDENTIALS=credentials.json'); 

$client = new Google_Client(); 
$client->useApplicationDefaultCredentials(); 
$client->addScope('https://www.googleapis.com/auth/calendar'); 
$client->setHttpClient(new GuzzleHttp\Client([ 'verify' => false ])); // disable ssl if necessary 

$service = new Google_Service_Calendar($client); 

// list events 
$service->events->listEvents($calendar_id, array('timeMin' => (new DateTime)->format(DateTime::RFC3339))); 

// add event 
$event = new Google_Service_Calendar_Event($data); 
$event = $service->events->insert($calendar_id, $event); 

您可以找到谷歌API的PHP客戶端庫包在releases頁面在GitHub上自動加載,因此唯一需要的require_once電話是自動加載的文件:

require_once '/path/to/google-api-php-client/vendor/autoload.php'; 

而且,Google docs可以幫助你此外,這只是一種適用於我的方式,所以它對您也可能有用。

+0

感謝Danijel爲您解答。我使用role> project> owner創建了一個新的服務帳戶證書。我下載了該文件並將其放在我的webserer的根目錄中。我在''然後轉到Google日曆 - >共享並在列表中添加服務帳戶ID。「我去了我的谷歌賬戶日曆>設置>日曆> mycalendar>日曆地址>共享設置>與特定的人分享,在這裏我試圖輸入'服務賬號密鑰ID'沒有成功。它要求一個電子郵件地址。我錯過了什麼 ? – blackndoor

+0

是的,我寫錯了,得到「管理服務帳戶」,並使用「服務帳戶ID」,而不是「密鑰ID」,我的錯誤。 – Danijel

+0

許多感謝,它的作品! – blackndoor