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

unable to request an API enabled with @requires_auth #21

Open
MSA028 opened this issue Apr 4, 2024 · 0 comments
Open

unable to request an API enabled with @requires_auth #21

MSA028 opened this issue Apr 4, 2024 · 0 comments

Comments

@MSA028
Copy link

MSA028 commented Apr 4, 2024

The following is the code I'm using to get token

from utills.config import AppConfigs
import subprocess,os,json,requests
def get_ad_token():
    curl_cmd = [
    "curl",
    "-X","post",
    f"https://login.microsoftonline.com/{AppConfigs.TENANT_ID}/oauth2/v2.0/token",        
    "-d", "grant_type=client_credentials",        
    "-d", f"client_id={AppConfigs.CLIENT_ID}",        
    "-d", f"client_secret={AppConfigs.CLIENT_SECRET}",        
    "-d", f"scope=api://{AppConfigs.CLIENT_ID}/.default"
    ]
    res=subprocess.run(curl_cmd,capture_output=True,text=True)
    access_token = json.loads(res.stdout)
    return access_token["access_token"]

This token I'm passing as Authorization header to api and it's giving an error
IDW10201: No scope or app permission (role) claim was found in the bearer token

Here is API code
    from fastapi import APIRouter,Request
    from fastapi_microsoft_identity import AuthError,requires_auth,validate_scope
    import fastapi
    from fastapi.exceptions import HTTPException
    from pydantic import ValidationError
    from utills.config import AppConfigs
    
    
    poc_router = APIRouter()
    scope = "access_api"
    
    
    @poc_router.get("/add/{num1}/{num2}")
    @requires_auth
    async def add_with_auth(request:Request,num1:int,num2:int):
        try:
            validate_scope(required_scope=scope,request=request)
            return num1+num2
        except AuthError as ae:
            return fastapi.Response(content=ae.error_msg, status_code=ae.status_code)
        except ValidationError as ve:
            return fastapi.Response(content=ve.error_msg, status_code=ve.status_code)
        except Exception as x:
            raise HTTPException(status_code=403,detail=f"Failed with Excetion--{e}")
Please help me how to get proper access token that can be sent to api
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant