If you want to use R to access APIs automatically, then authorization via browser is not an option. The solution is called Service User: With a Service User and the associated JSON file, an R program can access the Google Analytics API, the Google Search Console API, but also all the other wonderful machine learning APIs. This short tutorial shows what needs to be done to connect to the Google Search Console.

First, you create a project if you don’t have a suitable one yet, and then the appropriate APIs have to be enabled. After a short search, you will find the Google Search DesktopAPI, which only needs to be activated.

Click on IAM & admin and then click on Service accounts (it’s kind of strangely highlighted in this screenshot:

Click Create Service Account:

Important: Give a meaningful name for the e-mail address, so that you can at least keep track of it a bit…
Browse Project is sufficient for this step:

Click on “Create Key” here:

Create a JSON key, download it and then put it in the desired RStudio directory.

Now the service user must be added as a user in the Search Console; the important thing is that he receives all rights.

What’s really great about the Google Search Console API is that you can see the query and landing page at the same time, unlike in the GUI. By the way, I get the data every day and write it to a database, so I have a nice history that goes beyond the few months of Search Console.

Last but not least, I also provide the R notebook, with which I query the data; it’s basically the code written by the author of the API, Mark Edmondson, but just for the sake of completeness, how the JSON file is included. There is a more elegant variant with R Environment variables, but I don’t know if it works on Windows.