Python Discord Oauth2如何使只有公会成员才能访问我的网站

Python Discord Oauth2如何使只有公会成员才能访问我的网站,python,oauth-2.0,discord,Python,Oauth 2.0,Discord,所以我有一个discord服务器,我想制作一个Oauth2,它只允许访问我的discord服务器中的成员到我的网站,但我是编程爱好者,不知道如何做到这一点,如果有人知道,请帮助这里是我到目前为止编写的代码教程我使用python脚本 . """ from requests_oauthlib import OAuth2Session import getpass from flask import Flask, request, redirect, session im

所以我有一个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) ```


。
"""
从请求导入OAuth2Session
导入getpass
从烧瓶导入烧瓶、请求、重定向、会话
导入操作系统
进口不和
#禁用SSL要求
os.environ['OAUTHLIB\u unsecure\u TRANSPORT']=“1”
#应用程序的设置
基本\u不一致\u api\u url=https://discordapp.com/api'
客户id=r'753694107364229211'#从https://discordapp.com/developers/applications
client_secret=getpass.getpass('qauunpggz0oj1wt51n76fax8eq5b4g9')
重定向http://localhost:8000/oauth_callback'
范围=[“识别”、“电子邮件”、“协会”]
令牌https://discordapp.com/api/oauth2/token'
授权https://discordapp.com/api/oauth2/authorize'
app=烧瓶(名称)
app.secret_key=os.uradom(24)
bot=discord.client
@附件路线(“/”)
def home():
"""
显示“不协调登录”链接
"""
oauth=OAuth2Session(客户端id,重定向uri=重定向uri,范围=scope)
login\u url,state=oauth.authorization\u url(authorization\u url)
会话['state']=状态
打印(“登录url:%s”%Login\u url)
返回“”
@应用程序路径(“/oauth\u回调”)
def oauth_callback():
"""
我们在应用程序中指定的回调。
处理Discord提供给我们的代码并将其发回
请求临时访问令牌以便我们可以
代表用户(好像我们是)提出请求。
例如https://discordapp.com/api/users/@我
令牌存储在会话变量中,因此可以
可以跨单独的web请求重用。
"""
discord=OAuth2Session(客户端id,重定向uri=redirect\uri,状态=会话['state'],作用域=作用域)
令牌=discord.fetch\u令牌(
令牌地址,
客户机密=客户机密,
授权\响应=request.url,
)
会话['discord_token']=token
return“感谢您授予我们授权。我们正在让您登录!您现在可以访问”
@应用程序路线(“/profile”)
def配置文件():
"""
示例配置文件页面,演示如何提取用户信息
一旦我们在所有OAuth协商之后获得了有效的访问令牌。
"""
discord=OAuth2Session(客户端id,令牌=会话['discord\u令牌])
response=discord.get(base_discord_api_url+'/users/@me')
# https://discordapp.com/developers/docs/resources/user#user-对象用户结构
返回“配置文件:%s”%response.json()['id']
#或者像这样跑
#烧瓶APP=ss
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
应用程序运行(主机=0.0.0.0',端口=8000)```