I am trying to get or list devices via the Device API in Golang but I keep getting Error 403: The caller does not have permission, forbidden
. I did the following steps:
- Created a service account and retrieved the service account key in .json format. Then I enabled Wide domain delegation using the numeric ID of the service account with the scopes:
- "https://www.googleapis.com/auth/cloud-platform"
- "https://www.googleapis.com/auth/cloud-identity"
- "https://www.googleapis.com/auth/cloud-identity.devices"
- Added the following permissions to the service account:
$ gcloud iam service-accounts get-iam-policy example@example.iam.gserviceaccount.com
bindings:
- members:
- user:mysuperadminuser@example
role: roles/iam.serviceAccountAdmin
- members:
- serviceAccount:example@example.iam.gserviceaccount.com
- mysuperadminuser@example
role: roles/iam.serviceAccountTokenCreator
- members:
- user:mysuperadminuser@example
role: roles/iam.serviceAccountUser
- members:
- serviceAccount:example@example.iam.gserviceaccount.com
- user:mysuperadminuser@example
role: roles/owner
- Then I have the following code to list devices and groups, impersonating an admin user (myself):
func getDevicesAndGroups(ctx context.Context, credentialsFile string) {
data, _ := ioutil.ReadFile(credentialsFile) // json file from the service account
config, _ := google.JWTConfigFromJSON(data, "https://www.googleapis.com/auth/cloud-platform", "https://www.googleapis.com/auth/cloud-identity", "https://www.googleapis.com/auth/cloud-identity.devices")
config.Subject = "mysuperadminuser@example"
ts := config.TokenSource(ctx)
httpClient := config.Client(ctx)
ciService, err := cloudidentity.NewService(ctx, option.WithTokenSource(ts))
ds := cloudidentity.NewDevicesService(ciService)
resp, err := ds.List().Customer("customers/my_customer").PageSize(1).OrderBy("email").Do()
log.Infof("NewDevicesService resp %#v", resp)
if err != nil {
log.Errorf("Unable to retrieve devices: %v", err)
}
gs := cloudidentity.NewGroupsService(ciService)
respgs, err := gs.List().Parent("customers/my_customer").PageSize(1).Context(ctx).View("BASIC").Do()
log.Infof("NewGroupsService %#v", respgs)
if err != nil {
log.Errorf("Unable to retrieve groups: %v", err)
}
}
However the call to list the devices keeps returning the following error:
Error 403: The caller does not have permission, forbidden
But the call to list the groups gives me a 200 response code
, meaning the requests returns result successfully.
Was wondering if anyone could point me to the correct direction of permissions that I might be missing?