My script pulls numbers out of Search Console and has been getting
unauthorized_client since today. Work out which rights it asks for and
which were granted, and show me both lists.
You have a script that pulls the Search Console numbers every week. It runs with a service account acting on behalf of a user. It worked, and now fetching a token gives you:
unauthorized_client: Client is unauthorized to retrieve access tokens
using this method, or client not authorized for any of the scopes
requested.
The error names two possible causes and the second one is nearly always the right one. The account is not the problem, the list of rights you are asking for is.
How this connection works
A service account is an account with no person behind it. For Search Console that is not enough: the numbers belong to a property and that property belongs to a user. So the service account acts as that user. That is called impersonation, and in Google Workspace you enable it per service account.
When you enable it you supply two things: the client id of the service account, and a list of rights (scopes) this account may request on behalf of users. That is an allow list.
On every token request your script asks for a list of scopes. And here is the catch.
The rule: everything you ask for must be granted
If your script asks for three scopes and two of them are on the allow list, you do not get a token for those two. You get no token at all. It is not an intersection, it is all or nothing.
That is exactly what happens with a script that asks for something extra out of habit. A common case is a helper that, alongside the right to read Search Console, also asks for the right to verify site ownership, because that was needed once during setup. That second right is not on the allow list, so you get unauthorized_client for the whole request.
The fix is not to add the second right. The fix is not to ask for it. Request exactly one scope: read access to Search Console.
Why it seems to work without impersonation
This is the confusing part. Fetch a token without impersonating a user and you simply get one, extra scope and all. The allow list only applies to impersonation.
That token just does not get you anywhere. A service account being itself has no Search Console properties, so every call afterwards gives an empty list or an access error. And then you go looking in Search Console for a permissions problem that is not there.
So always test with the user you impersonate in production. A token you fetch without impersonation proves nothing.
How to track it down
Three steps, in this order.
1. Which scopes does your script ask for? Look in your code or your helper script for the scope list. It is often in a variable with a default value that you override with an environment variable. That is exactly the place where a default was once extended and nobody looked again.
Find in this script which scopes it requests, including the default
when no environment variable is set.
2. Which scopes were granted? Those live in the Google Workspace admin console, under domain wide delegation, on the client id of the service account. Compare the two lists literally, character by character. Scopes are full web addresses, and an address that is slightly different counts as a different scope.
3. Ask only for what you use. If your script only reads numbers, you only need the read scope. Anything extra you request is something that can break without buying you anything.
Two things that look like this
It worked and now it does not, with nothing changed. Usually something did change, just not by you. An administrator tidying up the allow list removes a scope that was still in use. Put the date of the first failed call next to the changes in the admin console.
One domain works and another does not. That is not a scope problem but a property problem: the user you impersonate has to have access to that property. A property that sits on another account is invisible to you no matter how good your rights are.
Keep the two kinds of rights apart
There are two layers and they get mixed up.
- The scope says what kind of actions this token may perform. You arrange that in the Workspace admin console.
- Access to a property says on which websites you may perform them. You arrange that in Search Console itself, under the users of that property.
An error while fetching the token is about the first. An empty list or an access error while requesting numbers is about the second. Once you know which of the two you are looking at, you only have one place to check.
Common mistakes
Requesting a scope you do not use. That is the cause in most cases. Ask for the read scope only.
Extending the allow list instead of shrinking the request. That works too, and leaves you with a service account allowed to do more than it needs. That is a risk you do not have to take.
Testing without impersonation. You get a token, you think it is fine, and it still fails in production.
Adding up scopes across several scripts. Each script asks for what it needs itself. A shared helper requesting the sum of all scopes breaks as soon as one of them is not granted.
Reading the error as "this account may do nothing". It says the account is not authorised for one of the requested rights. That is a much smaller problem, and it sits in your own code.