【问题标题】:PHP post request and unsupported_grant_typePHP post 请求和 unsupported_grant_type
【发布时间】:2016-11-22 00:16:40
【问题描述】:

这是我的代码

$data = array(
        'grant_type'  => 'Password',
        'username'    => 'username',
        'password'    => 'pwd',
        'DeviceId'    => ''
    );


    $url = "http://test/sync/oauth/token";    

    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

    $response = curl_exec($curl);

    $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

    if ( $status != 201 ) {
        die("Error: call to URL $url failed with status $status, response $response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
    }


    curl_close($curl);

    print_r($response);`

我认为是正确的,但是服务器返回这个错误

错误:调用 URL http://test/sync/oauth/token 失败并显示状态 400, 响应 {"error":"unsupported_grant_type"}, curl_error , curl_errno 0

哪里出错了?

【问题讨论】:

  • cURL 工作正常 - 错误似乎是由 http://test/sync/oauth/token 生成的 JSON ...我将冒险猜测并说它期望 grant_typepassword(带有小写的“p”)但没有看到该脚本的来源,仅此而已,猜测。
  • 是的,您是对的,但错误仍然存​​在:错误:调用 URL test.audipsp.volkswagengroup.it/sync/oauth/token 失败,状态为 400,响应 HTTP/1.1 100 继续 是的,您是对的,但错误仍然存​​在:HTTP/1.1 400 Bad Request Cache-Control: no-cache Pragma: no-cache Content-Length: 34 Content-Type: application/json;charset=UTF-8 Expires: -1 Server: Microsoft-IIS/8.5 X-Powered-By: ASP.NET 日期:2016 年 7 月 19 日星期二 10:29:27 GMT {"error":"unsupported_grant_type"}, curl_error , curl_errno 0

标签: php post curl


【解决方案1】:

OAuthAuthorizationServerHandler的默认实现只接受表单编码(即application/x-www-form-urlencoded),不接受JSON编码(application/JSON)

您的请求的 ContentType 应该是 application/x-www-form-urlencoded 并将正文中的数据传递为: grant_type=密码&用户名=用户名&密码=密码 即不是 JSON 格式。

$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

【讨论】:

  • 好吧,错误改变了一点,但 unsupported_grant_type 是一样的:'错误:调用 URL test/sync/oauth/token 失败,状态为 400,响应 HTTP/1.1 100 Continue HTTP/1.1 400 Bad Request Cache-Control : no-cache Pragma: no-cache Content-Length: 34 Content-Type: application/json;charset=UTF-8 Expires: -1 服务器: Microsoft-IIS/8.5 X-Powered-By: ASP.NET 日期: Tue , 2016 年 7 月 19 日 10:29:27 GMT {"error":"unsupported_grant_type"}, curl_error , curl_errno 0'
  • 你能告诉我请求标头吗?我需要查看请求标头中的内容类型。或者在打开开发工具后在 Chrome 中尝试test.audipsp.volkswagengroup.it/sync/oauth/…
  • 请求标头不需要参数,所以我不知道正确的内容类型
【解决方案2】:

对于那些需要它的人... 这是解决方案:

$data = array(
    'grant_type'  => 'password',
    'username'    => 'username',
    'password'    => 'password',
    'DeviceId'    => ''
);
$url_token = "http://test/sync/oauth/token";    
try {
    $oauth = new OAuth('username','password');
    $signature = $oauth->fetch ($url_token,$data,'POST');
    $response_info = $oauth->getLastResponseInfo();
    header("Content-Type: {$response_info["content_type"]}");
    $token = $oauth->getLastResponse();
    $token = json_decode($token, true);

    $authorization_token = $token['access_token'];
} catch(OAuthException $E) {
    echo "Response: ". $E->lastResponse . "\n";
}
    return $authorization_token;

【讨论】:

  • 没有关于新 OAuth 的类定义,请对您的回答进行更多描述
【解决方案3】:

在浪费了我几个小时并进行了大量测试之后 我想出了一个解决方案。

提示:不要将 Post 参数连接成字符串并将其添加到 Array 中,只需单独使用它,这样您甚至不需要添加 Custom_HEADERS => Content-Type: application/x-www-form-urlencoded

有人说您需要将application/x-www-form-urlencoded 添加为Content-Type 然后它会起作用,但我的解决方案非常有效 这是工作代码

$curl = curl_init();
curl_setopt($curl, CURLOPT_POST, 1);  
curl_setopt($curl, CURLOPT_URL, 'YOUR_TOKEN_URL_HERE');
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query(array(
 'Username' => 'YOUR_USERNAME',
 'Password' => 'YOUR_PASSWORD',
 'grant_type' => 'password'
)));
$result = curl_exec($curl);
if(!$result){die("Connection Failure");}
curl_close($curl);
echo $result;

此代码也适用于 Laravel Bearer Token 和其他代码

参考:CURL POST FORMAT

【讨论】:

    猜你喜欢
    • 2021-07-25
    • 1970-01-01
    • 2021-03-11
    • 1970-01-01
    • 2015-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多