成为discord开发者
- 成为discord开发者
- 创建应用
- APPLICATION ID 应用ID, PUBLIC KEY 暂时无用(随时可以看)
- 在OAuth2中 CLIENT ID, CLIENT SECRET 是需要的, 第一打开需要生成secret, 保存
- 填写回调地址
- URL Generator 中可以生成你的登录跳转地址, 选择identify和email 就可以
- $url = ‘https://discord.com/api/oauth2/authorize?client_id=’. env(‘DISCORD_CLIENT_ID’) . ‘&redirect_uri=’ . env(‘DISCORD_REDIRECT_URI’) . ‘&response_type=code&scope=email%20identify’;
回调接口
public function callbackDiscord(request $request) { $tokenUrl = 'https://discord.com/api/oauth2/token'; $userUrl = 'https://discord.com/api/users/@me'; $all = $request->all(); if(isset($all['code']) && $all['code']){ $headers = [ 'Content-Type' => 'application/x-www-form-urlencoded' ]; $data= [ 'client_id' => env('DISCORD_CLIENT_ID'), 'client_secret' => env('DISCORD_CLIENT_SECRET'), 'grant_type' => 'authorization_code', 'redirect_uri' => env('DISCORD_REDIRECT_URI'), 'code' => $all['code'] ]; $res = $this->postUrl($tokenUrl, $data, $headers); if(isset($res['access_token']) && $res['access_token']){ $header = [ 'Authorization: Bearer ' . $res['access_token'] ]; $user = $this->getUrl($userUrl, $header); } } } public function postUrl($url, $data = [], $headerArr = []) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data)); curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArr); curl_setopt($curl, CURLOPT_TIMEOUT, 60); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($curl); curl_close($curl); return json_decode($output,true); } public function getUrl($url, $headerArr = []){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch,CURLOPT_HTTPHEADER, $headerArr); $output = curl_exec($ch); curl_close($ch); $output = json_decode($output, true); return $output; }
来源地址:https://blog.csdn.net/CNian_/article/details/127789371
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341