Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Sign-in in Android with django-rest-auth

I've been trying to add Google Sign-In in Android but have a couple of doubts. From the Android documentation Integrate google sign in android In the server side authentication part Client Id is required which is OAuth 2.0 web application client ID for your backend server.

From android's documentation:

Get your backend server's OAuth 2.0 client ID If your app authenticates with a backend server or accesses Google APIs from your backend server, you must get the OAuth 2.0 client ID that was created for your server. To find the OAuth 2.0 client ID

From my understanding the flow would be:

  • Android app will get the auth code from google which will be passed to the backend.
  • The backend will get the access token with the auth code from the android app and the client secret.
  • With the acess token we get the user's information and the access token is saved in the database.

My doubts are:

  1. I read somewhere on StackOverflow that we need to create two OAuth client one for Android and one for Web Application. Is this True?
  2. Django Rest Auth Login View need to have one redirect_url defined but I don't understand what would be the redirect_uri in case of Android device or we need to pass this URL while getting the auth code from Google.
  3. On OAuth Playground I put my backend's client id and client secret and got the auth code and when I passed this auth code to my login view I was getting the redirect_uri_mismatch but If I put redirect_url = 'developer.google.com' It works, I guess the auth code contains host information from where it is generated that's why this should be the same as redirect_url in my rest-auth view but then for android what it should be?

Here is my Google Login View.

class GoogleLogin(SocialLoginView):
    adapter_class = GoogleOAuth2Adapter
    client_class = OAuth2Client
    callback_url = 'localhost:8000' # What this should be?

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

I am using this django-rest-auth

Some helpful link -

  • https://github.com/Tivix/django-rest-auth/issues/262#issuecomment-256562095 # It says callback URL could be a fake one but I get redirect_uri_mismatch
like image 979
Mohit Solanki Avatar asked Oct 16 '22 08:10

Mohit Solanki


1 Answers

So Finally, I figured it out, Answering my own question so someone might find this helpful.

  1. Yes, you need two client id one for your Android device and one for your web application.
  2. Just add http://localhost:8000/accounts/google/login/callback/ as callback_url in the GoogleLoginView and put the same in your Google developer console.
  3. I don't know exactly if the auth code generated by the Android contains any host information or not but it seems as long as the callback URL you added in the login view class and in google developer console is the same it will work.

Your Google sign in view should look like this.

class GoogleLogin(SocialLoginView):
    authentication_classes = (JSONWebTokenAuthentication,)
    adapter_class = GoogleOAuth2Adapter
    callback_url = 'http://localhost:8000/accounts/google/login/callback/'
    client_class = OAuth2Client

Note: You only need callback_url and client_class in case where you are passing the auth code to this view but if in you are passing the access_token then callback_url and client_class is not necessary.

like image 85
Mohit Solanki Avatar answered Nov 15 '22 09:11

Mohit Solanki