discord oauth2如何使只有公会成员才能访问我的网站

j8ag8udp  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(250)

所以我有一个discord服务器,我想做一个oauth2,只允许访问我的discord服务器中的成员到我的网站,但我业余编程,不知道如何做到这一点,所以如果有人知道,请帮助这里是我写的代码到目前为止从教程我使用python脚本。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

.
"""
from requests_oauthlib import OAuth2Session
import getpass
from flask import Flask, request, redirect, session
import os
import discord

# Disable SSL requirement

os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'

# Settings for your app

base_discord_api_url = 'https://discordapp.com/api'
client_id = r'753694107364229211' # Get from https://discordapp.com/developers/applications
client_secret = getpass.getpass('QAUuoNpgGZ0oj1Wt51n76FAX8eq5b4G9')
redirect_uri='http://localhost:8000/oauth_callback'

scope = ['identify', 'email' , 'guilds']
token_url = 'https://discordapp.com/api/oauth2/token'
authorize_url = 'https://discordapp.com/api/oauth2/authorize'

app = Flask(__name__)
app.secret_key = os.urandom(24)
bot = discord.client

@app.route("/")
def home():
    """
    Presents the 'Login with Discord' link
    """
    oauth = OAuth2Session(client_id, redirect_uri=redirect_uri, scope=scope)
    login_url, state = oauth.authorization_url(authorize_url)
    session['state'] = state
    print("Login url: %s" % login_url)
    return '<a href="' + login_url + '">Login with Discord</a>'

@app.route("/oauth_callback")
def oauth_callback():
    """
    The callback we specified in our app.
    Processes the code given to us by Discord and sends it back
    to Discord requesting a temporary access token so we can
    make requests on behalf (as if we were) the user.
    e.g. https://discordapp.com/api/users/@me
    The token is stored in a session variable, so it can
    be reused across separate web requests.
    """
    discord = OAuth2Session(client_id, redirect_uri=redirect_uri, state=session['state'], scope=scope)
    token = discord.fetch_token(
        token_url,
        client_secret=client_secret,
        authorization_response=request.url,
    )
    session['discord_token'] = token
    return 'Thanks for granting us authorization. We are logging you in! You can now visit <a href="/profile">/profile</a>'

@app.route("/profile")
def profile():
    """
    Example profile page to demonstrate how to pull the user information
    once we have a valid access token after all OAuth negotiation.
    """
    discord = OAuth2Session(client_id, token=session['discord_token'])
    response = discord.get(base_discord_api_url + '/users/@me')
    # https://discordapp.com/developers/docs/resources/user#user-object-user-structure
    return 'Profile: %s' % response.json()['id']

# Or run like this

# FLASK_APP=ss

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8000) ```

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题