Retrieve account name with the NEW Google Drive API

后端 未结 3 1364
清歌不尽
清歌不尽 2020-12-20 07:05

I set up the authorization process for Google Play Services as described under https://developers.google.com/drive/android/auth

Now that the user has authorized the

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-20 07:47

    1. Check if user signed or not

      GoogleSignInClient mGoogleSignInClient;
          private void setupAuthorization() {
              GoogleSignInOptions signInOptions =
                      new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                              .requestEmail() // add it if you want to user email 
                              .requestScopes(Drive.SCOPE_FILE)
                              .build();
              mGoogleSignInClient = GoogleSignIn.getClient(this, signInOptions);
              mGoogleSignInClient.silentSignIn().addOnSuccessListener(googleSignInAccount -> {googleSignInAccount.getEmail()}).addOnFailureListener(e -> {});
      }
      
    2. If you want to get email and name after signing in

      @Override protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case REQUEST_CODE_DRIVE_SIGN_IN: Log.i(TAG, "Sign in request code"); // Called after user is signed in. if (resultCode == RESULT_OK) { Log.i(TAG, "Signed in successfully."); // Use the last signed in account here since it already have a Drive scope. GoogleSignInAccount googleSignInAccount = GoogleSignIn.getLastSignedInAccount(this); googleSignInAccount.getEmail(); mDriveClient = Drive.getDriveClient(this, GoogleSignIn.getLastSignedInAccount(this)); // Build a drive resource client. mDriveResourceClient = Drive.getDriveResourceClient(this, GoogleSignIn.getLastSignedInAccount(this)); } break;}}

    The details could be found here

提交回复
热议问题