Azure-Samples/fastapi-on-azure-functions

/docs → Failed to load API definition - Fetch Error - Unauthorized /openapi.json

pietz opened this issue · 6 comments

pietz commented

Thank you for providing this example. I believe the template doesn't apply here.

Everything works as described however when trying to access the OpenAPI definition through /docs, I'm getting the following error:

Screenshot 2022-04-06 at 20 37 40

I'm trying to do this on AuthLevel "Function" and couldn't find any other references to this problem.

I have the same problem with AuthLevel = Function, Did someone find the solution?

Same problem here with AuthLevel = Function... anyone?

pietz commented

Slightly dirty workaround that makes it possible to view the docs but doesn't allow to "Try out" the API.

from fastapi.openapi.docs import get_swagger_ui_html

app = FastAPI(docs_url=None)

@app.get("/docs", include_in_schema=False)
async def get_docs(code: str):
    openapi_url = "/openapi.json?code=" + code
    return get_swagger_ui_html(openapi_url=openapi_url, title="docs")

If you want to use the Try it out feature, you can use APIKeyHeader and Security to define the x-functions-key header:

from fastapi import FastAPI, Security
from fastapi.openapi.docs import get_swagger_ui_html
from fastapi.security import APIKeyHeader

app = FastAPI(docs_url=None)

functions_header = APIKeyHeader(
    name="x-functions-key", description="Azure Functions key"
)


@app.get("/docs", include_in_schema=False)
async def get_docs(code: str | None = None):
    if not code:
        code = ""
    openapi_url = "/openapi.json?code=" + code
    return get_swagger_ui_html(openapi_url=openapi_url, title="docs")


@app.get("/items/{item_id}")
async def get_items(item_id: str, key: str = Security(functions_header)) -> dict:
    return {item_id: "Foo"}

When deployed to Azure Functions, you would still have to pass the Functions key as a query parameter when accessing the /docs endpoint, like /docs?code=your-functions-key, but you'll be able to use the Authorize button to pass the functions key and try out the API

I've tried @farrukh-t and @pietz suggestions above above (if the try doesn't not work is not a biggy but at least having the docs working) but I am just getting constantly the same Failed to load API definition.

import azure.functions as func

from WrapperFunction import app as fastapi_app

app = func.AsgiFunctionApp(app=fastapi_app, http_auth_level=func.AuthLevel.FUNCTION)
from fastapi import FastAPI, Security
from fastapi.openapi.docs import get_swagger_ui_html
from fastapi.security import APIKeyHeader

app = FastAPI(docs_url=None)

functions_header = APIKeyHeader(
    name="x-functions-key", description="Azure Functions key"
)


@app.get("/docs", include_in_schema=False)
async def get_docs(code: str | None = None):
    if not code:
        code = ""
    openapi_url = "/openapi.json?code=" + code
    return get_swagger_ui_html(openapi_url=openapi_url, title="docs")


@app.get("/sample")
async def hello_world(key: str = Security(functions_header)) -> dict:
    return {"message": "Hello World"}

The 'code' is passed to the docs endpoint but error is the same.

image

Am I missing something or did things change since the above comment were posted ?