4

I want to implement the same scenario for use in Android Kotlin which is given in this url.

  1. For Web Application I have created login with google for web application by follow this link. Here is my Google Login View in views.py for access token (as one user have explained here )
class GoogleLogin(SocialLoginView):
    adapter_class = GoogleOAuth2Adapter

And It's working for me as I expected.

  1. For Android Application Now, Somehow I have managed a code for this google scenario. Here is my Google Client login View.view.py code
class GoogleClientView(APIView):
    def post(self, request):
        token = {'id_token': request.data.get('id_token')}
        print(token)

        try:
            # Specify the CLIENT_ID of the app that accesses the backend:
            idinfo = id_token.verify_oauth2_token(token['id_token'], requests.Request(), CLIENT_ID)
            print(idinfo)

            if idinfo['iss'] not in ['accounts.google.com', 'https://accounts.google.com']:
                raise ValueError('Wrong issuer.')

            return Response(idinfo)
        except ValueError as err:
            # Invalid token
            print(err)
            content = {'message': 'Invalid token'}
            return Response(content)

When I am requesting POST method with IdToken then this is providing below information and ofcourse we need this.

{
 // These six fields are included in all Google ID Tokens.
 "iss": "https://accounts.google.com",
 "sub": "110169484474386276334",
 "azp": "1008719970978-hb24n2dstb40o45d4feuo2ukqmcc6381.apps.googleusercontent.com",
 "aud": "1008719970978-hb24n2dstb40o45d4feuo2ukqmcc6381.apps.googleusercontent.com",
 "iat": "1433978353",
 "exp": "1433981953",

 // These seven fields are only included when the user has granted the "profile" and
 // "email" OAuth scopes to the application.
 "email": "testuser@gmail.com",
 "email_verified": "true",
 "name" : "Test User",
 "picture": "https://lh4.googleusercontent.com/-kYgzyAWpZzJ/ABCDEFGHI/AAAJKLMNOP/tIXL9Ir44LE/s99-c/photo.jpg",
 "given_name": "Test",
 "family_name": "User",
 "locale": "en"
}

Till here, everything is working good for me.But here I want to create account/login for a the user using above info and when account/login is created then need a key in return response. below What I had tried

class GoogleClientView(APIView):
    permission_classes = [AllowAny]
    adapter_class = GoogleOAuth2Adapter
    callback_url = 'https://example.com/user/accounts/google/login/callback/'
    client_class = OAuth2Client

    def post(self, request):
        token = {'id_token': request.data.get('id_token')}
        print(token)

        try:
            # Specify the CLIENT_ID of the app that accesses the backend:
            idinfo = id_token.verify_oauth2_token(token['id_token'], requests.Request(), CLIENT_ID)
            print(idinfo)

            if idinfo['iss'] not in ['accounts.google.com', 'https://accounts.google.com']:
                raise ValueError('Wrong issuer.')

            return Response(idinfo)
        except ValueError as err:
            # Invalid token
            print(err)
            content = {'message': 'Invalid token'}
            return Response(content)

Again It seems it's not creating any account/login for user and no key is getting in response. So, for creating account/login, How can I implement the code?

Please ask for more information If I forgot to put any.

Inception_K
  • 135
  • 1
  • 8

0 Answers0