0
votes

I want to retrieve all the users from a given domain with Google Directory API (Admin SDK) with PHP.

I have read all the related documentation but I can't find any working solution.

The GET request I need to run is :

GET https://www.googleapis.com/admin/directory/v1/users?domain=example.com&maxResults=2

I try to run it with Curl :

$curl_handle=curl_init();
                                    $header = array();
                                    $header[] = 'Content-length: 0';
                                    $header[] = 'Content-type: application/json';
                                    $header[] = 'client_id=xxx';
                                    $header[] = 'client_secret=yyy&token=zzz'  ;
                                    curl_setopt($curl_handle, CURLOPT_HTTPHEADER,$header);
                                    curl_setopt($curl_handle, CURLOPT_URL,'https://www.googleapis.com/admin/directory/v1/users?domain=westwing.fr&maxResults=2');
                                    curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
                                    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
                                    curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Your application name');
                                    $query = curl_exec($curl_handle);
                                    curl_close($curl_handle);
                                    echo $query;

The result is the following:

{ "error": { "errors": [ { "domain": "global", "reason": "required", "message": "Login Required", "locationType": "header", "location": "Authorization" } ], "code": 401, "message": "Login Required" } }

I assume there is an authentification problem but I can't solve it. Note that I have using the google-api-php-client to get the token and the google sign-in.

Thanks.

######### EDIT 15:04 09/05/15

I modified the request :

$thetok=array();
                                 $thetok= json_decode($_SESSION["token"] ,true);
                                 $thetoken =  $thetok["access_token"] ;
                                 //print_r($thetok);



                                    $ch = curl_init();
                                    curl_setopt($ch, CURLOPT_URL, 'https://www.googleapis.com/admin/directory/v1/users?domain=example.com&maxResults=2&viewType=domain_public');
                                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                                    $headers[0] = 'Authorization: Bearer ' . $thetoken;
                                    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
                                    $response = curl_exec($ch);
                                    curl_close($ch);
                                    print_r($response);
                                    print_r($headers);

but the answer remains :

{ "error": { "errors": [ { "domain": "global", "reason": "authError", "message": "Invalid Credentials", "locationType": "header", "location": "Authorization" } ], "code": 401, "message": "Invalid Credentials" } }

######### EDIT 15:14 09/05/15

I modified it again and now I have a 403 error (insufficient permissions):

    $ch = curl_init();
                                curl_setopt($ch, CURLOPT_URL, 'https://www.googleapis.com/admin/directory/v1/users?domain=example.com&maxResults=2&viewType=domain_public&access_token='.$thetoken.'');
                                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

                                curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
                                $response = curl_exec($ch);
                                curl_close($ch);
                                print_r($response);
                                print_r($headers);
2

2 Answers

1
votes

Just pass the OAuth 2.0 access token in the Authorization header as in:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.googleapis.com/admin/directory/v1/users?domain=example.com&maxResults=2&viewType=domain_public');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers[0] = 'Authorization: Bearer ' . $access_token;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
print_r($response);

No need for the client_id and/or client_secret anymore; they were used for obtaining an access_token earlier.

0
votes

Ok, found the answer.

I didn't define the scope previously.. "https://www.googleapis.com/auth/admin.directory.user.readonly"