gpt4 book ai didi

authentication - 在我的网站上设置 Discord oauth2 登录(使用 PHP?)

转载 作者:行者123 更新时间:2023-12-05 00:58:59 26 4
gpt4 key购买 nike

所以我在学习如何通过我的网站上的不和谐设置登录时遇到了麻烦。我已经浏览了几个小时,但找不到任何我理解的东西......

目前,我已经创建了 discord 应用程序,给了我一个客户端 ID 和客户端密码,以及一个返回到我的本地主机的链接:

https://discordapp.com/api/oauth2/authorize?client_id=550631359337594881&redirect_uri=http%3A%2F%2Flocalhost&response_type=code&scope=identify

目前,我已将其设置为从按钮重定向到该 URL,然后该 URL 将我发送到不和谐以接受。然后它让我回到 http://localhost?code=CODE_HERE

但是,我不知道我应该如何处理这段代码。我正在尝试对其进行设置,以便它显示带有主题标签的人的用户名以及他们的个人资料图片。

我目前在网站上使用 HTML、CSS、JS 和 PHP,但我想我可能需要使用其他东西,但我不知道如何设置,或者我需要什么。我正在使用 XAMPP 运行我的本地服务器。我更喜欢它只是 PHP,但我对其他选项持开放态度。

有人知道如何将代码转换为用户名+图片吗?

提前致谢!

最佳答案

试试这个

归功于:eslachance

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('max_execution_time', 300); //300 seconds = 5 minutes. In case if your CURL is slow and is loading too much (Can be IPv6 problem)

error_reporting(E_ALL);

define('OAUTH2_CLIENT_ID', '1234567890'); //Your client Id
define('OAUTH2_CLIENT_SECRET', 'verysecretclientcode'); //Your secret client code

$authorizeURL = 'https://discordapp.com/api/oauth2/authorize';
$tokenURL = 'https://discordapp.com/api/oauth2/token';
$apiURLBase = 'https://discordapp.com/api/users/@me';

session_start();

// Start the login process by sending the user to Discord's authorization page
if(get('action') == 'login') {

$params = array(
'client_id' => OAUTH2_CLIENT_ID,
'redirect_uri' => 'https://yoursite.location/ifyouneedit',
'response_type' => 'code',
'scope' => 'identify guilds'
);

// Redirect the user to Discord's authorization page
header('Location: https://discordapp.com/api/oauth2/authorize' . '?' . http_build_query($params));
die();
}


// When Discord redirects the user back here, there will be a "code" and "state" parameter in the query string
if(get('code')) {

// Exchange the auth code for a token
$token = apiRequest($tokenURL, array(
"grant_type" => "authorization_code",
'client_id' => OAUTH2_CLIENT_ID,
'client_secret' => OAUTH2_CLIENT_SECRET,
'redirect_uri' => 'https://yoursite.location/ifyouneedit',
'code' => get('code')
));
$logout_token = $token->access_token;
$_SESSION['access_token'] = $token->access_token;


header('Location: ' . $_SERVER['PHP_SELF']);
}

if(session('access_token')) {
$user = apiRequest($apiURLBase);

echo '<h3>Logged In</h3>';
echo '<h4>Welcome, ' . $user->username . '</h4>';
echo '<pre>';
print_r($user);
echo '</pre>';

} else {
echo '<h3>Not logged in</h3>';
echo '<p><a href="?action=login">Log In</a></p>';
}


if(get('action') == 'logout') {
// This must to logout you, but it didn't worked(

$params = array(
'access_token' => $logout_token
);

// Redirect the user to Discord's revoke page
header('Location: https://discordapp.com/api/oauth2/token/revoke' . '?' . http_build_query($params));
die();
}

function apiRequest($url, $post=FALSE, $headers=array()) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

$response = curl_exec($ch);


if($post)
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));

$headers[] = 'Accept: application/json';

if(session('access_token'))
$headers[] = 'Authorization: Bearer ' . session('access_token');

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$response = curl_exec($ch);
return json_decode($response);
}

function get($key, $default=NULL) {
return array_key_exists($key, $_GET) ? $_GET[$key] : $default;
}

function session($key, $default=NULL) {
return array_key_exists($key, $_SESSION) ? $_SESSION[$key] : $default;
}

?>

关于authentication - 在我的网站上设置 Discord oauth2 登录(使用 PHP?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54936975/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com