php - Sage One API - unsupported_grant_type

标签 php api oauth-2.0 guzzle sage-one

我正在尝试按照 docs 获取 Sage One API 的访问 token 使用 Guzzle (v6)/Laravel 5.2(Laravel 的参与与本题无关),卡在“请求访问 token ”阶段。

错误

Client error: `POST https://api.sageone.com/oauth2/token` resulted in a
 `400 Bad Request` response: {"error":"unsupported_grant_type"}

违规代码

    $client = new 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' => $request->code,
        'grant_type' => 'authorization_code',
        'redirect_uri'=>'https://myurl.com/sage/refresh'
    ];

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

文档声明 “grant_type - 代码相关的授权类型。authorization_code 或 refresh_token。”,我都试过了。其他 var 都很好,只是 grant_type 似乎失败了。

更新 1 调试 header 生成以下输出。

* Hostname in DNS cache was stale, zapped * Trying 52.49.116.133... 
* Connected to api.sageone.com (52.49.116.133) port 443 (#0) 
* CAfile: /etc/pki/tls/certs/ca-bundle.crt CApath: none 
* ALPN/NPN, server did not agree to a protocol 
* SSL connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 
* Server certificate: 
* subject: CN=api.sageone.com,O=The Sage Group PLC,L=Newcastle Upon Tyne,ST=Newcastle Upon Tyne,C=GB,serialNumber=02231246,businessCategory=Private Organization,incorporationCountry=GB 
* start date: May 12 00:00:00 2014 GMT 
* expire date: May 11 23:59:59 2016 GMT 
* common name: api.sageone.com 
* issuer: CN=GeoTrust Extended Validation SSL CA - G2,O=GeoTrust Inc.,C=US > POST /oauth2/token HTTP/1.1 Host: api.sageone.com content_type: application/x-www-form-urlencoded User-Agent: GuzzleHttp/6.1.1 curl/7.43.0 PHP/5.6.18 Content-Type: application/x-www-form-urlencoded Content-Length: 216 
* upload completely sent off: 216 out of 216 bytes < HTTP/1.1 400 Bad Request < Content-Type: application/json < Date: Tue, 08 Mar 2016 10:53:09 GMT < Server: openresty < Content-Length: 26 < Connection: keep-alive < 
* Connection #0 to host api.sageone.com left intact 

最佳答案

您将值作为 JSON 编码数据进行 POST,但您应该使用表单编码进行 POST。请参阅:http://docs.guzzlephp.org/en/latest/request-options.html#form-params , 所以

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

编辑:

您应该再次检查您的代码,因为我刚刚验证并仔细检查了此更改是否使其适用于 live sage 环境。我使用的完整代码:

<?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();

?>

关于php - Sage One API - unsupported_grant_type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35861583/

相关文章:

PHP 将 html 输出打包到一个变量中

oauth-2.0 - 如何在 Linked In Oauth2 中获取刷新 token ?

c# - 使用 Azure AD 客户端凭据授予流程的 Azure Devops Access

oauth-2.0 - IdentityServer 中的作用域是什么

javascript - 使用 PHP include 获取本地 js kosher?

php - 通知 : Uninitialized string offset in PHP

c# - Google Analytics 维度和指标验证

api - 是否有 API 将 long/lat 转换为 city,country 而不是 Google API

php - 如何从 PHP 中的 API 流式传输结果

javascript - 使用 PHP 和 AJAX 获取页面名称而不是页码