python - 有没有办法解析 python-flask oauth2
问题描述
我有类似下面的代码 -
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解析整个事情吗?
解决方案
听起来像是 Selenium 的工作!它可以打开网络浏览器并为您解析所需的详细信息。
启动服务器后运行以下代码
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']
您可以参考的一些来源:
推荐阅读
- javascript - 无法将对象转换为类型间隔打字稿
- javascript - 解析从 c# 返回的 json 字符串数组
- javascript - 在已经使用完全相同的参数触发后,我想触发 mouseup 事件
- c# - 使用 Automapper 将相关实体属性映射到视图模型属性
- javascript - 如何允许使用 slimerJS 访问浏览器地理定位 API?
- matlab - (在 Matlab 中)如何在 for 循环中间切换到另一个工作目录?
- powerbi - 在 PowerBI 中将一列列表拆分为多列
- python - NLTK 解析树中的转义括号
- android-studio - Android Studio gradle 发布构建需要无限时间
- c# - 为什么 Razor 在 html 元素上设置的条件样式总是 html 编码不正确