【问题标题】:Sage One API - unsupported_grant_typeSage One API - unsupported_grant_type
【发布时间】:2016-06-22 01:21:21
【问题描述】:

我正在尝试使用Guzzle(v6) / Laravel 5.2(Laravel 的参与与此问题无关)按照docs 获取 Sage One API 的访问令牌,它卡在“请求访问令牌” "舞台。

错误

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。”,我都试过了。其他变量都很好而且花花公子,只是 grant_type 似乎失败了。

更新 1 调试头生成下面的输出。

* 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 

【问题讨论】:

    标签: php api oauth-2.0 guzzle sage-one


    【解决方案1】:

    您将值作为 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();
    
    ?>
    

    【讨论】:

    • 感谢@HansZ,但这根本没有做任何事情。仍然是“unsupported_grant_type”
    • 你可以添加'debug' =&gt; true 并查看通过网络发送的内容吗,请参阅docs.guzzlephp.org/en/latest/request-options.html#debug
    • 从技术上讲,调试请求选项应该为您提供来自服务器的完整请求和响应。假设您的代码 sn-p 是您实际代码的简化是否安全?如果是这样,那么正在发送的请求(如图所示)应该在您的授权请求之后发生。当您查看您的请求时,访问令牌请求中发送的“代码”是否为预期值?
    • @ShaunBramley - 不,这就是所有相关代码。除了 Guzzle Client 依赖项之外,没有其他内容了。你想看看整个控制器类吗?
    • @DanWhite - 整个控制器;越多越好。包含通过网络发送的请求/响应的整个调试输出也很有用。
    【解决方案2】:

    使用 json 进行 oAuth 确实不正确:https://www.rfc-editor.org/rfc/rfc6749#section-4.1.3 我认为,这是不正确的:

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

    相反,我会尝试这样的事情:

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

    或者这个

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

    另一个问题,第一个电话https://www.sageone.com/oauth2/auth 怎么样?打通了吗?

    【讨论】:

    • 身体工作,感谢@Axalix。很想知道为什么会这样做而不是 form_params。
    • body 在 Guzzle 的最新版本中不起作用('InvalidArgumentException')并且已被弃用;使用form_params
    猜你喜欢
    • 2015-10-08
    • 2015-09-19
    • 2019-11-12
    • 2019-04-22
    • 2021-10-02
    • 1970-01-01
    • 2019-09-05
    • 2018-04-25
    • 1970-01-01
    相关资源
    最近更新 更多