Sage One API - unsupported_grant_type

落花浮王杯 提交于 2019-12-04 12:37:35

You are POST-ing the values as JSON encoded data but you should POST with form-encoded. See: http://docs.guzzlephp.org/en/latest/request-options.html#form-params, so

$response = $client->request('POST','/oauth2/token',['form_params' => $data]);

EDIT:

You should check your code again since I've just verified and double-checked that this change makes it work on the live sage environment. The complete code that I used:

<?php
require 'vendor/autoload.php';

$client = new GuzzleHttp\Client([
        'base_uri'=>'https://api.sageone.com',
        'headers' => ['content_type' => 'application/x-www-form-urlencoded']
    ]);

$data = [
    'client_id' => getenv('SAGE_CLIENT'),
    'client_secret' => getenv('SAGE_SECRET'),
    'code' => getenv('SAGE_CODE'),
    'grant_type' => 'authorization_code',
    'redirect_uri'=>'https://myurl.com/sage/refresh'
];

$response = $client->request('POST','/oauth2/token',['form_params' => $data]);

echo $response->getBody();

?>

Using json for oAuth is indeed incorrect: http://tools.ietf.org/html/rfc6749#section-4.1.3 I think, this is incorrect:

$response = $client->request('POST','/oauth2/token',['json' => json_encode($data)]);

Instead, I would try something like this:

$response = $client->post('/oauth2/token', [], $data);

OR this

$response = $client->post('/oauth2/token', ['body' => $data]);

Another question, what about the first call https://www.sageone.com/oauth2/auth - did it go through?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!