googleapis / google-api-php-client

A PHP client library for accessing Google APIs
Apache License 2.0
9.2k stars 3.52k forks source link

Problem retrieving domain user list #2564

Closed claudiobat closed 3 months ago

claudiobat commented 4 months ago

Hi, I'd like to get the list of the users of the domain using "google-api-php-client". As SuperAdmin on the Google backend I have:

However, when executing the "listUsers" method I have the error:

{ "error": { "code": 403, "message": "Not Authorized to access this resource/api", "errors": [ { "message": "Not Authorized to access this resource/api", "domain": "global", "reason": "forbidden" } ] } }

This is the code I'm using:

` $serviceAccountPath = base_path().'/config_google.json'; $credentials = json_decode(file_get_contents($serviceAccountPath), true);

    $this->client = new Client();
    $this->client->setApplicationName('Google Workspace Directory API');
    $this->client->setAuthConfig($credentials);
    $this->client->setScopes([
        'https://www.googleapis.com/auth/admin.directory.domain',
        'https://www.googleapis.com/auth/admin.directory.user',
        'https://www.googleapis.com/auth/admin.directory.group',
    ]);

    $service = new Directory($this->client);
    $users = $service->users->listUsers([
        'domain' => 'mydomain.com',
    ]);

` What could be the problem? How can I solve it? Thank you very much

Claudio

claudiobat commented 3 months ago

Hi all, I've solved specifying the "sub" parameter in the request.

claudio