Skip to content

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

Open
@mengshun2022

Description

@mengshun2022

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…

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions