贤者一个API

编程入门 行业动态 更新时间:2024-10-24 14:19:41
本文介绍了贤者一个API-unsupported_grant_type的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试通过遵循获取Sage One API的访问令牌. 使用枪口(v6)/Laravel 5.2(Laravel的参与与该问题无关) ,它停留在请求访问令牌"阶段.

I am trying to obtain an access token for Sage One API by following the docs using Guzzle(v6) / Laravel 5.2 (Laravel's involvement is irrelevant for this question), it is stuck at the "request access token" stage.

错误

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

违规代码

$client = new Client([ 'base_uri'=>'api.sageone', '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'=>'myurl/sage/refresh' ]; $response = $client->request('POST','/oauth2/token',['json' => $data]);

文档状态为 "grant_type-代码相关的授予类型.authorization_code或refresh_token." ,我都尝试过.其他变种都很好并且很花哨,只是grant_type似乎失败了.

The docs state "grant_type - The type of grant the code relates to. Either authorization_code or refresh_token.", I have tried both. The other vars are all fine and dandy, just the grant_type appears to fail.

更新1 调试标头将在下面生成输出.

UPDATE 1 The debug header generates the output below.

* Hostname in DNS cache was stale, zapped * Trying 52.49.116.133... * Connected to api.sageone (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,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 * issuer: CN=GeoTrust Extended Validation SSL CA - G2,O=GeoTrust Inc.,C=US > POST /oauth2/token HTTP/1.1 Host: api.sageone 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 left intact

推荐答案

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

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

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

由于我刚刚验证并仔细检查了此更改使其可以在实时鼠尾草环境中工作,因此您应该再次检查代码.我使用的完整代码:

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'=>'api.sageone', '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'=>'myurl/sage/refresh' ]; $response = $client->request('POST','/oauth2/token',['form_params' => $data]); echo $response->getBody(); ?>

更多推荐

贤者一个API

本文发布于:2023-10-12 16:44:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1485294.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!