boyfinal / google-api-php-client

Automatically exported from code.google.com/p/google-api-php-client
Apache License 2.0
0 stars 0 forks source link

Unable to Auth to Google_DirectoryService #440

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I have the following code in a simple php file.

The goal is to create a Wordpress plugin to add/remove emails to the Groups in 
Google Apps. 

-------------------
<?php 
require_once "google-api-php-client/src/Google_Client.php";
require_once "google-api-php-client/src/contrib/Google_DirectoryService.php";
require_once "google-api-php-client/src/contrib/Google_Oauth2Service.php";
session_start();

$CLIENT_ID = 'xxxxx.apps.googleusercontent.com';
$SERVICE_ACCOUNT_EMAIL = 'xxxxx@developer.gserviceaccount.com';
$SERVICE_KEY_FILE = 
'./google-api/6d8d7e22a06a599165023d89993ef3cfff58aea7-privatekey.p12';

$GROUP_SCOPE = 'https://www.googleapis.com/auth/admin.directory.group';

$client = new Google_Client();
$client->setApplicationName("wp-group-members-plugin");
$client->setClientId(CLIENT_ID); // from API console

// set assertion credentials
$assertionCredentials = new Google_AssertionCredentials($SERVICE_ACCOUNT_EMAIL, 
$GROUP_SCOPE, file_get_contents($SERVICE_KEY_FILE));
$assertionCredentials->generateAssertion();
$client->setAssertionCredentials($assertionCredentials);

$service = new Google_DirectoryService($client);
$member = new Google_Member(array('email' => 'abc@testing.com', 'role' => 
'MEMBER', 'type' => 'USER'));

$results = $service->members->insert('admins@mydomain.org', $member);
print '<h2>Response Result:</h2><pre>' . print_r($results, true) . '</pre>';
-------------------

I'm getting the following error 

Fatal error: Uncaught exception 'Google_ServiceException' with message 'Error 
calling POST 
https://www.googleapis.com/admin/directory/v1/groups/admins@stgeorgeseattle.org/
members: (403) Not Authorized to access this resource/api' in 
/home2/gdawoud/public_html/test/google-api-php-client/src/io/Google_REST.php:66 
Stack trace: #0 
/home2/gdawoud/public_html/test/google-api-php-client/src/io/Google_REST.php(36)
: Google_REST::decodeHttpResponse(Object(Google_HttpRequest)) #1 
/home2/gdawoud/public_html/test/google-api-php-client/src/service/Google_Service
Resource.php(186): Google_REST::execute(Object(Google_HttpRequest)) #2 
/home2/gdawoud/public_html/test/google-api-php-client/src/contrib/Google_Directo
ryService.php(347): Google_ServiceResource->__call('insert', Array) #3 
/home2/gdawoud/public_html/test/google.php(26): 
Google_MembersServiceResource->insert('admins@stgeorge...', 
Object(Google_Member)) #4 {main} thrown in 
/home2/gdawoud/public_html/test/google-api-php-client/src/io/Google_REST.php on 
line 66

----------------------

I have only the Admin SDK enabled in the Developer Console. 
I am using Service Account for the Client Info. 

What Am i missing... i read info about the email address needs to be an admin 
which is the account I'm logged into the Developers Console but I'm not sure 
what is going on... 

Thanks in advance
George

Original issue reported on code.google.com by geo...@stgeorgeseattle.org on 3 Jan 2014 at 8:14

GoogleCodeExporter commented 9 years ago
This issue tracker is now closing. Development on the Google PHP client library 
moved to GitHub with the release of the 1.0.0-alpha, and now the 1.0 branch has 
reached beta status there will be no further releases of the 0.6 branch of the 
library. 

Please take a look at the latest version on 
https://github.com/google/google-api-php-client

For information on migrating, please take a look at this guide: 
https://developers.google.com/api-client-library/php/guide/migration

For general library support please ask a question on StackOverflow: 
http://stackoverflow.com/questions/tagged/google-api-php-client

If you are looking for support with a specific API, please contact the team 
working with that API via StackOverflow or their preferred support mechanism. 

If your issue still exists with the new version of the library, please raise a 
bug in the GitHub issue tracker with a minimal code sample. 

Thanks!

Original comment by ianbar...@google.com on 22 Jan 2014 at 4:56

GoogleCodeExporter commented 9 years ago

Original comment by ianbar...@google.com on 22 Jan 2014 at 4:56