【问题标题】:Connecting to OpenId Using cURL Gives Error使用 cURL 连接到 OpenId 会出错
【发布时间】:2017-10-31 07:23:11
【问题描述】:

我正在使用以下代码访问 OpenId 系统并请求不记名令牌。

if (isset($_GET['code'])) {
    // try to get an access token
    $code = $_GET['code'];
    $url = 'https://3rdpartydomain.com/connect/token';
    $params = array(
        "code" => $code,
        "client_id" => CLIENTID,
        "client_secret" => CLIENTSECRET,
        "redirect_uri" => 'http://' . $_SERVER["HTTP_HOST"] . $_SERVER["PHP_SELF"],
        "grant_type" => "authorization_code"
    );

    $ch = curl_init();
    curl_setopt($ch, constant("CURLOPT_" . 'URL'), $url);
    curl_setopt($ch, constant("CURLOPT_" . 'POST'), true);
    curl_setopt($ch, constant("CURLOPT_" . 'POSTFIELDS'), $params);
    $output = curl_exec($ch);
    $info = curl_getinfo($ch);
    curl_close($ch);
    if ($info['http_code'] === 200) {
        header('Content-Type: ' . $info['content_type']);
        return $output;
    } else {
        return 'An error happened';
    }
} else {

    $url = "https://3rdpartydomain.com/connect/token";

    $params = array(
        "response_type" => "code",
        "client_id" => CLIENTID,
        "redirect_uri" => 'http://' . $_SERVER["HTTP_HOST"] . $_SERVER["PHP_SELF"],
        "scope" => "openid"
    );

    $request_to = $url . '?' . http_build_query($params);

    header("Location: " . $request_to);
}

但是,我收到以下消息:

{"Message":"The requested resource does not support http method 'GET'."}

有人可以帮忙确定我的代码的正确性吗?

谢谢!

【问题讨论】:

    标签: php api curl oauth openid


    【解决方案1】:

    添加这些:

    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);

    【讨论】:

    • 即使添加了这些,也会出现同样的错误信息。
    • 使用这些替换 curl_setopt($curl,CURLOPT_URL,$url); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
    【解决方案2】:

    您的 IdP 的重定向中请求的方法可能发生了变化。我在使用curl -XPOST 测试我的OIDC 时看到了这个问题。解决方法是删除该请求类型规范。尝试评论您的线路:curl_setopt($ch, constant("CURLOPT_" . 'POST'), true);

    【讨论】:

      猜你喜欢
      • 2018-04-09
      • 2017-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-17
      • 1970-01-01
      • 2015-12-27
      相关资源
      最近更新 更多