Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The process for accessing Google API in Java on Android Studio through login involves the following steps:

  1. Create a new Google API Console project and enable the API you want to use.
  2. Create an OAuth 2.0 client ID in the credentials section of the API Console.
  3. Add the Google Play services SDK to your Android Studio project and include the necessary permissions in your AndroidManifest.xml file.
  4. Add the relevant client ID and scopes to your app, and handle the authentication flow with the Google Sign-In API.
  5. Access the Google API using the appropriate Google Play services API class for the API you want to use.

Here is an example of how to access the Google Drive API in Java on Android Studio through login:

  1. Create a new Google API Console project and enable the Google Drive API.
  2. Create an OAuth 2.0 client ID in the credentials section of the API Console, and add your Android package name and SHA-1 fingerprint to the client ID.
  3. 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'

  4. Add the following permissions to your AndroidManifest.xml file:

    <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.GET_ACCOUNTS"/>

  5. 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()); } }

  6. 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.