LoginSignup
1
0

More than 3 years have passed since last update.

【2021年版】RubyでGoogle Search Console APIを利用する

Posted at

RubyでGoogle Search Console APIを利用したいが、ライブラリのアップデートが激しく、過去の情報しか残っていないため使い物にならなかったのでだいぶハマった。とりあえず2021年3月31日時点で動いたので、メモ。

過去のものをみる感じ、google-api-clientのバージョンアップでライブラリそのものが使えなくなっていたりするので、公式のGithubレポジトリをみながら頑張った。

Gemfileのインストール

Gemfile

gem 'google-apis-searchconsole_v1', '~> 0.1'

models/search_console.rb

## models/search_console.rb
require "google/apis/searchconsole_v1"

class SearchConsole
  def initialize
    authorizer = Google::Auth::ServiceAccountCredentials.make_creds(
      json_key_io: File.open(ENV["SERVICE_ACCOUNT_CREDENTIAL_FILE_PATH"]),
      scope: %w(
        https://www.googleapis.com/auth/webmasters
      )
    )
    @client = Google::Apis::SearchconsoleV1::SearchConsoleService.new
    @client.authorization = authorizer
  end
end

GCPでSearch Consoleのプロジェクトの有効化、サービスアカウントでのJSON KEYのダウンロードなど必要なので、参考資料をみてください!

参考

1
0
0

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
1
0