Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with adding oauth2.0 (flow=password) authentication function settings on the swagger page #596

Open
mengshun2022 opened this issue Mar 6, 2024 · 0 comments
Labels
question Further information is requested

Comments

@mengshun2022
Copy link

I want to add OAuth2.0 (flow = password) authentication to the Swagger page, and add mandatory authentication to some interfaces to access. I want to set up a request to log in to the oauth route, body parameters, username and password, but when the swagger page is used, it responds with a 415 message "Did not attempt to load JSON data because the request Content-Type was not 'application/json'." The reason is that Content-Type in headers: application/x-www-form-urlencoded instead of application/json, I tried specifying Content-Type = 'application/json' in headers when configuring authentication, but it didn't work.

#python mains.py
from flask import jsonify, Flask, Response
from typing import Union
from asgiref.wsgi import WsgiToAsgi
from flask_restx import Api, Resource, fields

app = Flask(name)

authorizations = {
"oauth2.0": {
"type": "oauth2",
"flow": "password",
"tokenUrl": "oauth",
"scopes": {"read": "获取信息权限", "write": "编辑权限"},
"paths": {
"/oauth": {
"post": {
"summary": "OAuth 2.0 认证",
"description": "Authorize 认证",
"parameters": [
{
"name": "body",
"in": "body",
"required": True,
"schema": {
"type": "object",
"properties": {
"username": {
"type": "string",
"description": "Username for authentication",
},
"password": {
"type": "string",
"description": "Password for authentication",
},
},
},
}
],
"responses": {"200": {"description": "Authorization successful"}},
}
}
},
}
}

api = Api(
app,
version="1.0",
title="Flask API",
description=f"Flask swagger",
doc="/docs",
authorizations=authorizations,
)

@api.route("/oauth")
class ExampleResource(Resource):
@api.expect(
api.model(
"obj",
{
"account": fields.String(required=True, description="账号"),
"password": fields.String(required=True, description="密码"),
},
),
validate=True,
)
def post(self):
return jsonify({"data": 111})

if name == "main":
import uvicorn

app = WsgiToAsgi(app)
uvicorn.run(app, host="0.0.0.0", port=7788)

Uploading 屏幕截图 2024-03-06 173139.png…

@mengshun2022 mengshun2022 added the question Further information is requested label Mar 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant