PHP检索所有用户Google Directory Admin SDK

时间:2015-05-09 11:30:39

标签: php curl google-api google-api-php-client

我想通过PHP使用Google Directory API(Admin SDK)检索给定域中的所有用户。

我已阅读所有相关文档,但找不到任何可行的解决方案。

我需要运行的GET请求是:

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

我尝试用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;

结果如下:

  

{“error”:{“errors”:[{“domain”:“global”,“reason”:“required”,   “message”:“需要登录”,“locationType”:“标题”,“位置”:   “授权”},“代码”:401,“消息”:“需要登录”}}

我认为存在身份验证问题,但我无法解决。请注意,我使用google-api-php-client获取令牌和Google登录。

感谢。

#########编辑15:04 09/05/15

我修改了请求:

$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);

但答案仍然是:

  

{“error”:{“errors”:[{“domain”:“global”,“reason”:“authError”,   “message”:“Invalid Credentials”,“locationType”:“header”,   “location”:“授权”}],“代码”:401,“消息”:“无效   证书“}}

#########编辑15:14 09/05/15

我再次修改它,现在我有403错误(权限不足):

    $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 个答案:

答案 0 :(得分:1)

只需在Authorization标题中传递OAuth 2.0访问令牌,如下所示:

$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);

不再需要client_id和/或client_secret;它们用于早先获得access_token

答案 1 :(得分:0)

好的,找到了答案。

我之前没有定义范围。 " https://www.googleapis.com/auth/admin.directory.user.readonly"