使用cURL连接到OpenId会产生错误

时间:2017-10-30 08:49:37

标签: php api curl oauth openid

我使用以下代码访问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'."}

有人可以帮我确定我的代码是否正确?

谢谢!

2 个答案:

答案 0 :(得分:0)

添加以下内容:

curl_setopt($ curl,CURLOPT_SSL_VERIFYPEER,false);

curl_setopt($ curl,CURLOPT_SSL_VERIFYHOST,false);

答案 1 :(得分:0)

您的IdP重定向中可能更改了请求的方法。当我用myFun <- function(label1, value1, label2, value2, which_label) { return(ifelse(label1 == which_label, value1, value2)) } desired_dat <- data.frame(first_col = mapply(FUN = myFun, dat$label_1, dat$value_1, dat$label_2, dat$value_2, MoreArgs = list(which_label = 'first_col'), SIMPLIFY = TRUE), second_col = mapply(FUN = myFun, dat$label_1, dat$value_1, dat$label_2, dat$value_2, MoreArgs = list(which_label = 'second_col'), SIMPLIFY = TRUE)) head(desired_dat) first_col second_col 1 88 84 2 40 30 3 98 32 4 64 80 5 34 43 6 10 52 测试我的OIDC时,我已经看到了这个问题。解决方法是删除该请求类型规范。尝试对您的专栏发表评论:curl -XPOST