« centos6のデフォルトphp5.3.3を5.6.*へ | メイン | php 4.4.1 開発環境構築 »

php

Google OAuth 2.0 サーバーサイド設定

 Googleアカウントと連携するためのサーバーサイド設定 備忘録。
 (旧)Google API Client Services は Google API PHP Client 0.6.7 までで 今は GitHub で公開。
https://code.google.com/archive/p/google-api-php-client/
https://code.google.com/archive/p/google-api-php-client/downloads
●Google API Client Libraries PHP (Beta)のインストール
https://developers.google.com/api-client-library/php/start/installation
で、GitHub の v1-master リポジトリをクローンする旨記述があるものの 結果的には不要。
× → git clone -b v1-master https://github.com/google/google-api-php-client.git
 カレントリポジトリも同様で、これに含まれる /src/Google/Service/~ も別リポジトリに枝分かれ。
 サンプルで使われている Google_Service_Drive_FileList::getItems() 等が未実装。
 ちなみにドキュメントではPHP 5.2.1 以上、GitHub では PHP 5.4.0 以上、試した環境は centos6.7 PHP 5.3.3。

 結果的には、composer による設置で十分...▼
●composer.pharをダウンロード
$ curl -sS https://getcomposer.org/installer | php
Downloading 1.1.1...

Composer successfully installed to: /media/sf_share/www/v1/app/composer.phar
Use it: php composer.phar
Some settings on your machine may cause stability issues with Composer.
If you encounter issues, try to change the following:

Your PHP (5.3.3) is quite old, upgrading to PHP 5.3.4 or higher is recommended.
Composer works with 5.3.2+ for most people, but there might be edge case issues.
PHPはいずれアップグレード必要のところ、不具合が現れるまで様子見。

●パスが通った場所にcomposerコマンドとして配置。
$ sudo -s mv composer.phar /usr/local/bin/composer

●ライブラリと同じパスに composer.json ファイルを作成。
{
	"require":{
		"google/apiclient":"1.0.*@beta"
	}
}

●ライブラリパスで google/apiclient をインストール。
$ composer install
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing google/apiclient (1.0.6-beta)
    Downloading: 100%

Writing lock file
Generating autoload files
vendor フォルダができる。
ライブラリパス
└vendor
   ├─composer
   └─google
       └─apiclient
           ├─examples ★
           │  │appengineauth.php
           │  │batch.php
           │  │fileupload.php
           │  │idtoken.php ★★★
           │  │index.php
           │  │key.p12
           │  │multi-api.php
           │  │service-account.php
           │  │simplefileupload.php
           │  │simple-query.php
           │  │user-example.php
           │  ├─styles
           │  └─templates
           ├─src ★★
           │  └─Google
           │      ├─Auth
           │      ├─Cache
           │      ├─Http
           │      ├─IO
           │      ├─Service
           │      ├─Signer
           │      ├─Utils
           │      └─Verifier
           ├─style
           └─tests
★パスをWebサーバーのドキュメントルートとするか、★内サンプルファイルをWebサーバーのドキュメントルートへコピー。
この時点で、ブラウザから http://example.asia/index.php を表示可能(example.asia は、ブラウザが稼動するOSのhostsファイルで指定)。

●サンプルの1つ★★★"•An example of verifying and retrieving the id token."(idtoken.php)を動作させる必要最小限の設定。
https://console.developers.google.com/
Google API を使うGoogleアカウントでログイン。

●Google Developers Console の「IAMと管理」→「プロジェクト作成」
https://console.developers.google.com/iam-admin/projects

●「API Manager」で利用するAPIを個々に有効化。
ここでは、Google Apps API の 「Contacts API」(以前 名称~auth~が Google Contacts API version 3.0 へ名称が変化)。
これをしないと後のサンプルで下記のようなエラーが発生。
PHP Fatal error:  Uncaught exception 'Google_Service_Exception' with message 'Error calling GET https://www.googleapis.com/drive/v3/files: (403) Access Not Configured. Drive API has not been used in project ・・・ before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/drive/overview?project=・・・ then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.' in /・・・/src/Google/Http/REST.php:110
Stack trace:
#0 /・・・/src/Google/Http/REST.php(62): Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request), Object(Google_Client))
#1 [internal function]: Google_Http_REST::doExecute(Object(Google_Client), Object(Google_Http_Request))
#2 /・・・/src/Google/Task/Runner.php(174): call_user_func_array(Array, Array)
#3 /・・・/app/google-api-php-client/src/Google/Http/REST.php(46):  in /・・・/src/Google/Http/REST.php on line 110
(その他のサンプルでは、他に"Drive API / Google Apps API"、"Google Books API"、"URL Shortener API"などが有効化必要。)

●「認証情報」の「OAuth同意画面」で、適当なサービス名を指定。
Google API を使うGoogleアカウントのメールアドレスが知られる。

●「認証情報」の「認証情報を作成」で「OAuthクライアントID」を作成。
適当な「名前」を指定。
「承認済みのリダイレクト URI」◆に"http://example.asia/idtoken.php"を指定。
(IPアドレスがNGなので example.asia などとしている)
「クライアント ID」◆◆と「クライアント シークレット」◆◆◆を控えておきサンプルプログラムに埋め込む。
今はこれらのハードコーディングから client_secret~.json ファイルを生成・ダウンロードさせ読み込ませるスタイルが紹介されている。
(その他のサンプルでは、「OAuthクライアントID」の他「APIキー」の作成が必要なものも含まれる。)

● ★★★のidtoken.phpファイルを適宜修正して完成。
★内サンプルファイルをWebサーバーのドキュメントルートへコピーしたのであれば 20行目の set_include_path を修正。
28,29,30,84,85,86行目にGoogle Developers Console で控えた値を埋め込む。
→ 「クライアント ID」◆◆
→ 「クライアント シークレット」◆◆◆
→ 「承認済みのリダイレクト URI」◆

トラックバック

このエントリーのトラックバックURL:
https://www.remix.asia/cgi/mt/mt-tb.cgi/7525

コメントを投稿

(いままで、ここでコメントしたことがないときは、コメントを表示する前にこのブログのオーナーの承認が必要になることがあります。承認されるまではコメントは表示されません。そのときはしばらく待ってください。)