pulkitjalan / google-apiclient

Google api php client wrapper with Cloud Platform and Laravel support
MIT License
246 stars 75 forks source link

Google Services work-around? #26

Open tommiepommieporretje opened 7 years ago

tommiepommieporretje commented 7 years ago

I'm an Google Admin of my own organization. Google API keys and API access all setup and ready to go. My goal is to manage Gmail accounts without OAuth auth, so I'm forced to use the Google Service Account. To get all users - as an example - I had to create a workaround.

To reproduce:

Change the scopes in your config/google.php file:

    'scopes'          => [
        'https://www.googleapis.com/auth/admin.directory.user'
    ],

Add these values in your .env file:

GOOGLE_SERVICE_ENABLED=true
GOOGLE_SERVICE_ACCOUNT_JSON_LOCATION='/path/to/your/file.json'

Now try to gain access to your users:

Route::get('no-users', function()
{
    $service = Google::make('directory'); // according to readme
    $results = $service->users->listUsers(); // call to get a list of users

    dump($results); // Google_Service_Exception 400 Bad Request
});

To get it working, I ended up using this...

Route::get('users', function()
{
    $client = new \PulkitJalan\Google\Client(config('google'), 'email@mydomain.com');
    $service = new Google_Service_Directory($client->getClient());

    $optParams = array(
      'domain' => 'mydomain.com', //required
    );

    $results = $service->users->listUsers($optParams);

    dump($results->users);
});

Isn't there a better way to handle this?

github-actions[bot] commented 6 days ago

This issue is stale because it has been open 21 days with no activity. Remove stale label or comment or this will be closed in 7 days