Android: Google Drive onActivityResult() getting resultCode 0 (RESULT_CANCELED)

北战南征 提交于 2020-06-16 12:05:18

问题


I've been scouring the Google documentation and sample code and Stack Overflow for a solution to this, and while I've seen a number of people running into the same symptoms I haven't found anything that helps in my particular case.

Basically I'm trying to do an auth workflow for the Google Drive REST API, and while I get as far as the Google account selection page, when it returns to my calling activity, I'm always seeing a result of RESULT_CANCELED (i.e., 0) instead of RESULT_OK (i.e., -1), no matter what account is selected.

I've checked to make sure that the credentials are set up correctly on the Google developer console — and they were working previously with authentication via the now-defunct CloudRail library. (As far as I can tell, now with Google Sign-In you don't provide any client id, etc.: that's all handled on Google's end when it matches your package name.)

Here is my call for authentication:

    GoogleSignInOptions signInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestScopes(new Scope(DriveScopes.DRIVE))
            .build();
    mSignInClient = GoogleSignIn.getClient(activity, signInOptions);

And here is the activity's onActivityResult():

@Override
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
    switch (requestCode) {
        case REQUEST_GOOGLE_SIGN_IN:
            if (resultCode == Activity.RESULT_OK && resultData != null) {
                /*** NEVER GET HERE ***/
                handleSignInResult(this, resultData);
            }
            else {
                /*** ALWAYS GET HERE ***/
                Log.e(TAG, String.format("Unable to complete Google sign-in (resultCode: %d)", resultCode));
            }
        }
        break;
    }
    super.onActivityResult(requestCode, resultCode, resultData);
}

I'm assuming that this is a case of me doing something obviously dumb and wrong — and was hoping that just typing out the question would help me figure out what it is, but that hasn't happened yet.


回答1:


As I expected, it was something relatively simple that I was doing wrong. So for anyone having the same issue and stepping over my body while searching for a solution, here's what I found:

While I did have the credentials in the Google developer console set up correctly, with my package name and SHA1 generated, as directed by the documentation, from mykeystore.keystore, what I missed was that Android Studio doesn't sign debug builds with mykeystore.keystore by default. Instead it uses its own provided debug.keystore.

So the thing to do is to create a new OAuth client ID with your package name and the SHA1 from Android Studio's debug.keystore instead of one's own. (You can either hunt down debug.keystore on your system, or run the Gradle "signingReport" task and get it from there.)

Hope that helps someone.




回答2:


First of all most probably it all happens because you add the credential.json from the Google Drive API console. I suggest you to create a project in Firebase console and add its google.json for validation of authentication.

and in your onActivityResult you can check the error code using following code:

  @Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    if (callbackManager != null) {
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }
    super.onActivityResult(requestCode, resultCode, data);
    // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
    if (requestCode == RC_SIGN_IN) {
        Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
        try {
            // Google Sign In was successful, authenticate with Firebase
            GoogleSignInAccount account = task.getResult(ApiException.class);
            handleSignInResult(account);
        } catch (ApiException e) {
            // Google Sign In failed, update UI appropriately
             System.out.println("errorCode = " + e.getStatusCode());// Print this error code on your console 
        }
    }

}

if the error code is 12500 then your problem with keys validation and hence you need to create a firebase project and add your machine SHA1 code and then use its google.json file in your app root folder.



来源:https://stackoverflow.com/questions/56350913/android-google-drive-onactivityresult-getting-resultcode-0-result-canceled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!