1
votes

I wrote a PHP application which tries to create an User in my Google Directory. I don't use the Google Libraries. I succeded making requests to the Android Enterprise API. I can enroll and unenroll Enterprise Service Accounts with my MSA. So I assume my Code for the JWT and Requests work.

I created a Service Account and enabled "Domain Wide Delegation" and added the following permission "https://www.googleapis.com/auth/admin.directory.user, https://www.googleapis.com/auth/admin.directory.group" to my API Client under the "Manage API client access" windows. My Service Account has the status role "Editor" in the "Permissions for project" windows.

So first my script gets the Bearer token from the Google Server, for that I create a JWT and encrypt it with my private key. The Token contains the following fields

"iss" => [email protected]
"scope" => "https://www.googleapis.com/auth/admin.directory.user, https://www.googleapis.com/auth/admin.directory.group"
"aud" => "https://www.googleapis.com/oauth2/v4/token",
"exp" => timestamp+3000,
"iat" => timestamp

When I do that request I get a bearer token, but when I use that token to make the insert request I always get the message "Not Authorized to access this resource/api" with an HTTP 403. When I add the field "sub" to my JWT and specify the email of the Project admin "[email protected]" I can't even get the bearer token, then I get a 401 error with the following message "Unauthorized client or scope in request."

After that I tried something "easy", I just wanted to get a list of all users in my directory. But the Google Server just reponds with an "bad request" error. I got the same error with the API Explorer which is on API Page. Maybe the API is broken ? At least the API Explorer should work. https://developers.google.com/admin-sdk/directory/v1/reference/users/list

Do you have some ideas why I can't create users with my service account ?

(I had to insert some spaces in the scopes and urls because I'm not allowed to post more than two links)

Greetings Philip

2

2 Answers

0
votes

Adding the sub claim is the right thing to do, because you must impersonate a super admin to use Directory API. If you get a "Unauthorized client or scope in request" response, that might be because there's a typo in the service account client ID you used to authorize (or the scopes), or that not enough time has passed (it usually propagates within a few minutes, but could take up to 24 hours).

See JWT error codes for more details on possible errors and causes.

-1
votes

Do you have some ideas why I can't create users with my service account?

Yes. Your service account seems to have no authority to create users. Check your Service Account's role in GDC to check if it's Owner, Editor, Viewer,etc. The scope of what they can do depends on that. Watch this Google video for a live demo.

Also, try to read more on Using OAuth 2.0 for Server to Server Applications.