The process for accessing Google API in Java on Android Studio through login involves the following steps:
Here is an example of how to access the Google Drive API in Java on Android Studio through login:
Add the following dependencies to your app's gradle file:
implementation 'com.google.android.gms:play-services-auth:18.1.0' implementation 'com.google.android.gms:play-services-drive:17.0.0'
Add the following permissions to your AndroidManifest.xml file:
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.GET_ACCOUNTS"/>
Implement the Google Sign-In API in your activity or fragment, using the following code:
private SignInButton mSignInButton; private GoogleSignInClient mGoogleSignInClient; private static final int RCSIGNIN = 9001;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
mSignInButton = findViewById(R.id.sign_in_button);
mSignInButton.setOnClickListener(view -> signIn());
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestScopes(new Scope(DriveScopes.DRIVE_FILE))
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
}
private void signIn() { Intent signInIntent = mGoogleSignInClient.getSignInIntent(); startActivityForResult(signInIntent, RCSIGNIN); }
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
}
}
private void handleSignInResult(Task<googlesigninaccount> completedTask) { try { GoogleSignInAccount account = completedTask.getResult(ApiException.class); // Signed in successfully, show authenticated UI. // Access Google Drive API using account.getIdToken() or account.getServerAuthCode() } catch (ApiException e) { // The ApiException status code indicates the detailed failure reason. Log.w(TAG, "signInResult:failed code=" + e.getStatusCode()); } }
Once the user is authenticated with Google, you can access the Google Drive API using the following code:
Drive googleDriveService = new Drive.Builder( AndroidHttp.newCompatibleTransport(), new GsonFactory(), mGoogleSignInClient.getSignInAccount().getCredentials(Drive.class)) .setApplicationName(getString(R.string.app_name)) .build();
FileList files = googleDriveService.files().list() .setFields("nextPageToken, files(id, name)") .setPageSize(10) .execute();
This code creates a new Drive service using the Google sign-in credentials, and lists the user's Google Drive files. You can modify this code to access other Google APIs as needed.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2021-11-23 11:00:00 +0000
Seen: 11 times
Last updated: Jul 14 '22
What are the typical approaches found in a Gradle build script?
What steps can be taken to increase the runtime of a debugger in NativeScript?
What is the method to show the dependencies in Java?
How can I incorporate BeanComparator into my gradle project?
What does the "Invalid source release 1.7" error mean when using gradle for compilation?