0

我想添加用户使用服务帐户,并不断得到'401登录需要'的错误。 我已经在服务器上放置了p12键,并在管理控制台中添加了权限服务帐号/作用域。 我用通常的身份验证完成了实现,但得到同样的问题。谷歌目录API(PHP客户端) - 错误401登录需要

<?php 
     session_start(); 
     set_include_path($_SERVER['DOCUMENT_ROOT'].'/src/php/'); 
     require_once ('Google/Client.php'); 

     $scope = 'https://www.googleapis.com/auth/admin.directory.user'; 
     $client_id = 'xxxxx.apps.googleusercontent.com'; //Client ID 
     $service_account_name = '[email protected]'; //Email Address 
     $key_file_location = 'key.p12'; //key.p12 

     $client = new Google_Client(); 
     $client->setApplicationName("test product"); 

     if (isset($_SESSION['service_token'])) 
     { 
      $client->setAccessToken($_SESSION['service_token']); 
     } 

     $key = file_get_contents($key_file_location); 
     $cred = new Google_Auth_AssertionCredentials 
     (
      $service_account_name, 
      array($scope), 
      $key 
     ); 
     $client->setAssertionCredentials($cred); 

     if($client->getAuth()->isAccessTokenExpired()) 
     { 
      $client->getAuth()->refreshTokenWithAssertion($cred); 
     } 
     $_SESSION['service_token'] = $client->getAccessToken(); 

     if($client->getAccessToken()) 
     { 
      $requestUrl = 'https://www.googleapis.com/admin/directory/v1/users'; 
      $requestMethod = 'POST'; 
      $requestHeader = array('Content-Type' => 'application/json', 'Content-Length' => 'CONTENT_LENGTH'); 
      $postBody ='{ 
          "primaryEmail": "[email protected]", 
          "name": { 
          "givenName": "user_name", 
          "familyName": "user_familyName" 
          }, 
          "suspended": false, 
          "password": "passpass", 
          "ims": [ 
          { 
          "type": "work", 
          "protocol": "gtalk", 
          "im": "[email protected]", 
          "primary": true 
          } 
          ] 
         }'; 

      $request = new Google_Http_Request($requestUrl , $requestMethod, $requestHeader, $postBody); 
      $result = $client->execute($request); 

      print_r($result);  
     } 
    ?> 

错误

Uncaught exception 'Google_Service_Exception' with message 'Error calling POST https://www.googleapis.com/admin/directory/v1/users: (401) Login Required' in /home/u538421519/public_html/src/php/Google/Http/REST.php:79 Stack trace: 
#0 /home/u538421519/public_html/src/php/Google/Http/REST.php(44): Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request)) 
#1 /home/u538421519/public_html/src/php/Google/Client.php(556): Google_Http_REST::execute(Object(Google_Client), Object(Google_Http_Request)) 
#2 /home/u538421519/public_html/index.php(58): Google_Client->execute(Object(Google_Http_Request)) 
#3 {main} thrown in /home/u538421519/public_html/src/php/Google/Http/REST.php on line 79 

回答

2

请求不被发送令牌:

$request = new Google_Http_Request($requestUrl , $requestMethod, $requestHeader, $postBody); 
$result = $client->getAuth()->authenticatedRequest($request); 

$request = new Google_Http_Request($requestUrl , $requestMethod, $requestHeader, $postBody); 
$result = $client->execute($request); 

请求与令牌发送