'Tweeter Oauth:You are authenticated as XXX, but are not authorized to access this page. Would you like to login to a different account?

I have created just a simple django app to login using tweeter, but when i try to login from my app I get this message "You are authenticated as xxx, but are not authorized to access this page. Would you like to login to a different account?"

Screenshot showing message

I have tried to login with account I used to create the app and different account, but the result is the same. What could be the solution to this?

I am using django and django-allauth library



Solution 1:[1]

To access the Django admin, the user needs is_staff=True. If you log into the admin with a superuser, you can edit your testmyap user and set the flag.

If you have not yet created a superuser, you can create one with:

python manage.py createsuperuser

Solution 2:[2]

On the terminal type:

$ python manage.py shell
$ from django.contrib.auth.models import User
$ u = User.objects.get(username='testmyapp')
$ u.is_superuser = True
$ u.save()
$ quit()
$ python manage.py runserver

Solution 3:[3]

If using a custom user model (which the Django docs recommend), Django will throw an error: "AttributeError: Manager isn't available...", which is the problem @elizandro-sparcbr had in 2021.

The steps are a bit different than the answer by @Paolo. When I ran into the "Manager isn't available" problem, the steps below worked for me in Feb 2022, on Django 3.2.

settings.py

ACCOUNT_EMAIL_VERIFICATION = 'none'

(Or if using cookiecutter, then in settings folder, modify base.py as above.)

In the terminal:

$ python manage.py shell
$ from django.contrib.auth import get_user_model
$ User = get_user_model()
$ u = User.objects.get(username='testmyap')
$ u.is_staff = True  # required to access admin panel
$ u.is_superuser = True  # optional
$ u.save()
$ quit()

The key steps that are different are the 2nd and 3rd lines above.

For more background on why this is required and what these steps are doing, see:

Solution 4:[4]

Check that the user has the is_staff attribute set to 1

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alasdair
Solution 2 Paolo
Solution 3 Karl Hill
Solution 4 Ihebuzo Chris