Python Django /w Microsoft Graphs - I keep getting value error "state missing from auth_code_flow"

Viewed 1828

Python Django /w Microsoft Graphs -

I'm following this Microsoft Tutorial for building Django apps with Microsoft Graph (using it on my existing Django webapp), and I am having an issue with authentication: https://docs.microsoft.com/en-us/graph/tutorials/python

I'm on the step 'Add Azure AD authentication' and, after implementing, I hit the sign in button and enter credentials...and I keep getting value error "state missing from auth_code_flow".

The "callback" method is only making it to result=get_token_from_code(request) and then fails.

Here is the get_token_from_code method:

def get_token_from_code(request):
  cache = load_cache(request)
  auth_app = get_msal_app(cache)

  # Get the flow saved in session
  flow = request.session.pop('auth_flow', {})

  result = auth_app.acquire_token_by_auth_code_flow(flow, request.GET)
  save_cache(request, cache)

  return result

What I'm trying to do is eventually access excel online from my webapp.

Any help is greatly appreciated!

3 Answers

I just had this issue and resolved it. It is one of these two things:

  1. You are starting out at 127.0.0.1:8000 and then when you're redirected you're at localhost:8000, which is a different domain. The sessions aren't remembered from one domain to the other. The solution is to start out on localhost:8000 so that the session persists across login.

  2. Your browser is using super-strict cookie settings. Microsoft Edge appears to default to this mode on localhost and 127.0.0.1. There is a lock or shield icon in or near your address bar that lets you relax the restrictions on your cookie settings.

Try one or both of these and you should succeed.

I'm a beginner coder, so i'm pretty sure im just circumventing around the error. But replacing the website URL with http://localhost:8000/# and re running it somehow got around the error. maybe that could be of some use.

If you are running on chrome, rather than running application on http://127.0.0.1:8000 run it on http://localhost:8000, because chrome isn't saving the cookies when the ip address is being used.

Related