Google Android Drive api sign in fails on installe

2019-05-10 06:16发布

问题:

I have developed an android application that uses the GoogleDrive api,

When under debug or running the debug version, the application works fine, and correctly authenticates to the attached google account..etc. When I build a release version, (with my signed keys), and install the apk file, when I run, the Googleapiclient fails to "connect", using the same google account that works under debug, giving me a "Sign in Failed" message, after it tries once to resolve.

回答1:

solution was here https://developers.google.com/drive/android/get-started

I need to create a seperate OAuth 2.0 client ID for the released version of my application, using the sha1 key from my release key.



回答2:

First, you need to start one intent to get the login credential

    if(mGoogleApiClient == null){
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Drive.API)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .enableAutoManage(this, this)
                .addScope(Drive.SCOPE_FILE)
                .addScope(Drive.SCOPE_APPFOLDER)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(AppIndex.API)
        .build();
    }
    Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
    startActivityForResult(signInIntent, DRIVE_SIGNIN_RESULT);

No need to connect the mGoogleApiClient here you can create GoogleSignInResult object from the intent data you have received in onActivityResult

    if (resultCode == RESULT_OK) {
        switch (requestCode) {
            case DRIVE_SIGNIN_RESULT:
                GoogleSignInResult signInResult = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
                handleSignInResult(signInResult);
                break;
        }
    }

Connect mGoogleApiClient now

    private void handleSignInResult(GoogleSignInResult result) {
        if (result.isSuccess()) {
            GoogleSignInAccount account = result.getSignInAccount();
            mGoogleApiClient.connect(GoogleApiClient.SIGN_IN_MODE_OPTIONAL);
        }
    }