Hi. I’m trying to implement social authentication with using django-allauth. Every works fine when testing until the last part where I have chosen my google account and press continue and then this error:
JSONDecodeError at /api/v1/auth/google/callback/
Expecting value: line 1 column 1 (char 0)
Request Method: | GET |
---|---|
Request URL: | http://localhost:8000/api/v1/auth/google/callback/?code=4%2F0AVMBsJig0AR9vWKpHX8vr6j6PSyKzRcTcnSqZ0hSGE0lz6iCyTk7Hvzq7CxHhpvGrbNg3Q&scope=email+profile+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+openid&authuser=0&prompt=consent |
undefined | —: |
Django Version: | 5.2.5 |
undefined | —: |
Exception Type: | JSONDecodeError |
undefined | —: |
Exception Value: | |
undefined | —: |
Expecting value: line 1 column 1 (char 0)
Exception Location: | C:\Users\Boostan1999\.virtualenvs\SocialAuth-C1HobtWO\Lib\site-packages\requests\models.py, line 980, in json |
---|---|
Raised during: | accounts.views.GoogleLoginCallback |
undefined | —: |
Python Executable: | C:\Users\Boostan1999\.virtualenvs\SocialAuth-C1HobtWO\Scripts\python.exe |
undefined | —: |
Python Version: | 3.13.5 |
undefined | —: |
Python Path: | |
undefined | —: |
['F:\\SocialAuth',
'C:\\Users\\Boostan1999\\.virtualenvs\\SocialAuth-C1HobtWO\\Scripts\\python313.zip',
'C:\\Python313\\DLLs',
'C:\\Python313\\Lib',
'C:\\Python313',
'C:\\Users\\Boostan1999\\.virtualenvs\\SocialAuth-C1HobtWO',
'C:\\Users\\Boostan1999\\.virtualenvs\\SocialAuth-C1HobtWO\\Lib\\site-packages']
I read somewhere that this is due to incompatibility issue between dj-rest-auth and django-allauth versions so I downgraded to dj-rest-auth==7.0.0 and django-allauth==0.61.1 and also wrote custom client.
accounts/views.py:
```
class CustomGoogleOAuth2Client(OAuth2Client):
def \__init_\_(
self,
request,
consumer_key,
consumer_secret,
access_token_method,
access_token_url,
callback_url,
\_scope, # This is fix for incompatibility between django-allauth==65.3.1 and dj-rest-auth==7.0.1
scope_delimiter=" ",
headers=None,
basic_auth=False,
):
super().\__init_\_(
request,
consumer_key,
consumer_secret,
access_token_method,
access_token_url,
callback_url,
scope_delimiter,
headers,
basic_auth,
)
class GoogleLogin(SocialLoginView):
adapter_class = GoogleOAuth2Adapter
callback_url = settings.GOOGLE_OAUTH_CALLBACK_URL
client_class = CustomGoogleOAuth2Client
class GoogleLoginCallback(APIView):
def get(self, request, \*args, \*\*kwargs):
code = request.GET.get("code")
if code is None:
return Response(status=status.HTTP_400_BAD_REQUEST)
token_endpoint_url = urljoin("http://localhost:8000", reverse("google_login"))
response = requests.post(url=token_endpoint_url, data={"code": code})
return Response(response.json(), status=status.HTTP_200_OK)
class LoginPage(View):
def get(self, request, \*args, \*\*kwargs):
return render(
request,
'pages/login.html', {
'google_callback_uri': settings.GOOGLE_OAUTH_CALLBACK_URL,
"google_client_id": settings.GOOGLE_OAUTH_CLIENT_ID,
},
)
config/urls.py:
urlpatterns = [
path("admin/", admin.site.urls),
path("login/", LoginPage.as_view(), name="login"),
path("api/v1/auth/", include("dj_rest_auth.urls")),
re_path(r"^api/v1/auth/accounts/", include("allauth.urls")),
path("api/v1/auth/registration/", include("dj_rest_auth.registration.urls")),
path("api/v1/auth/google/", GoogleLogin.as_view(), name="google_login"),
path(
"api/v1/auth/google/callback/",
GoogleLoginCallback.as_view(),
name="google_login_callback",
),
]
```
any help is appreciated.