Setting up Microsoft Identity Platform with FastApi

Viewed 54

I am following this tutorial on how to generate a code and access token using Microsoft's identity platform. I am able to get the code by hitting the /authorize endpoint in a browser from a redirect. Then using thunderclient in VSCode and I am able to get an access token and use that access token to call microsoft graph and get my information.

I am using FastApi for the backend of my webapp and I am trying to set up API endpoints to retrieve the code and access tokens through python's Requests library. However, when I hit the /authorize endpoint, within my swagger docs, it does not redirect to the microsoft login screen like it would if I did it within a browser. Here's what I am trying to do:

def get_code():
    url = 'https://login.microsoftonline.com/organizations/oauth2/v2.0/authorize'

    params = {'client_id': 'my-client-id', 'response_type': 'code',
            'redirect_uri': 'http://localhost:4200', 'response_mode': 'query',
            'scope': 'offline_access%20user.read%20mail.read', 'state': '12345'
    }

    resp = requests.get(url=url, params=params)
    return resp.text
@router.get("/code")
def get_user_code():
    user_code = login_helper.get_code()
    return user_code

The output seems like it returns the Microsoft login screen and tries to redirect but does not.

<!-- Copyright (C) Microsoft Corporation. All rights reserved. -->
<!DOCTYPE html>
<html dir="ltr" class="" lang="en">
  <head>
    <title>Sign in to your account</title>

Is it possible to be redirected when hitting the endpoint in swagger? If not, What would be the best way to get the code within FastApi?

Any help would be appreciated.

0 Answers
Related