Php 如何格式化Oauth 2.0请求以包含作用域

Php 如何格式化Oauth 2.0请求以包含作用域,php,oauth-2.0,http-headers,lamp,lichess,Php,Oauth 2.0,Http Headers,Lamp,Lichess,我正在编写一个PHP web程序,需要访问和编辑lichess.org上的用户信息。他们的api使用OAuth2.0来实现这一点。问题是,无论我在请求中输入什么,它似乎只请求公共配置文件信息:。我想我可能对url的格式设置不正确,但我不知道如何设置,因为url是用JavaScript编写的,我对此不太熟悉。下面是我用来格式化字符串的代码片段: $scopes = "email:read%20preference:write"; $state=generate_string(20); $autho

我正在编写一个PHP web程序,需要访问和编辑lichess.org上的用户信息。他们的api使用OAuth2.0来实现这一点。问题是,无论我在请求中输入什么,它似乎只请求公共配置文件信息:。我想我可能对url的格式设置不正确,但我不知道如何设置,因为url是用JavaScript编写的,我对此不太熟悉。下面是我用来格式化字符串的代码片段:

$scopes = "email:read%20preference:write";
$state=generate_string(20);
$authorize_url = "https://oauth.lichess.org/oauth/authorize";

function getAuthorizationCode() {
        global $authorize_url, $client_id, $callback_uri;

        $authorization_redirect_url = $authorize_url . "?response_type=code&client_id=" . $client_id . "&redirect_uri=" . $callback_uri . "&scope=" . $scopes . "&state=".$state;

        header("Location: " . $authorization_redirect_url);

}
为了生成状态字符串,我使用了这个helper函数

    function generate_string($strength,$charset='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') {
    $Length = strlen($charset);
    $random_string = '';
    for($i = 0; $i < $strength; $i++) {
        $random_character = $charset[mt_rand(0, $Length - 1)];
        $random_string .= $random_character;
    }

    return $random_string;
}
function generate_string($strength,$charset='0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvxyz')){
$Length=strlen($charset);
$random_string='';
对于($i=0;$i<$strength;$i++){
$random_character=$charset[mt_rand(0,$Length-1)];
$random\u string.=$random\u字符;
}
返回$random_字符串;
}
果然,当我实际尝试请求资源时,响应是“缺少范围”。我正在使用:

$lichessurl = "https://lichess.org/api/account/email";
function getAccessToken($authorization_code) {
        global $token_url, $client_id, $client_secret, $callback_uri;

        $authorization = base64_encode("$client_id:$client_secret");
        $header = array("Authorization: Basic {$authorization}","Content-Type: application/x-www-form-urlencoded");
        $content = "grant_type=authorization_code&code=$authorization_code&redirect_uri=$callback_uri";

        $curl = curl_init();
        curl_setopt_array($curl, array(
                CURLOPT_URL => $token_url,
                CURLOPT_HTTPHEADER => $header,
                CURLOPT_SSL_VERIFYPEER => false,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_POST => true,
                CURLOPT_POSTFIELDS => $content
        ));
        $response = curl_exec($curl);
        curl_close($curl);

        if ($response === false) {
                echo "Failed";
                echo curl_error($curl);
                echo "Failed";
        } elseif (json_decode($response)->error) {
                echo "Error:<br />";
                echo $authorization_code;
                echo $response;
        }

        return json_decode($response)->access_token;
}
$lichessurl=”https://lichess.org/api/account/email";
函数getAccessToken($authorization\u code){
全局$token\u url、$client\u id、$client\u secret、$callback\u uri;
$authorization=base64_encode($client_id:$client_secret”);
$header=array(“授权:基本{$Authorization}”,“内容类型:application/x-www-form-urlencoded”);
$content=“grant\u type=authorization\u code&code=$authorization\u code&redirect\u uri=$callback\u uri”;
$curl=curl_init();
curl_setopt_数组($curl,数组(
CURLOPT_URL=>$token_URL,
CURLOPT_HTTPHEADER=>$header,
CURLOPT_SSL_VERIFYPEER=>false,
CURLOPT_RETURNTRANSFER=>true,
CURLOPT_POST=>true,
CURLOPT_POSTFIELDS=>$content
));
$response=curl\u exec($curl);
curl_close($curl);
如果($response==false){
echo“失败”;
回波旋度误差($curl);
echo“失败”;
}elseif(json_解码($response)->错误){
回显“错误:
”; echo$authorization\u代码; 回音$应答; } 返回json_decode($response)->access_令牌; }
为了做到这一点