authentication - Google Directory API(PHP-client) - error 401 Login Required -
i trying add together user using service business relationship , '401 login required' error. set p12-key on server , add together permission service account/scope in admin console. had done implementation usual authentication got same problem.
<?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 = 'xxxxx@developer.gserviceaccount.com'; //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": "newuser@testpurpose.esy.es", "name": { "givenname": "user_name", "familyname": "user_familyname" }, "suspended": false, "password": "passpass", "ims": [ { "type": "work", "protocol": "gtalk", "im": "user_im@talk.example.com", "primary": true } ] }'; $request = new google_http_request($requesturl , $requestmethod, $requestheader, $postbody); $result = $client->execute($request); print_r($result); } ?>
error
uncaught exception 'google_service_exception' 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
request sent without token:
$request = new google_http_request($requesturl , $requestmethod, $requestheader, $postbody); $result = $client->execute($request);
request sent token:
$request = new google_http_request($requesturl , $requestmethod, $requestheader, $postbody); $result = $client->getauth()->authenticatedrequest($request);
php authentication google-apps google-api-php-client google-directory-api
No comments:
Post a Comment