[英]Discord OAUTH2 with node.js
I need discord OAUTH2 login system but i dont know how.我需要 discord OAUTH2 登录系统,但我不知道怎么做。 I was trying something but it shows me "loginned" again when im not loginned.
我正在尝试一些事情,但是当我没有登录时它再次显示我“已登录”。
i was trying this but i dont know how to do that, if im not loginned that it shows me im not loginned.我正在尝试这个,但我不知道该怎么做,如果我没有登录,它会显示我没有登录。
const express = require("express")
const fetch = require('node-fetch')
const { URLSearchParams } = require('url')
const app = express()
var config = {
"clientId": process.env["CLIENT_ID"],
"clientSecret": process.env['CLIENT_SECRET'],
"redirectUri": "redirect uri"
}
app.get("/", (request, response) => {
response.send("login with discord: <a href='redirect url'>login</a>")
})
app.get("/authorize", (request, response) => {
var code = request.query["code"]
var params = new URLSearchParams()
params.append("client_id", config["clientId"])
params.append("client_secret", config["clientSecret"])
params.append("grant_type", "authorization_code")
params.append("code", code)
params.append("redirect_uri", config["redirectUri"])
fetch(`https://discord.com/api/oauth2/token`, {
method: "POST",
body: params
})
.then(res => res.json())
.then(json => {
response.send("logged in")
})
})
app.listen(80, () => {
console.log("Listening on :80")
})
Using axis with POST call.通过 POST 调用使用轴。 It makes life easier.
它让生活更轻松。
Add Redirects, Copy Client and Client Secret And give administrator permission (for test purpose)添加重定向、复制客户端和客户端密码并授予管理员权限(用于测试目的)
config.json
with Client ID/Secret and Redirects URI config.json
It will use demo.js
for get Token API call.它将使用
demo.js
获取令牌 API 调用。
{
"CLIENT_ID" : "********** your Client ID *********",
"CLIENT_SECRET" : "********** your Client Secret *********",
"REDIRECT_URI" : "http://localhost:3000/api/callback" <- this should be matched your REDIRECT_URI of Developer Portal
}
demo.js
file name. demo.js
文件名的 Express App Server。const express = require("express")
const axios = require('axios')
const config = require('./config.json');
const app = express()
app.get("/login", (request, response) => {
const redirect_url = `https://discord.com/oauth2/authorize?response_type=code&client_id=${config.CLIENT_ID}&scope=identify&state=123456&redirect_uri=${config.REDIRECT_URI}&prompt=consent`
response.redirect(redirect_url);
})
app.get("/api/callback", async (request, response) => {
const code = request.query["code"]
const resp = await axios.post('https://discord.com/api/oauth2/token',
new URLSearchParams({
'client_id': config.CLIENT_ID,
'client_secret': config.CLIENT_SECRET,
'grant_type': 'authorization_code',
'redirect_uri': config.REDIRECT_URI,
'code': code
}),
{
headers:
{
'Content-Type': 'application/x-www-form-urlencoded'
}
})
response.send('Logged In: ' + JSON.stringify(resp.data));
})
app.listen(3000, () => {
console.log("Listening on :3000")
})
$ npm install express axios
$ node demo.js
Listening on :3000
http://localhost:3000/login
It will be forward to discord login page.它将转发到 discord 登录页面。 If shows this screen, press
如果显示此屏幕,请按
button.
按钮。
Back to call my express URL then display access token and logged in message.返回呼叫我的快递 URL,然后显示访问令牌和登录消息。
Returned Result this JSON format.返回结果为 JSON 格式。
{
"access_token": "*****************************",
"expires_in": 604800,
"refresh_token": "*****************************",
"scope": "identify",
"token_type": "Bearer"
}
The code above is almost done, but that is not store an authorization data that use to get user info.上面的代码差不多完成了,但是它没有存储用于获取用户信息的授权数据。 You need to make another request to the Discord API to get user info.
您需要向 Discord API 再次请求以获取用户信息。 install axios and add the code below
安装 axios 并添加下面的代码
axios.get("https://discord.com/api/users/@me", make_config(data.access_token)).then(response => {
res.status(200).send(response.data.username);
}).catch(err => {
console.log(err);
res.sendStatus(500);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.