使用Grant CodeFlow进行PHP-Oauth身份验证

时间:2018-10-08 08:02:30

标签: php curl post oauth request

我实现OAUTH身份验证。我正在将POST请求发送到API。但是我的代码出了点问题。我收到错误404。有人能帮我吗。该API不支持ClientSecret。 这是我的代码

<?php

$callback_uri  =  "http://localhost/zeiterfassung/" ;

$client_id  =  "2371031349-3075189699-483870032-1896673008" ;

$code = $_GET['code'];

$resourc = "http://localhost/zeiterfassung/";

if (isset($_GET['code'])) {
    // try to get an access token
    $code = $_GET['code'];
    $url = 'https://adfs3int.fds.metro.info/oauth2/token';
    $params = array(
        "code" => $code,
        "client_id" => $client_id,
        "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://adfs3int.fds.metro.info/adfs/oauth2/authorize";

    $params = array(
        "response_type" => "code",
        "client_id" => $client_id,
        "redirect_uri" => $callback_uri,
        "resource" => $resourc
    );

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

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

enter image description here

1 个答案:

答案 0 :(得分:0)

404是一个客户端站点错误,并且说您做错了什么-这意味着您所寻找的内容不在您所寻找的位置。 就像您在壁橱中搜索袜子,但找不到袜子是因为袜子不在壁橱中。

我看起来请求的api托管在您的本地主机上,也许该api在其他文件夹中?