How to fix KeyError: 'device_code' when trying to set up OAuth2 sign-in on my website

Robert Piperno 20 Reputation points
2023-07-07T19:51:58.8166667+00:00

I am receiving the following error after following this quickstart tutorial: https://learn.microsoft.com/en-us/azure/active-directory/develop/web-app-quickstart?pivots=devlang-python&tabs=windows

Capture

Full Error Code:

Traceback (most recent call last):
  File "C:\Python\Lib\site-packages\flask\app.py", line 2552, in __call__
    return self.wsgi_app(environ, start_response)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python\Lib\site-packages\werkzeug\middleware\proxy_fix.py", line 182, in __call__
    return self.app(environ, start_response)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python\Lib\site-packages\flask\app.py", line 2532, in wsgi_app
    response = self.handle_exception(e)
               ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python\Lib\site-packages\flask\app.py", line 2529, in wsgi_app
    response = self.full_dispatch_request()
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python\Lib\site-packages\flask\app.py", line 1825, in full_dispatch_request
    rv = self.handle_user_exception(e)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python\Lib\site-packages\flask\app.py", line 1823, in full_dispatch_request
    rv = self.dispatch_request()
         ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python\Lib\site-packages\flask\app.py", line 1799, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Intranet\ms-identity-python-webapp-main\app.py", line 40, in auth_response
    result = auth.complete_log_in(request.args)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python\Lib\site-packages\identity\web.py", line 146, in complete_log_in
    result = self._build_msal_app(cache=cache).acquire_token_by_device_flow(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python\Lib\site-packages\msal\application.py", line 1674, in acquire_token_by_device_flow
    code=flow["device_code"],  # 2018-10-4 Hack:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyError: 'device_code'
Microsoft Security Microsoft Entra Microsoft Entra ID
{count} votes

Accepted answer
  1. Shweta Mathur 30,296 Reputation points Microsoft Employee Moderator
    2023-07-10T08:45:02.3833333+00:00

    Hi @Robert Piperno ,

    Thanks for reaching out.

    The URL you are trying to access the application is not correct. http://localhost:5000/getAToken is the redirect URL where the authorization server sends the user once the app has been successfully authorized and granted an authorization code or access token.

    You can access the application directly using http://localhost:5000 or http://localhost:5000/login to sign in the application and call the downstream API.

    User's image

    Once you are able to login, you can get the token on console which sent to API directly as bearer token to call the API in the application.

    User's image

    Hope this will help.

    Thanks,

    Shweta


    Please remember to "Accept Answer" if answer helped you.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.