Using Google Search Console APIs to get a list of all sites in your account

I wanted to get a list of all websites in a users Google Search console account and was stuck as the API documentation was poor. This is how I solved it incase anyone else is looking for help on the same.

This post looks at how you can retrieve all the sites in Google Search console account via the API (Ruby).

Google Search Console API's are really useful to get more data from your account, but the documentation of the same from Google is lacking. Especially if you are using Ruby or PHP as your programming language, you will find that most scenarios don't have code samples unlike python or java.

I wanted to get a list of all websites in a users Google Search console API and was stuck as the documentation was poor. This is how I solved it incase anyone else is looking for help on the same.

These are the steps to follow to get a list of all sites in your GSC account.

1. Enable the Search Console API

This is fairly well documented and easy to do. Go to your console.developers.google.com account and create a new project. You need to create an oAuth client ID and also add the Google Search console api to your library. The process is similar to Python or other programming languages.

2. Install the Google API client gem

If you haven't installed the Google API client library for Ruby before, you need to add the following gem

$ gem install google-api-client

3. Request Google search console access for your user

Once you have setup the API, you need to get your user to give access to their GSC account. You can do that with the following code:

client = Signet::OAuth2::Client.new({
      client_id: "XXXXX",
      client_secret: "XXXXX",
      authorization_uri: 'https://accounts.google.com/o/oauth2/auth',
      scope: 'https://www.googleapis.com/auth/webmasters.readonly',
      access_type: "offline",
      redirect_uri: url_for(:action => :google_search_console_callback)
    })
    redirect_to client.authorization_uri.to_s

Make sure you have a callback url set up in your routes. This is where the API will return your request. You also need to specify this URL in your console.developers.google.com account. 

4. Save the access token and refresh token

Make sure to get offline access if you need to access the APIs later. You can do that with the following code. 

client = Signet::OAuth2::Client.new(
      :authorization_uri => 'https://accounts.google.com/o/oauth2/auth',
      :token_credential_uri =>  'https://www.googleapis.com/oauth2/v3/token',
      :client_id => 'XXXXX',
      :client_secret => 'XXXXX',
      :code => params[:code],
      :access_type => "offline",
      :redirect_uri => url_for(:action => :google_search_console_callback)
    )
    response = client.fetch_access_token!

Use the client.fetch_access_token! command to get the response. In this response, you will have all the details which you need to store in your database for the user. 
E.g 

def update_analytics(response)
    self.update(
      analytics_token: response['access_token']
    )
    if response['refresh_token'] != nil
      self.update(
        analytics_refresh: response['refresh_token']
      )
    end
  end

5. Getting a list of all websites in the users account.

You can get a list of all the sites in the users account with the following code. 

client = Signet::OAuth2::Client.new(access_token: @website.analytics_token)
    client.expires_in = Time.now + 1_000_000
    service = Google::Apis::WebmastersV3::WebmastersService.new
    service.authorization = client
    begin
      @all_accounts = service.list_sites
    rescue Google::Apis::ClientError => e
      @error = e.message
    end


Recent Blogs