Google APIのPHPクライアントでUserinfoを取得できるようにする

カレンダーとかGoogle+とかのGoogleAPIを使うとき,Googleさんが用意しているPHP用のクライアントがあります.
これがそうですね.
http://code.google.com/p/google-api-php-client/
ライブラリが用意されているなんて,楽だなー.
と,思ったのですが.
このライブラリには,認証を受けたユーザの情報を取得する,Userinfoサービスが見当たらないんですよね.*1
ということで,それを実装して見ました.

ライブラリのディレクトリ構成的に,contribディレクトリ以下に各サービス用のクラス群が実装されたファイルが設置してあるので,これにならってcontrib/apiUserinfoService.phpというファイルを作りました.ここに,色々と実装を書いていきます.他のサービスをコピペして改造しただけなんですがね・・・.

まずは,APIの仕様を確認しておきましょう.
http://code.google.com/intl/ja/apis/accounts/docs/OAuth2Login.html#userinfocall
なるほど.なるほど.
認証周りはライブラリがやってくれているみたいなので,それ以外を実装して行きましょう.

必要なファイルをインクルードしておきます.

<?php
require_once 'service/apiModel.php';
require_once 'service/apiService.php';
require_once 'service/apiServiceRequest.php';
...
?>

データ構造を表すモデルを定義します.

<?php
...
class Userinfo extends apiModel {
  public $id;
  public $email;
  public $verified_email;
  public $name;
  public $given_name;
  public $family_name;
  public $picture;
  public $locale;
  public $timezone;
  public $gender;
}
?>

各属性の意味に関しては,上のURLを見てください.

最後にサービスを実装します.

<?php
...
class UserinfoServiceResource extends apiServiceResource {

  public function get($optParams = array()) {
    $params = array();
    $params = array_merge($params, $optParams);
    $data = $this->__call('get', array($params));
    if ($this->useObjects()) {
      return new Userinfo($data);
    } else {
      return $data;
    }
  }
}

class apiUserinfoService extends apiService {
  public $userinfo;
  /**
   * Constructs the internal representation of the Userinfo service.
   *
   * @param apiClient apiClient
   */
  public function __construct(apiClient $apiClient) {
    $this->rpcPath = '/rpc';
    $this->restBasePath = '/oauth2/v1/';
    $this->version = 'v1';
    $this->serviceName = 'userinfo';
    $this->io = $apiClient->getIo();

    $apiClient->addService($this->serviceName, $this->version);
    $this->userinfo = new UserinfoServiceResource($this, $this->serviceName, 'userinfo', json_decode('{"methods": {"get": {"scopes": ["https://www.googleapis.com/auth/userinfo.profile", "https://www.googleapis.com/auth/userinfo.email"], "parameters": {}, "id": "userinfo.get", "httpMethod": "GET", "path": "userinfo", "response": {"ref": "Userinfo"}}}}', true));
  }
}
...
?>

他のとこからの改造なのでよくわかっていないとこもあるのだが,こんなふうになった.ServiceResourceクラスは,完全にラッパーなような感じがする.主なリソースの設定はjson_decode内のjsonで定義していて,メソッドごとにscopes,parameters,id*2,httpMethod,path,responsを設定するみたいです.

使い方としては,まずconfig.phpにOAuth2関係の設定を書きます.後,一番下にある*3,servicesを設定します.

<?php
...
'services' => array(
  'userinfo' => array(
        'scope' => array('https://www.googleapis.com/auth/userinfo.profile', 'https://www.googleapis.com/auth/userinfo.email')
  ) // other services ...
)
?>

そして,認証と取得を書きます.

<?php
session_start();

require_once "../src/apiClient.php";
require_once "../src/contrib/apiUserinfoService.php";

$apiClient = new apiClient();
$apiClient->setUseObjects(true);
$service = new apiUserinfoService($apiClient);

if (isset($_SESSION['oauth_access_token'])) {
  $apiClient->setAccessToken($_SESSION['oauth_access_token']);
} else {
  $token = $apiClient->authenticate();
  $_SESSION['oauth_access_token'] = $token;
}

$userinfo = $service->userinfo->get(); // Get userinfo data.
print_r($userinfo);

...
?>

という感じで,取得できるようになってます.*4

全体のコードをGistにあげときました.
https://gist.github.com/1523354

*1:あるなら教えてください...

*2:よくわかっていない

*3:はず

*4:Google Calendar APIに載っているものを改造しました