【发布时间】:2021-07-27 00:03:33
【问题描述】:
我有类似下面的代码 -
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解析整个东西吗?
【问题讨论】:
标签: python flask web-crawler