非公開のGoogle SheetをPHPで操作するメモ。
まずは、Google Cloud Platformにてプロジェクトを作成する。
上部の”プロジェクトの選択”をクリック。
![](https://taak.biz/wp-content/uploads/2020/09/gcp01-edited.png)
“新しいプロジェクト”をクリック。
既存プロジェクトを選択してもOK。
![](https://taak.biz/wp-content/uploads/2020/09/Google_Cloud_Platform02.png)
新しいプロジェクト登録画面では、プロジェクト名を入力。
変えたければ、プロジェクトIDも変えて”作成”ボタンを押す。
![](https://taak.biz/wp-content/uploads/2020/09/Google_Cloud_Platform03.png)
上部の”プロジェクトの選択”から作成したプロジェクトを選ぶ。
左上のハンバーガーメニューを開き、APIとサービスの子メニュー”ライブラリ”を選択する。
![](https://taak.biz/wp-content/uploads/2020/09/gcp04.png)
検索ボックスに”sheet”と入力する。
![](https://taak.biz/wp-content/uploads/2020/09/gcp05-1.png)
Google Sheets API が出てくるので選択する。
![](https://taak.biz/wp-content/uploads/2020/09/gcp06.png)
有効にするボタンを押す。
![](https://taak.biz/wp-content/uploads/2020/09/gcp07.png)
認証情報をクリック。
![](https://taak.biz/wp-content/uploads/2020/09/gcp08.png)
認証情報を作成をクリック。
![](https://taak.biz/wp-content/uploads/2020/09/gcp09.png)
サービスアカウントを選択。
![](https://taak.biz/wp-content/uploads/2020/09/gcp10.png)
サービスアカウント名に名前を登録し、必要に応じてその他の情報も入力して作成ボタンを押す。
![](https://taak.biz/wp-content/uploads/2020/09/gcp11.png)
サービスアカウントの権限では、特に問題が無い限りProjectのオーナーを選んで続行ボタンを押す。
![](https://taak.biz/wp-content/uploads/2020/09/gcp12.png)
![](https://taak.biz/wp-content/uploads/2020/09/gcp13.png)
次のサービスアカウントへのアクセス権を付与はオプションなので無視して完了ボタン。
![](https://taak.biz/wp-content/uploads/2020/09/gcp15.png)
プロジェクトのサービスアカウント一覧画面になるので、今作成したサービスアカウントをクリック。
![](https://taak.biz/wp-content/uploads/2020/09/gcp16.png)
サービスアカウントの詳細の一番下、鍵を追加を押す。
![](https://taak.biz/wp-content/uploads/2020/09/gcp17.png)
新しい鍵を作成、キーのタイプはJSONを選んで作成を押す。
![](https://taak.biz/wp-content/uploads/2020/09/gcp18.png)
秘密鍵がダウンロードされるのでサーバーに置いておく。
最後に、サービスアカウントで作成されたメールアドレスをアクセスしたいGoogle Sheetに共有して終了。
共有を忘れると下記のようなエラーが出る。
{ "error": { "code": 403, "message": "The caller does not have permission", "errors": [ { "message": "The caller does not have permission", "domain": "global", "reason": "forbidden" } ], "status": "PERMISSION_DENIED" } }
Google APIを使用する準備はここまで。