Google OAuth2 для Android с помощью python-social-auth

У меня есть проект Django, который реализует Google OAuth2 с помощью пакета python-social-auth. Он отлично работает для веб-приложений. Мой конфиг в settings.py:

SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = os.getenv('GOOGLE_OAUTH2_KEY')
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = os.getenv('GOOGLE_OAUTH2_SECRET')

AUTHENTICATION_BACKENDS = (
    'entity.oauth.backends.CustomGoogleOAuth2',
    'entity.oauth.backends.CustomFacebookOAuth2',
    'django.contrib.auth.backends.ModelBackend',
)

SOCIAL_AUTH_STRATEGY = 'entity.oauth.strategies.CustomStrategy'

После выполнения запроса я генерирую JWT-токены для пользователя:

def jwt_do_complete(backend, login, user=None, redirect_name='next',
                    *args, **kwargs):
    ...
    if user:
        if user_is_active(user):
            # Get the JWT payload for the user.
            refresh = TokenObtainPairSerializer.get_token(user)
            payload = {
                'refresh': str(refresh),
                'access': str(refresh.access_token)
            }
        else:
            raise RuntimeError('Unauthorized', 'The user account is disabled.', status.HTTP_401_UNAUTHORIZED)
    else:
        raise RuntimeError('Unauthorized', 'The user account does not exist.', status.HTTP_401_UNAUTHORIZED)

    return user, payload

Итак, рабочий процесс авторизации выглядит следующим образом:

  1. Frontend authenticates by Google form and receives code and other information.
  2. It sends it to the complete route of the Django server.
  3. Django verify the code with help of python-social-auth.
  4. Django sends back JWT tokens.

Вот полный вид:

@never_cache
@csrf_exempt
@psa(f'{NAMESPACE}:complete')
def complete_with_jwt(request, backend, *args, **kwargs):
    """Authentication complete view"""
    try:
        user, token_info = jwt_do_complete(
            request.backend,
            _do_login,
            user=request.user,
            redirect_name=REDIRECT_FIELD_NAME, request=request,
            *args, **kwargs
        )
    except RuntimeError as e:
        if len(e.args) != 3:
            raise
        return HttpResponse(json.dumps({
            'status': e.args[0],
            'message': e.args[1],
        }), status=e.args[2])
    return HttpResponse(json.dumps(token_info))

Все работает нормально, как я говорил ранее. Но когда дело доходит до приложений Android, возникает проблема: учетные данные Google для Android не имеют client_secret, только client_id.

Google credentials

Но до сих пор в документах Google об OAuth в Android фигурирует client_secret без объяснения того, как его получить. https://developers.google.com/identity/protocols/oauth2/native-app#exchange-authorization-code

Google Android OAuth2 docs

Итак, чтобы собрать это вместе: Как получить client_secret для Google Android OAuth2? Есть ли другой подход для Android приложений для выполнения аутентификации?

Вернуться на верх