当前位置: 首页 > 知识库问答 >
问题:

有没有办法解析蟒蛇烧瓶oauth2

蓬化
2023-03-14

我有如下代码-

app = Flask(__name__)


# access token
access_token = None


@app.route('/getcode')
def get_authorization_url():
    oauth = OAuth2Session(client_id, redirect_uri=redirect_uri, scope=scope)
    authorization_url, _state = oauth.authorization_url(authorization_base_url, access_type="authorization_code")
    print('authorization_url')
    print(authorization_url)
    return redirect(authorization_url)


@app.route('/')
def callback():
    global access_token
    oauth = OAuth2Session(client_id, redirect_uri=redirect_uri, scope=scope)
    token = oauth.fetch_token(token_url, authorization_response=request.url, client_secret=client_secret)
    access_token = token['access_token']
    print('access token is:', access_token)

    ## we will be shutting down the server after getting access_token
    ## the thread created here is copied in if __name__ == '__main__' block
    ## and will run after closing the server

    # th = threading.Thread(target=data_from_resource_server, args=(access_token,))
    # th.start()

    func = request.environ.get('werkzeug.server.shutdown')
    if func:
        print('stoping server')
        func()


    return 'see terminal for logs'


if __name__ == '__main__':
    app.secret_key = 'example'
    app.env = 'development'
    print()
    print('Open this url in browser:', 'http://127.0.0.1/getcode', end='\n\n')

    app.run(host='127.0.0.1', port='80')

    print('server stopped')

    ## got access_token, closed the server, now running ray integration code
    if access_token:
        th = threading.Thread(target=data_from_resource_server, args=(access_token,))
        th.start()

在这里,当app.run(host='127.0.0.1 ',port='80 ')运行时,会给出URL - http://127.0.0.1/getcode。我需要手动打开输入用户名和密码,然后又一个窗口来输入YOB,然后给我这样的东西-

127.0.0.1 - - [04/May/2021 21:20:23] "GET /**getcode?code=G7h_QL0Cpo3kEqyyNBZ68DTX3JhQ_6E6sl_Sk1x5iBc.oG4JFQiKyZGupTuJ-bV6qE9lA**&scope=orders&state=M6hdb7EJxgNKkuBqbihg1SKaUGAJ7W HTTP/1.1" 302  

我的问题是,有没有一种方法可以避免手动打开浏览器,输入凭证并获取代码。我们可以用python来解析整个事情吗?

共有1个答案

颜森
2023-03-14

听起来像是硒的工作!它可以打开一个web浏览器,并为您解析所需的详细信息。

启动服务器后运行以下代码

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

url = 'http://127.0.0.1/getcode'
driver = webdriver.Firefox()  # (Or Chrome())
driver.get(url)

username = driver.find_element_by_id("username")
password = driver.find_element_by_id("password")

# uncomment this code if your text boxes have pre-populated text
#username.clear()
#password.clear()

username.send_keys("YourUsername") # change this to your username
password.send_keys("PassworD")     # change this to your password
driver.find_element_by_name("submit").click()

# we can implicitly wait before the page loads
driver.implicitly_wait(2)

现在,这解决了你问题的第一部分,即自动登录过程。现在我不确定你的下一个目标是什么,但我假设你想要URL中的代码变量,我假设它是由OAuth2函数返回的。

我们可以通过简单地获取URL并解析代码变量来实现这一点

获取URL

current_url = driver.current_url;

现在,您可以使用urlparse简单地解析URL。

import urllib.parse as urlparse
from urllib.parse import parse_qs

parsed = urlparse.urlparse(current_url)
OAuth_code = parse_qs(parsed.query)['code']

您可以参考以下来源:

    < Li > https://medium . com/swlh/automate-data-collection-with-selenium-in-python-246 a 051206 e 2 < li >在python中使用selenium填写用户名和密码 < li >单击链接后查找URL < Li > https://stack overflow . com/a/5075477/11029298 < Li > https://selenium-python . readthe docs . io/getting-started . html
 类似资料:
  • 我发现很难找到有关这方面的资料。会是什么?我如何解决这个问题?有哪些可能的修复方法? UWSGI日志文件 时钟来源:unix检测到CPU核数:4当前工作目录:/home/pi检测到二进制路径:/usr/local/bin/uwsgi!!!没有内部路由支持,重建与pcre支持!!!*警告:您在没有主进程管理器的情况下运行uWSGI进程数限制为7336内存页大小为4096字节检测到最大文件描述符号:6

  • 我需要在我的中添加一个新的目录位置,但问题是我使用的是一个全新安装的系统(Linux),其中尚未定义任何。我读过并使用过,我认为我很了解它,但我不知道当没有存在时会发生什么。 我不能附加到不存在的东西上,但我希望当前发现的所有重要库都能正常工作,因此要小心,我在Python中使用了来获取所有标准值。然后我为定义了一个-变量,包括我刚刚找到的所有节点,以及我的新目录。但是哇,很多东西都停止工作了!P

  • 我在这里遵循烧瓶教程: http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world 我达到了我尝试的程度/跑py和我得到: 这看起来类似于: ImportError:没有名为flask的模块 但他们的解决方案没有帮助。作为参考,我确实有一个名为flask的文件夹,其中一位用户提到该文件夹可能会导致问题

  • 在CentOS 6.4中,我在/var/www/html/venv文件夹中创建了python虚拟环境。然后在激活虚拟环境后,我为我的flask应用程序安装了所有必要的python库。我检查了一下,Flask库位于/var/www/html/venv/lib/python2.7/site-packages文件夹中。我已经安装并加载了mod_wsgi。现在,在我的flask应用程序中(位于/var/w

  • 问题内容: 我正在建立一个带有flask的网站,其中用户具有帐户并能够登录。我正在使用flask- principal作为登录部分和角色管理。有没有办法让用户的会话在5分钟或10分钟后过期?我在flask文档或flask- principal文档中找不到该文件。 我想到了一种手动方法,在登录时在服务器端设置一个带有时间标签的变量,并在用户执行下一个操作时,服务器会验证该时间戳记上的时间增量并删除会

  • 我正在运行Ubuntu 18.04。 我使用mysql连接器-python连接Python到MySQL。 我使用的是Python 3.6.7,并且已经安装了mysql连接器-python。 我已经安装了mysql连接器-python-py3_8.0.13-1ubuntu18.10_all.deb. 在运行Python脚本时,mysql。连接器模块似乎加载正确,但脚本在碰到光标时失败。next()具