LoginSignup
6

More than 5 years have passed since last update.

PHPで、Google Clientを作成する(サービスアカウント)

Last updated at Posted at 2018-03-24

目的

PHPで、Googleサービスに接続する際に作成した内容のメモ。
サービスアカウントを使用するパターンです。

サービスアカウント

Google Cloud Platform から使用するプロジェクトを選択し、APIサービスの認証情報からサービスアカウントを作成し、jsonキーを用意します。

Google APIs Client Library for PHP

GitHub Google APIs Client Library for PHP

composer require google/apiclient:^2.0

でインストール。

Google Clientの作成

require_once 'vendor/autoload.php';

define('CREDENTIALS_PATH', 'サービスアカウントキーのファイルパス');

putenv('GOOGLE_APPLICATION_CREDENTIALS='.CREDENTIALS_PATH);
$client = new Google_Client();
$client->useApplicationDefaultCredentials();

$client->addScope(Google_Service_Sheets::SPREADSHEETS);
$service = new Google_Service_Sheets($client);

以上。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6