google-apiclient icon indicating copy to clipboard operation
google-apiclient copied to clipboard

Google Services work-around?

Open tommiepommieporretje opened this issue 8 years ago • 1 comments

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:

  • create a service account in your Google App Console
  • give the proper scopes in your Google Admin security (advanced) settings
  • have this composer plugin installed in L5 as found in the readme

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 protected]');
    $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?

tommiepommieporretje avatar Jun 07 '17 21:06 tommiepommieporretje

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

github-actions[bot] avatar Jul 01 '24 05:07 github-actions[bot]