PHP oAuth POST requests -


having little trouble getting oauth post requests return workable response. thoughts appreciated.

$request = $provider->getauthenticatedrequest(     'post',     'https://graph.microsoft.com/v1.0/me/calendar/events',     $_session['access_token'],     ['body' =>         json_encode([             'id' => null,             'subject' => 'test 54575',             'start' => [                 'datetime' => '2016-11-17t02:00:00',                 'timezone' => 'w. europe standard time'             ],             'end' => [                 'datetime' => '2016-11-17t04:00:00',                 'timezone' => 'w. europe standard time'             ],             'body' => [                 'contenttype' => 'text',                 'content' => 'estruyf'             ],             'isreminderon' => false         ])     ] );  $response = $provider->getresponse($request); 

error:

fatal error: uncaught unexpectedvalueexception: failed parse json response: syntax error in c:\projects\agentprocal\vendor\league\oauth2-client\src\provider\abstractprovider.php:663 stack trace: #0 c:\projects\agentprocal\vendor\league\oauth2-client\src\provider\abstractprovider.php(704): league\oauth2\client\provider\abstractprovider->parsejson(null) #1 c:\projects\agentprocal\vendor\league\oauth2-client\src\provider\abstractprovider.php(643): league\oauth2\client\provider\abstractprovider->parseresponse(object(guzzlehttp\psr7\response)) #2 c:\projects\agentprocal\index.php(58): league\oauth2\client\provider\abstractprovider->getresponse(object(guzzlehttp\psr7\request)) #3 {main} thrown in c:\projects\agentprocal\vendor\league\oauth2-client\src\provider\abstractprovider.php on line 663 

i've had no issues creating tokens, or requesting data. if needs further information please don't hesitate ask. thanks!

(using "league/oauth2-client": "^1.4")

correct answer @ end

problem

i'm looking inside class abstractprovider , seems in vendor have:

protected function parsejson($content) {     $content = json_decode($content, true);     if (json_last_error() !== json_error_none) { // ! here problem occurs         throw new unexpectedvalueexception(sprintf(             "failed parse json response: %s",             json_last_error_msg()         ));     }     return $content; } 

which throws exception says there problem parsing json because in function have:

protected function parseresponse(responseinterface $response) {     $content = (string) $response->getbody();     $type = $this->getcontenttype($response);      if (strpos($type, 'urlencoded') !== false) { // ! here checks header         parse_str($content, $parsed);         return $parsed;     }      // attempt parse string json regardless of content type,     // since providers use non-standard content types. throw     // exception if json not parsed when expected to.      try {         return $this->parsejson($content);     } catch (unexpectedvalueexception $e) { // ! here catch         if (strpos($type, 'json') !== false) { // ! again checks header             throw $e; // ! , here throw         }         return $content;     } } 

solution

it looks dont set right headers.

so seems if add in request like:

$options['header']['content-type'] = 'application/x-www-form-urlencoded'; 

it should work, because return string, without trying json_decode() in protected function parsejson($content) method.

in code this:

$request = $provider->getauthenticatedrequest(     'post',     'https://graph.microsoft.com/v1.0/me/calendar/events',     $_session['access_token'],     ['body' =>         json_encode([             'id' => null,             'subject' => 'test 54575',             'start' => [                 'datetime' => '2016-11-17t02:00:00',                 'timezone' => 'w. europe standard time'             ],             'end' => [                 'datetime' => '2016-11-17t04:00:00',                 'timezone' => 'w. europe standard time'             ],             'body' => [                 'contenttype' => 'text',                 'content' => 'estruyf'             ],             'isreminderon' => false         ]),      'header' => [          'content-type' => 'application/x-www-form-urlencoded', // set header          ],     ], );  $response = $provider->getresponse($request); 

if want response in json should set headers like:

$options['header']['accept'] = `application/json`; $options['header']['content-type'] = `application/json`; 

and in code like:

$request = $provider->getauthenticatedrequest(     'post',     'https://graph.microsoft.com/v1.0/me/calendar/events',     $_session['access_token'],     ['body' =>         json_encode([             'id' => null,             'subject' => 'test 54575',             'start' => [                 'datetime' => '2016-11-17t02:00:00',                 'timezone' => 'w. europe standard time'             ],             'end' => [                 'datetime' => '2016-11-17t04:00:00',                 'timezone' => 'w. europe standard time'             ],             'body' => [                 'contenttype' => 'text',                 'content' => 'estruyf'             ],             'isreminderon' => false         ]),      'header' => [          'content-type' => 'application/json', // set content type json          'accept' => 'application/json', // set expect in answer          ],     ], );  $response = $provider->getresponse($request); 

update

after our chat conversation got solution. problem header , correct code is:

$body = [             'id' => null,             'subject' => 'test 54575',             'start' => [                 'datetime' => '2016-11-17t02:00:00',                 'timezone' => 'w. europe standard time'             ],             'end' => [                 'datetime' => '2016-11-17t04:00:00',                 'timezone' => 'w. europe standard time'             ],             'isreminderon' => false         ];  $options['body'] = json_encode($body); $options['headers']['content-type'] = 'application/json;charset=utf-8';  $request = $provider->getauthenticatedrequest(     'post',     'https://graph.microsoft.com/v1.0/me/calendar/events',     $_session['access_token'],     $options );  $response = $provider->getresponse($request); 

Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -