1

我正在尝试从我们的 fusionauth 服务器中检索 OAUTH2 令牌 1.16.1
通过 SAML 登录工作非常完美。登录页面 php 请求 /oauth2/authorize?并且重定向到 /callback 有效。

登录页面 php

    $redirect_url = urlencode('http://test.eyecatcher.ch/callback');
    $location = 'https://{auth domain}/oauth2/authorize?client_id={client id}&response_type=code&redirect_uri=' . $redirect_url;
    header( "Location: " . $location );


在 fusionauth、OAuth 应用程序设置中,设置了“授权重定向 URL”。
不管输入哪个redirect_url,错误总是出现

回调页面php

    $client_id      = "{client id}";
    $clientSecret   = '{client secrect}';
    $base64Auth     = base64_encode($client_id . ":" . $clientSecret );

    $code           = $_GET[ "code" ];
    $url            = 'https://{auth domain}/oauth2/token';
    $redirect_url   = urlencode('http://test.eyecatcher.ch/callback');

    $header = array(    'Authorization: Basic ' .$base64Auth .'', 
                        'Content-Type: application/x-www-form-urlencoded' );

    $post = [
        'code' => $code,
        'grant_type'   => 'authorization_code',
        'redirect_uri'   => $redirect_url,
        ];


    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url );
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header );
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));

    $response = curl_exec($ch);
    var_export($response);

在此请求之后,我收到此错误消息。

{"error":"invalid_grant","error_description":"redirect_uri: http%3A%2F%2Ftest.eyecatcher.ch%2Fcallback is not valid.","error_reason":"invalid_redirect_uri"}
4

1 回答 1

2

这看起来不是 FusionAuth 的问题。

PHP 函数http_build_query将代表您对请求正文进行 URL 编码。

redirect_url因为您正在使用urlencode我的猜测进行预编码,redirect_uri所以一旦 FusionAuth 解码该值,现在双重编码的值与您配置的授权重定向 URL 不匹配。

这是一个仅使用http_build_query. https://stackoverflow.com/a/5676572/3892636

于 2020-06-09T21:54:04.643 回答