Django-rest-framework-social-oauth2 error with google - django

I am using Django 1.11 with Django Rest Framework 3.6. I am trying to schedule a social network login from an Android application with the django-rest-framework-social-oauth2 plugin but I am having an error that is not solved.
Configuration setting.py
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'social_core.backends.open_id.OpenIdAuth',
'social_core.backends.google.GoogleOAuth2',
'social_core.backends.google.GoogleOAuth',
'social_core.backends.facebook.FacebookOAuth2',
'social_core.backends.facebook.FacebookAppOAuth2',
# 'social_core.backends.instagram.InstagramOAuth2',
'rest_framework_social_oauth2.backends.DjangoOAuth2',
'frontend.login.EmailOrUsernameModelBackend',
)
SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = ****
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = ***
REST_FRAMEWORK = {
...
'DEFAULT_AUTHENTICATION_CLASSES': (
...
'oauth2_provider.contrib.rest_framework.OAuth2Authentication',
'rest_framework_social_oauth2.authentication.SocialAuthentication',
)
}
The data configuration according to the documents.
Then from Postman I make the next call and it returns an error 400.
enter the description of the image here
The fact is that it returns the error:
The backend responded with HTTP403: the daily limit for
unauthenticated use was exceeded. Continuous use requires
registration.

Related

KeyCloack integration wirh DRF (Django Rest Framework)

I am a beginner level DRF developer. I am trying to integrate Keycloak with Django Rest Framework. Unfortunately, I was unable to find any type of help/blog/tutorial online.
You can use KeyCloack's Oauth2 API to authenticate and authorize your djagno users. Is is the same as implementing Sign-in with Google or any other provider.
My favorite package to implement social auth is python-social-auth, and it even has an existing backend for KeyCloack.
Here is how a configuration for Oauth2 against KeyCloack should look like:
First, setup social auth in your project like so
$ pip install social-auth-app-django
In your settings.py
INSTALLED_APPS = (
# ...
'social_django',
# ...
)
AUTHENTICATION_BACKENDS = (
'social_core.backends.keycloak.KeycloakOAuth2',
'django.contrib.auth.backends.ModelBackend',
)
# Add you connection settings here
SOCIAL_AUTH_KEYCLOAK_KEY = 'test-django-oidc'
SOCIAL_AUTH_KEYCLOAK_SECRET = 'a7a41-245e-...'
SOCIAL_AUTH_KEYCLOAK_PUBLIC_KEY = \
'MIIBIjANBxxxdSD'
SOCIAL_AUTH_KEYCLOAK_AUTHORIZATION_URL = \
'https://iam.example.com/auth/realms/voxcloud-staff/protocol/openid-connect/auth'
SOCIAL_AUTH_KEYCLOAK_ACCESS_TOKEN_URL = \
'https://iam.example.com/auth/realms/voxcloud-staff/protocol/openid-connect/token'
In your urls.py
urlpatterns = [
...
path('auth/', include('social_django.urls', namespace='social'))
...
]
Then add this to your login page template:
Login with KeyCloack

Django login with rest framework

While working with the rest framework and frontend native client, I need to log in with both native clients and directly to the API endpoint for development and testing purpose.
Native client login requires token authentication and direct API login requires session authentication. But if I put both in settings.py as default I get csrf error from the native client and if I remove session auth I am not able to login directly to API (I can still log in to the admin console).
My settings .py
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.TokenAuthentication',
'rest_framework.authentication.SessionAuthentication'
],
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.AllowAny',
)
}
What can be done to log in from both for development and testing? Any help?
#Daniel This is my current setting, check if this helps you -
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.TokenAuthentication',
# 'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
),
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.AllowAny',
)
}
AUTHENTICATION_BACKENDS = (
# Needed to login by username in Django admin, regardless of `allauth`
"django.contrib.auth.backends.ModelBackend",
# `allauth` specific authentication methods, such as login by e-mail
"allauth.account.auth_backends.AuthenticationBackend"
)

Django: How to use a custom social-auth backend?

I have overridden the GoogleOAuth2 class in django social-auth.
Currently the new class is in views.py.
But I cannot find how to add it to AUTHENTICATION_BACKENDS in settings.py
I have tried like this:
AUTHENTICATION_BACKENDS = (
'_auth.backends.YouTubeOAuth2', <---------- new backend
'social_core.backends.google.GoogleOAuth2',
'social_core.backends.twitter.TwitterOAuth',
'social_core.backends.facebook.FacebookOAuth2',
'django.contrib.auth.backends.ModelBackend',
)
And adjusted the template:
window.open('{% url 'social:begin' 'youtube' %}', '_blank', 'location=yes,height=570,width=520,scrollbars=yes,status=yes');
In the end of the Google auth process comes an error:
ModuleNotFoundError at /api/oauth/complete/google-oauth2/
No module named '_auth'

Django Rest Framework : Authentication credentials were not provided

Images in the current database have one piece of data.
But, I am currently experiencing the following error
"GET /images/all/ HTTP/1.1" 401 58"
"detail": "Authentication credentials were not provided."
My Git Hub URL : https://github.com/Nomadcoders-Study/Nomadgram
Which part of the setup went wrong?
I saw your Github project settings.py file.
This error is because you are using IsAuthenticated backend for all of your requests to Rest APIs. Also you setup jwt authorization system:
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
),
}
So basically, if you want to create a request to any of your API endpoints, you should provide jwt token authorization header in it. like this for:
curl "<your api endpoint>" -H "Authorization: jwt <token_received>"
Also remember to setup and API to receive token from it, by providing username and password in serializer.
try this in your settings file
settings.py
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.IsAuthenticated',),
'DEFAULT_AUTHENTICATION_CLASSES': ('rest_framework_simplejwt.authentication.JWTAuthentication',),
}
You can add it to your project Settings rest_framework configuration
settings.py
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES'('rest_framework.authentication.BasicAuthentication', ),
}

Django - after login using google, i tried to access a simple api view. gives Unauthorized error

my view
class ldata(APIView):
def get(self, request):
data = "my name"
return Response(data)
i can confirm, every time i login using google, it successfully register the user to auth_user & social_auth_usersocialauth table.
but i am getting this error when i access the view.
{
"detail": "Authentication credentials were not provided."
}
i have configured everything said here https://github.com/RealmTeam/django-rest-framework-social-oauth2
and added few code to settings for google login
AUTHENTICATION_BACKENDS = (
# for Google authentication
'social_core.backends.open_id.OpenIdAuth',
'social_core.backends.google.GoogleOpenId',
'social_core.backends.google.GoogleOAuth2',
'rest_framework_social_oauth2.backends.DjangoOAuth2',
'django.contrib.auth.backends.ModelBackend',
)
SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = "***"
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = "***"
my login call url: http://localhost:8000/auth/login/google-oauth2/
eidts:
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'oauth2_provider.contrib.rest_framework.OAuth2Authentication',
'rest_framework_social_oauth2.authentication.SocialAuthentication',
),
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
}