Search code examples
google-drive-android-apigoogle-signingooglesigninaccount

Android Google Sign-In


I need to enable server-side access to Google Drive. In this case a person is using his Android device. As far as I understood the steps are as follows: 1. Create GoogleSignInOptions 2. Using the GoogleSignInOptions create GoogleSignInAccount 3. Getting authCode from GoogleSignInAccount 4. Exchange the authCode for access/refresh/ID tokens

I am stuck on step 3. I followed the well-described tutorials without any success - https://developers.google.com/identity/sign-in/android/offline-access, https://developers.google.com/identity/sign-in/android/sign-in#configure_google_sign-in_and_the_googleapiclient_object

Here is the code that initialize sign-in process:

final GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestScopes(new Scope(Scopes.DRIVE_APPFOLDER))
                .requestServerAuthCode(backend_server_web_client_id)
                .build();

GoogleSignInClient google_api_client = GoogleSignIn.getClient(context, gso);

activity.startActivityForResult(google_api_client.getSignInIntent(), RC_SIGN_IN);

Here is the code that handles the sign-in result:

// data is the intent from onActivityResult callback
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);

if (task.isComplete())
    handle(task);
else {
    task.addOnCompleteListener(new OnCompleteListener<GoogleSignInAccount>() {
            @Override
            public void onComplete(@NonNull Task<GoogleSignInAccount> task) {
                handle(task);
            }}
    });
}

And finally here is the handle function where is the problem:

public void handle(Task<GoogleSignInAccount> task) {
    try {
        GoogleSignInAccount account = task.getResult(ApiException.class);
    } catch (ApiException e) {
    //I'm always getting this exception with status code 10, which means DEVELOPER ERROR. Keys in Google API console are checked multiple times.
    }
}

In handle function I'm always getting an exception with status code 10, which means DEVELOPER_ERROR. Keys in Google API console are checked multiple times. Code was rewritten few times.... I really have no idea what could be wrong.

Thank you :)


Solution

  • You might have forgotten to configure Google API Console. Follow the instructions: https://developers.google.com/identity/sign-in/android/start-integrating

    You see to create OAuth client ID for Android with corresponding package name and signing certificate's SHA1. You do NOT have to enter this key anywhere in the code. It just have to exist in Google API Console.