![](/img/trans.png)
[英]Creating an google calendar event with gadget using google-api-php-client
[英]google service account authentication with google-api-php-client using calendar api
我使用的是php 5.3.3和codeigniter 2.1.0。 我想要做的是設置一個服務帳戶,這樣用戶就可以在我的網站上的文本輸入字段中添加約會,然后將該約會添加到共享的共享谷歌日歷中。
我有一個谷歌帳戶,並使用: https : //code.google.com/apis/console我在服務上創建了一個名為'pqp'的新項目:在api訪問時啟用了日歷api:我創建了一個誓言2.0客戶端ID ...產品名稱= pqp,應用程序類型=服務帳戶。
下載了密鑰46 ... -privatekey.p12。 有一個設置的屏幕截圖:
我得到了google-api-php-client的svn簽出(28/6/2012)在google-api-php-client / src / config.php中我更改了行:
25: 'application_name' => 'pqp',
28: 'oauth2_client_id' => '373xxx730.apps.googleusercontent.com',
57: 'ioFileCache_directory' => 'tmp/apiClient', // my apache user does not have access to the system /tmp folder. + tmp/apiClient has permissions of 777 on the server.
使用此鏈接:
http://code.google.com/p/google-api-php-client/source/browse/trunk/examples/prediction/serviceAccount.php?spec=svn445&r=395
我把它修改為:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Test extends CI_Controller{
function __construct()
{
parent::__construct();
}
function index()
{
set_include_path(get_include_path() . PATH_SEPARATOR .dirname(__FILE__).'/../libraries/google-api-php-client/src');
ini_set('error_reporting',E_ALL);
ini_set('display_errors','1');
// Set your client id, service account name, and the path to your private key.
// For more information about obtaining these keys, visit:
// https://developers.google.com/console/help/#service_accounts
define('CLIENT_ID','3731xxx44730.apps.googleusercontent.com');
define('SERVICE_ACCOUNT_NAME','373xxx244730@developer.gserviceaccount.com');
// Make sure you keep your key.p12 file in a secure location, and isn't
// readable by others.
define('KEY_FILE',dirname(__FILE__).'/../../461290xxx796c0b7db9582c-privatekey.p12');
require_once "apiClient.php";
require_once "contrib/apiCalendarService.php";
$client = new apiClient();
$client->setApplicationName("pqp");
// Set your cached access token. Remember to replace $_SESSION with a
// real database or memcached.
session_start();
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
echo 'client access token is set.<br/>';
}
// Load the key in PKCS 12 format (you need to download this from the
// Google API Console when the service account was created.
$key = file_get_contents(KEY_FILE);
$creds = new apiAssertionCredentials(SERVICE_ACCOUNT_NAME,array('https://www.googleapis.com/auth/calendar'),$key);
$client->setAssertionCredentials($creds);
$client->setClientId(CLIENT_ID);
$service = new apiCalendarService($client);
echo 'client:<br/>';
var_dump($client);
echo 'service:<br/>';
var_dump($service);
// We're not done yet. Remember to update the cached access token.
// Remember to replace $_SESSION with a real database or memcached.
if ($client->getAccessToken()) {
$_SESSION['token'] = $client->getAccessToken();
echo 'token is good!, so creating an event....';
echo $this->insert_event($service,'testing summary','my location','2012-06-29T10:00:00.000+10:00','2012-06-29T10:00:00.000+10:00');
}
}
function insert_event($service,$summary,$location,$from,$to){
$event = new Event();
$event->setSummary($summary);
$event->setLocation($location);
$start = new EventDateTime();
$start->setDateTime($from);
$event->setStart($start);
$end = new EventDateTime();
$end->setDateTime($to);
$event->setEnd($end);
$attendee1 = new EventAttendee();
$attendee1->setEmail('test@example.com');
$attendees = array($attendee1);
$event->attendees = $attendees;
$createdEvent = $service->events->insert('primary', $event);
return $createdEvent->getId();
}
}
輸出的餡餅在這里 :
$ client對象未經過身份驗證,未設置getAccessToken,並且未插入事件。
我發現很難找出$ config文件中的哪些設置要更改,因為有不同的命名法。 我想這是代碼進展的一個神器。 src / config.php中的設置是否正確? 我需要更改更多設置嗎?
我的理解是,如果我創建服務帳戶,下載密鑰文件,並使用我的開發人員ID,該文件的內容,它應該返回一個令牌,並且不需要設置重定向uri ..是正確的? 這是我想要的功能。 我不希望用戶必須授權訪問,因為該網站只會與一個谷歌帳戶進行交互。
所以,問題是,我如何使用谷歌服務帳戶獲取此日歷api進行身份驗證?
你可以嘗試這個,如果你還沒有...
編輯: 這個有趣,但如果你仍然想自己寫一個,那么試試這個 oauth2的母親。 有幫助
關於你使用的google-api-php-client的服務帳戶(總是帶着SVN使用trunk)我在代碼操作中找不到任何引用apiAssertionCredentials::generateAssertion()
肯定沒有調用auth在那里
public function __construct(
$serviceAccountName,
$scopes,
$privateKey,
$privateKeyPassword = 'notasecret',
$assertionType = 'http://oauth.net/grant_type/jwt/1.0/bearer',
$prn = false) {
$this->serviceAccountName = $serviceAccountName;
$this->scopes = is_string($scopes) ? $scopes : implode(' ', $scopes);
$this->privateKey = $privateKey;
$this->privateKeyPassword = $privateKeyPassword;
$this->assertionType = $assertionType;
$this->prn = $prn;
}
應該調用這個方法我猜...
public function generateAssertion() {
$now = time();
$jwtParams = array(
'aud' => apiOAuth2::OAUTH2_TOKEN_URI,
'scope' => $this->scopes,
'iat' => $now,
'exp' => $now + self::MAX_TOKEN_LIFETIME_SECS,
'iss' => $this->serviceAccountName,
);
if ($this->prn !== false) {
$jwtParams['prn'] = $this->prn;
}
return $this->makeSignedJwt($jwtParams);
}
編輯:原諒。 在新鮮的版本。 看起來像是Google_OAuth2::refreshTokenWithAssertion()
實際應該啟動真正的過程
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.