0

I cannot connect the two pieces together: I am able to authenticate my service account and I know how to form the request for the information I need to retrieve from the API, but I cannot figure out how to make that request authenticated with the token.

This builds an object from the credential file I got for the service account and generates token successfully:

require 'googleauth'

require 'google/apis/webmasters_v3'

website = "https://example.com"

scope = 'https://www.googleapis.com/auth/webmasters'
authorizer = Google::Auth::ServiceAccountCredentials.make_creds(
  json_key_io: File.open('service-account.json'),
 scope: scope)
token = authorizer.fetch_access_token!

The final request is like

wt_service.query_search_analytics(website, {
  "startDate"=> "2019-12-01",
  "endDate"=> "2019-12-02",
  "dimensions"=> [
    "query"
  ]
})

But the object for the webmaster tools API endpoint does not accept any arguments:

wt_service = Google::Apis::WebmastersV3::WebmastersService.new

But I need to somehow build it in an authenticated way, that is with the token - but how if I cannot add arguments?!

How do I build the object with the token?

=====

The response from Chadwick Wood solved it. There was a followup issue because Google API was using inconsistent variable names, so I'm pasting the full working code below (Works after adding the service user to webmaster tools user list for the domain)

require 'googleauth'
require 'google/apis/webmasters_v3'

website = "https://example.com"

scope = 'https://www.googleapis.com/auth/webmasters'
authorizer = Google::Auth::ServiceAccountCredentials.make_creds(json_key_io: File.open('service-account.json'),scope: scope)
token = authorizer.fetch_access_token!
wt_service = Google::Apis::WebmastersV3::WebmastersService.new
wt_service.authorization = authorizer
wt_service.authorization.apply(token)

request_object = {
  start_date: "2019-02-01",
  end_date: "2020-02-28",
  dimensions: ["query"]
}
params = Google::Apis::WebmastersV3::SearchAnalyticsQueryRequest.new(request_object)

res = wt_service.query_search_analytics(website, params)
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449

1 Answers1

0

I think you just have to add:

wt_service.authorization = authorizer

... after you create the service, and before you make the query. So:

authorizer = Google::Auth::ServiceAccountCredentials.make_creds(json_key_io: File.open('service-account.json'), scope: scope)
wt_service = Google::Apis::WebmastersV3::WebmastersService.new
wt_service.authorization = authorizer
wt_service.query_search_analytics ...
Chadwick Wood
  • 2,012
  • 1
  • 15
  • 9
  • How do you delegate the service account access to your account so that it can read the data? – Linda Lawton - DaImTo Jun 08 '20 at 06:29
  • That has to be done using the Google Cloud Platform. https://console.cloud.google.com/ Google has some docs about setting up service accounts in the Help, I believe. – Chadwick Wood Jun 09 '20 at 23:00
  • As far as i know you cant delegate access to a service account to WEb master tools api so i would like you to explain how you do this. Since even the documentation does not mention you can use a service account with this API. Google cloud console has nothing to do with setting up the deligation of a service account to users data contained within web master tools. How do you give it access to that data. – Linda Lawton - DaImTo Jun 10 '20 at 07:01
  • It's a multi-step process, but essentially, yes, as I said you do it through the Google Cloud Platform. Create a project. Then add the Google Search Console API to that project (under Library). Then go to Credentials and click Create Credentials, create a Service account, and give it access to the Search Console API. If you have specific questions I'll try to answer, but at least try before telling me I'm wrong. ;) – Chadwick Wood Jun 11 '20 at 21:14
  • If you're looking for something other than Search Console access, I don't know specifically about that. – Chadwick Wood Jun 11 '20 at 21:17