I want to get data from External API. but, I must login to get session before Getting data and I must Logout after get data
this is my code
use GuzzleHttp\Client;
$client = new Client();
$res = $client->request('POST', 'Login_URL', [
'json' => [
"JSON param"
]
]);
$res = $client->request('GET', 'Get_URL');
$res = $client->request('POST', 'Logout_URL');
but I can only the first step (Login). and I getting error message in the second step to get data
Client error: `GET "Get_URL" ` resulted in a `401 Unauthorized` response:{"message":"You are not logged in."}
how I can run all this code with login session on first step ?
this code for you to do login and logout session
$cookie_jar = tempnam("tmp", "cookie");
//login
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => '_URL',
CURLOPT_RETURNTRANSFER=> true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION=> true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_COOKIEJAR => $cookie_jar,
CURLOPT_POSTFIELDS =>'{
"name" : "****",
"password" : "****",
"company" : "****"
}',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'),
)
);
$response = curl_exec($curl);
//get data
curl_setopt_array($curl, array(
CURLOPT_URL => '_URL',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_COOKIEFILE => $cookie_jar
)
);
$datapo = json_decode(curl_exec($curl));
//logout
curl_setopt_array($curl, array(
CURLOPT_URL => '_URL',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => '{
"name" : "****",
"password" : "****",
"company" : "****"
}',
CURLOPT_COOKIEFILE => $cookie_jar
)
);
$response = curl_exec($curl);
I am attempting to recreate a working CURL command with LWP in Perl and I'm getting a 401 unauthorized error from LWP. The command posts JSON to a specific URL as seen in the code below. The server FQDN, IP, port and path are correct and identical between the curl and Perl as are the credentials and realm. Any help would be appreciated - thank you!
Below is the working syntax in cURL and debug output:
#curl -v -k -u "USERNAME:PASSWORD" -X POST <SERVER_URL> -d '<JSON CONTENT>';
* About to connect() to <SERVER_URL> port 443 (#0)
* Trying <SERVER_IP>... connected
* Connected to <SERVER_URL> (<SERVER_IP>) port 443 (#0)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
* warning: ignoring value of ssl.verifyhost
* skipping SSL peer certificate verification
* SSL connection using TLS_DHE_RSA_WITH_AES_256_CBC_SHA
* Server certificate:
* subject: [REDACTED]
* start date: Apr 21 00:00:00 2016 GMT
* expire date: Apr 21 23:59:59 2019 GMT
* common name: <SERVER_URL>
* issuer: [REDACTED]
* Server auth using Basic with user '<USERNAME>'
> POST <SERVER_PATH> HTTP/1.1
> Authorization: Basic <BASE64-ENCODED USERNAME:PASSWORD>
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.19.1 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: <SERVER_URL>
> Accept: */*
> Content-Length: 144
> Content-Type: application/x-www-form-urlencoded
>
< HTTP/1.1 200 OK
< Date: Fri, 13 May 2016 13:48:42 GMT
< Server: Apache
< Content-Type: application/json
< Content-Length: 256
<
* Connection #0 to host <SERVER_URL> left intact
* Closing connection #0
Updated Perl code and output per Steffen's suggestion. I corrected an initial quoting error and also added the Accept header to the LWP post command:
use strict;
use warnings;
use LWP::UserAgent;
use Data::Dumper;
my $server_root_with_port = "<FQDN>:443";
my $url = "<SERVER_URL>";
my $realm = "<SERVER_REALM>";
my $json = "<JSON CONTENT>";
my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 1 });
$ua->credentials($server_root_with_port,$realm,$username=>$password);
$response = $ua->post($url, 'Content-Type' => 'application/x-www-form-urlencoded', 'Accept' => '*/*', 'Content' => $json);
print Dumper $response;
exit;
$VAR1 = bless( {
'_protocol' => 'HTTP/1.1',
'_content' => '',
'_rc' => '400',
'_headers' => bless( {
'connection' => 'close',
'client-response-num' => 1,
'date' => 'Mon, 16 May 2016 14:18:59 GMT',
'client-ssl-cert-issuer' => '[REDACTED]',
'client-ssl-cipher' => 'AES128-SHA256',
'client-peer' => '<SERVER_IP>:443',
'content-length' => '0',
'::std_case' => {
'client-date' => 'Client-Date',
'client-response-num' => 'Client-Response-Num',
'client-ssl-cert-subject' => 'Client-SSL-Cert-Subject',
'client-ssl-cert-issuer' => 'Client-SSL-Cert-Issuer',
'client-ssl-cipher' => 'Client-SSL-Cipher',
'client-peer' => 'Client-Peer',
'client-ssl-socket-class' => 'Client-SSL-Socket-Class'
},
'client-date' => 'Mon, 16 May 2016 14:18:59 GMT',
'client-ssl-cert-subject' => '[REDACTED]',
'server' => 'Apache',
'client-ssl-socket-class' => 'IO::Socket::SSL'
}, 'HTTP::Headers' ),
'_previous' => bless( {
'_protocol' => 'HTTP/1.1',
'_content' => '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>401 Unauthorized</title>
</head><body>
<h1>Unauthorized</h1>
<p>This server could not verify that you
are authorized to access the document
requested. Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn\'t understand how to supply
the credentials required.</p>
</body></html>
',
'_rc' => '401',
'_headers' => bless( {
'connection' => 'close',
'client-response-num' => 1,
'date' => 'Mon, 16 May 2016 14:18:59 GMT',
'client-ssl-cert-issuer' => '[REDACTED]',
'client-ssl-cipher' => 'AES128-SHA256',
'client-peer' => '<SERVER_IP>:443',
'content-length' => '381',
'::std_case' => {
'client-date' => 'Client-Date',
'client-response-num' => 'Client-Response-Num',
'client-ssl-cert-subject' => 'Client-SSL-Cert-Subject',
'title' => 'Title',
'client-ssl-cert-issuer' => 'Client-SSL-Cert-Issuer',
'client-ssl-cipher' => 'Client-SSL-Cipher',
'client-peer' => 'Client-Peer',
'client-ssl-socket-class' => 'Client-SSL-Socket-Class'
},
'client-date' => 'Mon, 16 May 2016 14:18:59 GMT',
'content-type' => 'text/html; charset=iso-8859-1',
'client-ssl-cert-subject' => '[REDACTED]',
'www-authenticate' => 'Basic realm="<SERVER_REALM>"',
'title' => '401 Unauthorized',
'server' => 'Apache',
'client-ssl-socket-class' => 'IO::Socket::SSL'
}, 'HTTP::Headers' ),
'_msg' => 'Unauthorized',
'_request' => bless( {
'_content' => '<JSON_CONTENT>',
'_uri' => bless( do{\(my $o = '<SERVER_URL>')}, 'URI::https' ),
'_headers' => bless( {
'user-agent' => 'libwww-perl/6.15',
'content-type' => 'application/x-www-form-urlencoded',
'accept' => '*/*',
'content-length' => 144,
'::std_case' => {
'if-ssl-cert-subject' => 'If-SSL-Cert-Subject'
}
}, 'HTTP::Headers' ),
'_method' => 'POST',
'_uri_canonical' => $VAR1->{'_previous'}{'_request'}{'_uri'}
}, 'HTTP::Request' )
}, 'HTTP::Response' ),
'_msg' => 'Bad Request',
'_request' => bless( {
'_protocol' => undef,
'_content' => '<JSON_CONTENT>',
'_uri' => bless( do{\(my $o = '<SERVER_URL>')}, 'URI::https' ),
'_headers' => bless( {
'user-agent' => 'libwww-perl/6.15',
'content-type' => 'application/x-www-form-urlencoded',
'accept' => '*/*',
'content-length' => 144,
'authorization' => '<BASE64-ENCODED USERNAME:PASSWORD>',
'::std_case' => {
'if-ssl-cert-subject' => 'If-SSL-Cert-Subject'
}
}, 'HTTP::Headers' ),
'_method' => 'POST',
'_uri_canonical' => $VAR1->{'_request'}{'_uri'}
}, 'HTTP::Request' )
}, 'HTTP::Response' );
Perl Revision #1:
use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Request::Common;
use Data::Dumper;
my $fqdn_port = "<FQDN>:443";
my $url = "<URL>";
my $realm = "<REALM>";
my $username = "<USERNAME>";
my $password = "<PASSWORD>";
my $json = "<JSON_CONTENT>";
my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 1 });
#$ua->credentials($fqdn_port,$realm,$username=>$password);
#my $response = $ua->post($url, 'Content-Type' => 'application/x-www-form-urlencoded', 'Accept' => '*/*', Content => $json);
my $request = HTTP::Request->new('POST',$url);
$request->header('Content-Type' => 'application/x-www-form-urlencoded', 'Accept' => '*/*');
$request->authorization_basic($username,$password);
$request->content($json);
my $response = $ua->request($request);
print Dumper $response;
exit;
$VAR1 = bless( {
'_protocol' => 'HTTP/1.1',
'_content' => '',
'_rc' => '400',
'_headers' => bless( {
'connection' => 'close',
'client-response-num' => 1,
'date' => 'Mon, 16 May 2016 15:41:10 GMT',
'client-ssl-cert-issuer' => '[REDACTED]',
'client-ssl-cipher' => 'AES128-SHA256',
'client-peer' => '<SERVER_IP>:443',
'content-length' => '0',
'::std_case' => {
'client-date' => 'Client-Date',
'client-response-num' => 'Client-Response-Num',
'client-ssl-cert-subject' => 'Client-SSL-Cert-Subject',
'client-ssl-cert-issuer' => 'Client-SSL-Cert-Issuer',
'client-ssl-cipher' => 'Client-SSL-Cipher',
'client-peer' => 'Client-Peer',
'client-ssl-socket-class' => 'Client-SSL-Socket-Class'
},
'client-date' => 'Mon, 16 May 2016 15:41:10 GMT',
'client-ssl-cert-subject' => '[REDACTED]',
'server' => 'Apache',
'client-ssl-socket-class' => 'IO::Socket::SSL'
}, 'HTTP::Headers' ),
'_msg' => 'Bad Request',
'_request' => bless( {
'_content' => '<JSON_CONTENT>',
'_uri' => bless( do{\(my $o = '<URL>')}, 'URI::https' ),
'_headers' => bless( {
'user-agent' => 'libwww-perl/6.15',
'content-type' => 'application/x-www-form-urlencoded',
'accept' => '*/*',
'::std_case' => {
'if-ssl-cert-subject' => 'If-SSL-Cert-Subject'
},
'authorization' => 'Basic <BASE64-ENCODED USERNAME:PASSWORD>'
}, 'HTTP::Headers' ),
'_method' => 'POST',
'_uri_canonical' => $VAR1->{'_request'}{'_uri'}
}, 'HTTP::Request' )
}, 'HTTP::Response' );
TL;TR: always use strict !!
$response = $ua->post($url, Content-Type => 'application/json', Content => $json);
You've missed to quote around Content-Type which would have been detected by use strict. This result of this is a strange header 0 you see in the debug output:
'content-type' => 'application/x-www-form-urlencoded',
'0' => 'application/json',
'content-length' => 144,
And this also means that the setting of the content-type is wrong. This together results in the server not accepting your request:
'_rc' => '400',
...
'_msg' => 'Bad Request',
To understand what happens here look at what Perl actually sees in such code:
$ perl -MO=Deparse -e 'my %x = (Content-Type => 1, Foo => 2 )'
my(%x) = ('Content' - 'Type', 1, 'Foo', 2);
This shows that it will interpret the unquoted Content-Type as 'Content' - 'Type'. And since subtraction is not defined for strings they will be cast to an integer, i.e. 0. Which means the result is 0 (0-0).
When using strict you get instead:
perl -Mstrict -e 'my %x = (Content-Type => 1, Foo => 2 )'
Bareword "Content" not allowed while "strict subs" in use at -e line 1.
Execution of -e aborted due to compilation errors.
Found a solution - perhaps not the most elegant, but it does the trick. Ended up using HTTP::Request::Common to get around the authorization issue and reversed the quoting on the JSON variable to mitigate the 400 Bad Request and success - getting a correct return from the server! Thanks for the help #steffen_ullrich.
use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Request::Common;
use Data::Dumper;
my $fqdn_port = "<FQDN>:443";
my $url = "<URL>";
my $realm = "<REALM>";
my $username = "<USERNAME>";
my $password = "<PASSWORD>";
my $json = '<JSON_CONTENT>';
my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 1 });
my $request = HTTP::Request->new('POST',$url);
$request->header('Content-Type' => 'application/x-www-form-urlencoded', 'Accept' => '*/*');
$request->authorization_basic($username,$password);
$request->content($json);
my $response = $ua->request($request);
print Dumper $response;
exit;
$VAR1 = bless( {
'_protocol' => 'HTTP/1.1',
'_content' => '<RETURN JSON FROM SERVER>',
'_rc' => '200',
'_headers' => bless( {
'connection' => 'close',
'client-response-num' => 1,
'date' => 'Mon, 16 May 2016 16:07:07 GMT',
'client-ssl-cert-issuer' => '[REDACTED]',
'client-ssl-cipher' => 'AES128-SHA256',
'client-peer' => '<SERVER_IP>:443',
'content-length' => '233',
'::std_case' => {
'client-date' => 'Client-Date',
'client-response-num' => 'Client-Response-Num',
'client-ssl-cert-subject' => 'Client-SSL-Cert-Subject',
'client-ssl-cert-issuer' => 'Client-SSL-Cert-Issuer',
'client-ssl-cipher' => 'Client-SSL-Cipher',
'client-peer' => 'Client-Peer',
'client-ssl-socket-class' => 'Client-SSL-Socket-Class'
},
'client-date' => 'Mon, 16 May 2016 16:07:07 GMT',
'content-type' => 'application/json',
'client-ssl-cert-subject' => '[REDACTED]',
'server' => 'Apache',
'client-ssl-socket-class' => 'IO::Socket::SSL'
}, 'HTTP::Headers' ),
'_msg' => 'OK',
'_request' => bless( {
'_content' => '<JSON_CONTENT>',
'_uri' => bless( do{\(my $o = '<URL>')}, 'URI::https' ),
'_headers' => bless( {
'user-agent' => 'libwww-perl/6.15',
'content-type' => 'application/x-www-form-urlencoded',
'accept' => '*/*',
'::std_case' => {
'if-ssl-cert-subject' => 'If-SSL-Cert-Subject'
},
'authorization' => 'Basic <BASE64-ENCODED USERNAME:PASSWORD>'
}, 'HTTP::Headers' ),
'_method' => 'POST',
'_uri_canonical' => $VAR1->{'_request'}{'_uri'}
}, 'HTTP::Request' )
}, 'HTTP::Response' );
I am using ADmad/cakephp-jwt-auth plugin for Json web token authentication for rest api.
For the token generation we i am sending email and password to login via form . but it fails i don't what is issue.
Here is the my settings. in AppController.php
$this->loadComponent('Auth', [
'Form' => [
'fields' => [
'username' => 'email',
'password' => 'password'
]
],
'authenticate' => [
'ADmad/JwtAuth.Jwt' => [
'parameter' => '_token',
'userModel' => 'Users',
'scope' => ['Users.active' => 1],
'fields' => [
'id' => 'id'
]
]
],
'unauthorizedRedirect' => false,
// Config below is available since CakePHP 3.1.
// It makes user info available in controller's beforeFilter() which is not possible in CakePHP 3.0.
'checkAuthIn' => 'Controller.initialize',
]);
UsersController Token generation method script;
public function token(){
$user = $this->Auth->identify();
if (!$user) {
throw new UnauthorizedException('Invalid email or password');
}
$this->set([
'success' => true,
'data' => [
'token' => $token = \JWT::encode([
'id' => $user['id'],
'exp' => time() + 604800
],
Security::salt())
],
'_serialize' => ['success', 'data']
]);
}
The json data i am posting is
{
'email' : 'muni#smart.com',
'password': '123'
}
Please say me what is mistake?
Form must be in the authenticate
$this->loadComponent('Auth', [
'authenticate' => [
'Form' => [
'fields' => [
'username' => 'email',
'password' => 'password'
]
],
'ADmad/JwtAuth.Jwt' => [
'parameter' => '_token',
'userModel' => 'Users',
'fields' => [
'id' => 'id'
]
]
]
]);
When i want to access to backend yii2 always redirect me to frontend.
Example:
I have installed the yii2-user (dektrium) module in frontend and backend and yii2-admin (mdm) module only in backend . And when i want to acess to
http://localhost/american_eshop/yii-application/frontend/web/**user/admin/index**"
yii2 in the first place accessing to this route and after redirect me to "http://localhost/american_eshop/yii-application/frontend/web/".
I.E. i can get access to resourse but it always redirect me and i cannot undestand where i am in my site.
Sorry for my poor english...
backend cofigs:
main.php
<?php
$params = array_merge(
require(__DIR__ . '/../../common/config/params.php'),
require(__DIR__ . '/../../common/config/params-local.php'),
require(__DIR__ . '/params.php'),
require(__DIR__ . '/params-local.php')
);
return [
'id' => 'app-backend',
'basePath' => dirname(__DIR__),
'controllerNamespace' => 'backend\controllers',
'bootstrap' => ['log'],
'modules' => [],
'components' => [
'user' => [
'identityClass' => 'dektrium\user\models\User',
'enableAutoLogin' => true,
],
'authManager' => [
'class' => 'yii\rbac\DbManager'
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
],
'errorHandler' => [
'errorAction' => 'site/error',
],
],
'params' => $params,
];
main-local:
<?php
$config = [
'components' => [
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'QuXcvHHNSiT3mEGgBln9c85IYF7uVkoU',
],
],
'modules' => [
'user' => [
'class' => 'backend\modules\user\Module',
'viewPath' => '#dektrium/user/views',
'enableUnconfirmedLogin' => true,
'confirmWithin' => 21600,
'cost' => 14,
'admins' => ['Admin']
],
]
];
if (!YII_ENV_TEST) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = 'yii\debug\Module';
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = 'yii\gii\Module';
}
return $config;
frontend:
main:
<?php
$params = array_merge(
require(__DIR__ . '/../../common/config/params.php'),
require(__DIR__ . '/../../common/config/params-local.php'),
require(__DIR__ . '/params.php'),
require(__DIR__ . '/params-local.php')
);
return [
'id' => 'app-frontend',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'controllerNamespace' => 'frontend\controllers',
'components' => [
'user' => [
'identityClass' => 'dektrium\user\models\User',
'enableAutoLogin' => true,
],
'authManager' => [
'class' => 'yii\rbac\DbManager'
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
],
],
'params' => $params,
];
main-local:
<?php
$config = [
'components' => [
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'xlEFSbBB0pjvAJHtEOfY6r5BhDOTIAtB',
],
],
'modules' => [
'user' => [
'class' => 'frontend\modules\user\Module',
'viewPath' => '#dektrium/user/views',
'enableUnconfirmedLogin' => true,
'confirmWithin' => 21600,
'cost' => 14,
'admins' => ['Admin']
],
],
];
if (!YII_ENV_TEST) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = 'yii\debug\Module';
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = 'yii\gii\Module';
}
return $config;
Back story:
Migrating from the Provisioning API to the Admin SDK Directory API. Using Perl.
I can successfully obtain a Bearer token, and I can use the token to obtain an individual User Resource and User Resource listing of the entire domain. This all works fine. I have ensured that I'm using the proper scope in the token request (https://www.googleapis.com/auth/admin.directory.user).
Problem:
Calls to update a user are returning 200 OK (expected) but the changes are not being picked up.
Using LWP to PUT the update request. Here's a dump of the LWP object after the request comes back. You can see that I'm getting a 200 OK response and a User Resource object as part of the response. You can also see that the returned User Resource does not reflect the changes I've sent in the request. I have confirmed in the admin console for the domain that the changes were not picked up.
Any help would be appreciated.
'_content' => '{
"kind": "admin#directory#user",
"id": "somenumber",
"etag": "\\"etag\\"",
"primaryEmail": "user#googletestdomain",
"name": {
"givenName": "user",
"familyName": "name",
"fullName": "user name"
},
"isAdmin": false,
"isDelegatedAdmin": false,
"lastLoginTime": "2014-10-02T17:20:02.000Z",
"creationTime": "2010-01-04T22:27:44.000Z",
"agreedToTerms": true,
"suspended": false,
"changePasswordAtNextLogin": false,
"ipWhitelisted": false,
"emails": [
{
"address": "user#googletestdomain",
"primary": true
},
],
"customerId": "C01id",
"orgUnitPath": "/",
"isMailboxSetup": true,
"includeInGlobalAddressList": true
}
'
'_headers' => HTTP::Headers=HASH(0x2031048)
'::std_case' => HASH(0x2031240)
'alternate-protocol' => 'Alternate-Protocol'
'client-date' => 'Client-Date'
'client-peer' => 'Client-Peer'
'client-response-num' => 'Client-Response-Num'
'client-ssl-cert-issuer' => 'Client-SSL-Cert-Issuer'
'client-ssl-cert-subject' => 'Client-SSL-Cert-Subject'
'client-ssl-cipher' => 'Client-SSL-Cipher'
'client-ssl-socket-class' => 'Client-SSL-Socket-Class'
'x-content-type-options' => 'X-Content-Type-Options'
'x-frame-options' => 'X-Frame-Options'
'x-xss-protection' => 'X-XSS-Protection'
'alternate-protocol' => '443:quic,p=0.01'
'cache-control' => 'no-cache, no-store, max-age=0, must-revalidate'
'client-date' => 'Mon, 27 Oct 2014 17:48:14 GMT'
'client-peer' => '173.194.79.95:443'
'client-response-num' => 1
'client-ssl-cert-issuer' => '/C=US/O=Google Inc/CN=Google Internet Authority G2'
'client-ssl-cert-subject' => '/C=US/ST=California/L=Mountain View/O=Google Inc/CN=*.googleapis.com'
'client-ssl-cipher' => 'ECDHE-RSA-AES128-GCM-SHA256'
'client-ssl-socket-class' => 'IO::Socket::SSL'
'connection' => 'close'
'content-type' => 'application/json; charset=UTF-8'
'date' => 'Mon, 27 Oct 2014 17:48:14 GMT'
'etag' => '"etag"'
'expires' => 'Fri, 01 Jan 1990 00:00:00 GMT'
'pragma' => 'no-cache'
'server' => 'GSE'
'vary' => ARRAY(0x20311b0)
0 'Origin'
1 'Referer'
2 'X-Origin'
'x-content-type-options' => 'nosniff'
'x-frame-options' => 'SAMEORIGIN'
'x-xss-protection' => '1; mode=block'
'_msg' => 'OK'
'_protocol' => 'HTTP/1.1'
'_rc' => 200
'_request' => HTTP::Request=HASH(0x1f5dc90)
'_content' => '{"name":{"givenName":"BBB","familyName":"BBB"}}'
'_headers' => HTTP::Headers=HASH(0x224fa08)
'::std_case' => HASH(0x1f28c90)
'if-ssl-cert-subject' => 'If-SSL-Cert-Subject'
'authorization' => 'Bearer mytokenhere'
'content-length' => 47
'user-agent' => 'libwww-perl/6.05'
'_method' => 'PUT'
'_uri' => URI::https=SCALAR(0x1cbc8b8)
-> 'https://www.googleapis.com/admin/directory/v1/users/user#googletestdomain'
'_uri_canonical' => URI::https=SCALAR(0x1cbc8b8)
-> REUSED_ADDRESS
Here's a sample of the code used:
#!/usr/bin/perl -w
use JSON;
use LWP::UserAgent;
my $auth_token = 'myauthtoken';
my $changes = {
'name' => {
'givenName' => 'BBB',
},
};
my $json = new JSON;
my $ur = $json->encode($changes,{utf8 => 1});
my $url = 'https://www.googleapis.com/admin/directory/v1/users/user#googletestdomain';
my $ua = LWP::UserAgent->new(timeout => 30);
my $res = $ua->put($url,
'Authorization' => 'Bearer '.$auth_token,
'Content' => $ur,
);
Setting the Content-Type header in the PUT request to application/json fixed the problem.