简体   繁体   English

相当于 FastAPI 中的 flask.request.url

[英]equivalent of flask.request.url in FastAPI

i am trying to change google oauth2 example code from python flask to FastAPI.我正在尝试将 google oauth2 示例代码从 python flask 更改为 FastAPI。

this is the code这是代码

def oauth2callback():
# Specify the state when creating the flow in the callback so that it can
# verified in the authorization server response.
state = flask.session['state']

flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
    CLIENT_SECRETS_FILE, scopes=SCOPES, state=state)
flow.redirect_uri = flask.url_for('oauth2callback', _external=True)
# Use the authorization server's response to fetch the OAuth 2.0 tokens.
print("this is the flask request url -----------", flask.request.url)
**authorization_response = flask.request.url**

flow.fetch_token(authorization_response=authorization_response)
# Store credentials in the session.
# ACTION ITEM: In a production app, you likely want to save these
#              credentials in a persistent database instead.

credentials = flow.credentials
flask.session['credentials'] = credentials_to_dict(credentials)

creds = google.oauth2.credentials.Credentials(
    **flask.session['credentials'])

return flask.redirect(flask.url_for('test_api_request'))

the line is authorization_response = flask.request.url the value of it is "http://localhost:8080/oauth2callback?state=79aCVleeoxdA4bYgo5YnzuK8vsvM22&code=4%2F0AWtgzh611Sc3dhUo_pqQSa4RVTEZOgX9rJHc328XCJ4UmLvHdg5zz3t1k8VS3ihZwKMEA&scope=email+profile+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile+openid+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fgmail.readonly+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fgmail.modify+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fgmail.labels+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email&authuser=0&prompt=none" the line is authorization_response = flask.request.url the value of it is "http://localhost:8080/oauth2callback?state=79aCVleeoxdA4bYgo5YnzuK8vsvM22&code=4%2F0AWtgzh611Sc3dhUo_pqQSa4RVTEZOgX9rJHc328XCJ4UmLvHdg5zz3t1k8VS3ihZwKMEA&scope=email+profile+https%3A%2F%2Fwww.googleapis.com% 2Fauth%2Fuserinfo.profile+openid+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fgmail.readonly+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fgmail.modify+https%3A%2F% 2Fwww.googleapis.com%2Fauth%2Fgmail.labels+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email&authuser=0&prompt=none"

now i just want to change this line to the same in fastapi, i have tried request.url.path but it gives me "/oauth2callback" or sometimes None.现在我只想在 fastapi 中将此行更改为相同的行,我已经尝试过 request.url.path 但它给了我“/oauth2callback”或有时没有。

The request.url property is a string-like object, and you can get the full URL by casting it as a string. request.url属性是一个类似字符串的 object,您可以通过将其转换为字符串来获得完整的 URL。

ie str(request.url)str(request.url)

You can also access properties of the URL individually - more information on this can be found in Starlette's documentation .您还可以单独访问 URL 的属性——有关这方面的更多信息,请参阅 Starlette 的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM