【问题标题】:Getting "The authorization grant type is not supported by the authorization server" from amazon从亚马逊获取“授权服务器不支持授权授予类型”
【发布时间】:2017-03-29 02:49:18
【问题描述】:

我正在尝试获取访问令牌,不,我收到此错误

{"error_description":"授权服务器不支持授权授权类型","error":"unsupported_grant_type"}

$code =  $_GET['code'];

$postfields = array(
    'grant_type'=>'authorization_code',
    'code'=>$code,
    'redirect_uri='=>'example/myTest.php',
    'client_id'=>'amzn1.application-oa2-client.xxxxxxxxxxx',
    'client_secret'=>'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    );
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.amazon.com/auth/o2/token');
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($httpRequest, CURLOPT_HEADER, 1);
// Edit: prior variable $postFields should be $postfields;
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // On dev server only!
$result = curl_exec($ch);

print_r($result);

【问题讨论】:

    标签: php amazon-web-services curl amazon


    【解决方案1】:

    更改 CURLOPT_HTTPHEADER

    Content-Type: application/x-www-form-urlencoded

    Content-Type: application/json

    【讨论】:

    • 感谢您的建议,它可能会提供一些有限的短期帮助。一个正确的解释would greatly improve 其长期价值,通过展示为什么这是解决问题的好方法,并将使其对未来有其他类似问题的读者更有用。请edit您的回答添加一些解释,包括您所做的假设。
    • 你也可以在发送之前尝试json_encode你的POST数组。
    【解决方案2】:

    尝试将'token_type' => 'bearer' 添加到您的$postfields 数据中。

    以下来自亚马逊开发者文档: “访问令牌请求...返回的令牌类型。应该是不记名的。”

    “访问令牌响应:... unsupported_grant_type 客户端指定了错误的令牌类型。”

    【讨论】:

    • 我认为该参数是指亚马逊响应。他们总是在响应中发送一个名为 token_type 的参数。您在请求中不需要它。
    【解决方案3】:

    这确实是个老问题,但在official documentation 中它说有效值为refresh_tokenclient_credentials

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-28
      • 2017-11-10
      • 2017-11-07
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 2015-03-22
      • 1970-01-01
      相关资源
      最近更新 更多