authentication - Google Directory API(PHP-client) - error 401 Login Required -
I am trying to add a user by using a service account and am continually getting the '401 login required' error . I have already added p12-key to the server and allowed to add service account / area permissions in the Admin console. I have implemented with general authentication but there is only one problem.
& lt ;? Php session_start (); Set_include_path ($ _ server ['DOCUMENT_ROOT'] '/ src / php /'.); Need_once ('Google / Client.php'); $ Scope = 'https://www.googleapis.com/auth/admin.directory.user'; $ Client_id = 'xxxxx.apps.googleusercontent.com'; // Client ID $ service_account_name = 'xxxxx@developer.gserviceaccount.com'; // email address $ key_file_location = 'key.p12'; //key.p12 $ client = New Google_Client (); $ Client- & gt; Set upspan ("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 ($ radius), $ key); $ Customer & gt; SetAssertionCredentials ($ cred); If ($ client- & gt; getAuth () - & gt; isxTokenAxide ()) {$ client-> GetAuth () - & gt; RefreshTokenWeight Assurance ($ cred); } $ _SESSION ['service_token'] = $ client- & gt; GetAccessToken (); If ($ client-> getAccessToken ()) {$ requestUrl = 'https://www.googleapis.com/admin/directory/v1/users'; $ RequestMethod = 'POST'; $ RequestHeader = array ('Content-Type' => 'application / json', 'Content-Length' = & gt; 'CONTENT_LENGTH'); "Postname": "newuser@testpurpose.esy.es", "name": { "givenName": "user_name", "familyName": "user_familyName"}, "suspended": false, "password": "Primary": true}]} "" "" "" "" "" "" "; $ request = new Google_Http_Request ($ requestUrl, $ requestMethod, $ requestHeader, $ postBody); $ result = $ client & execute gt; ($ Error in posting with the message 'Google_Service_Exception' message;); error
spam_r ($ result);}? & Gt; Https://www.googleapis.com/admin/directory/v1/users: (401) Entry Required '/home/u538421519/public_html/src/php/Google/Http/REST.php:79 In 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- & gt; Implementation (object (Google_Http_Request)) # 3 {main} / home / u538421519 / public_html / src / php / Google / thrown Http / on REST.php row 79 < P> The request is sent without the token: $ request = new Google_Http_Request ($ request URL, $ request Method, $ request header, $ postBody); $ Result = $ client- & gt; Execute ($ request); The request is sent with token:
$ request = new Google_Http_Request ($ requestUrl, $ requestMethod, $ requestHeader, $ postBody); $ Result = $ client- & gt; GetAuth () - & gt; Certified request ($ request);
Comments
Post a Comment