Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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://learn.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!

like image 951
albertrw Avatar asked Jan 24 '23 10:01

albertrw


1 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.

like image 129
newz2000 Avatar answered Feb 03 '23 08:02

newz2000