"Diary" インターネットさんへの恩返し

いつもソースコードコピペばかりなので,みなさまへ少しばかりの恩返しを

403 Forbidden が出て困っている人へ、Google Index APIのPHPでの実装の仕方



スポンサーリンク

Google API の実装の仕方でいろいろてこずったので同じ罠に皆さんはまらないように共有です。

大まかなGoogle API 実装手順は以下の手順。

1)Goolge API Consoleでお使いのgmailアカウントを開発者アカウント登録(まだやっていない人は)

2)Goolge API Consoleでプロジェクトを作成

3)そのプロジェクトでIndex APIを有効化

4)そのプロジェクトでサービスアカウント(xxxxxx@xxxxxgserviceaccount.comみたいなやつ)を作成

5)サービスアカウントからキーファイルを発行

6)Goolge Search Consoleでサイト登録(やってない人は)

7)Goolge Search Consoleで4)で作ったxxxx@xxxxxxxgserviceaccount.com アカウントをサイトの所有者として登録
 ↑ ここてこずった。何回やってもphpのプログラムの結果が 403 Forbidden とでて何が問題か調べるのに時間がかかった。

 結果的に以下のオペレーションで所有者登録すれば解決した。

f:id:azumami:20190404185025p:plain

f:id:azumami:20190404185233p:plain


うまくいったら出る画面

testController.php on line 57:
Response {#603 ▼
  -reasonPhrase: "OK"
  -statusCode: 200
  -headers: array:11 [▼
    "Content-Type" => array:1 [▶]
    "Vary" => array:3 [▶]
    "Date" => array:1 [▶]
    "Server" => array:1 [▶]
    "Cache-Control" => array:1 [▶]
    "X-XSS-Protection" => array:1 [▶]
    "X-Frame-Options" => array:1 [▶]
    "X-Content-Type-Options" => array:1 [▶]
    "Alt-Svc" => array:1 [▶]
    "Accept-Ranges" => array:1 [▶]
    "Transfer-Encoding" => array:1 [▶]
  ]
  -headerNames: array:11 [▼
    "content-type" => "Content-Type"
    "vary" => "Vary"
    "date" => "Date"
    "server" => "Server"
    "cache-control" => "Cache-Control"
    "x-xss-protection" => "X-XSS-Protection"
    "x-frame-options" => "X-Frame-Options"
    "x-content-type-options" => "X-Content-Type-Options"
    "alt-svc" => "Alt-Svc"
    "accept-ranges" => "Accept-Ranges"
    "transfer-encoding" => "Transfer-Encoding"
  ]
  -protocol: "1.1"
  -stream: Stream {#591 ▶}
}
testController.php on line 58:
200

以上。