2015-05-09 26 views
0

我想用PHP通過Google Directory API(Admin SDK)檢索給定域中的所有用戶。PHP檢索所有用戶Google Directory Admin SDK

我已閱讀所有相關文檔,但找不到任何工作解決方案。

的GET請求,我需要運行是:

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

我試着用捲曲運行:

$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」:「Login R equired」, 「的locationType」: 「首部」, 「位置」: 「授權」}], 「代碼」:401, 「消息」: 「

我假定有需要登錄」}}是一個鑑別問題,但我無法解決它。請注意,我使用google-api-php-client獲取令牌和谷歌登錄。

謝謝。

#########編輯15:04 15年9月5日

我修改請求:

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

但得到的答覆仍然是:

{「錯誤「:{」errors「:[{」domain「:」global「,」reason「:」authError「, 」message「:」Invalid Credentials「,」locationType「:」header「, 」location「 「}],」code「:401,」message「:」無效 憑證「}}

#########編輯15:14 15年9月5日

我再次修改它,現在我有一個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); 

回答

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

+0

我只是去嘗試,它給我下面的錯誤:{「錯誤」:{「錯誤」:[{「域」:「全局「,」原因「:」authError「,」消息「:」無效憑證「,」locationType「:」標題「,」位置「:」授權「}],」代碼「:401,」消息「:」無效憑據「}} – justberare

+0

可能您的訪問令牌已過期;用curl檢查一下https://www.googleapis.com/oauth2/v1/tokeninfo?access_token= ' –

相關問題