Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Latest commit

 

History

History

GDrive

Mobile app showing the AeroGear Authz feature on Android

Author: Daniel Passos (dpassos) & Summers Pittman (supittma)
Level: Beginner
Technologies: Java, Android
Summary: A basic example of OAuth2 : Login and retrieve data.
Source: https://github.com/aerogear/aerogear-android-cookbook/tree/master/GDrive

What is it?

GDrive project demonstrates how to include Authz/OAuth2 functionality in Android applications.

This simple project consists of a ready-to-build Android application. Before building the application, you must create an OAuth2 credential in Google's console. The credentials must then be inserted into the application source code. After this is complete, the application can be built and deployed to Android devices.

When the application is deployed to an Android device, the application will ask for your Google credentials, ask permission to access your account and retrieve your documents.

How do I run it?

System Requirements

Configure

The project source code must be customized with your Google credentials.

  1. Open /path/to/gdrive/src/org/jboss/aerogear/android/example/gdrive/Constants.java for editing.

  2. Enter the client id, client secret and redirect url for the following constants:

    String AUTHZ_CLIENT_ID = "";
    String AUTHZ_CLIENT_SECRET = "";
    String AUTHZ_REDIRECT_URL = "";

For the complete instructions about how to setup Google credentials, visit our OAuth2 documentation guide

Build Application

$ cd /path/to/gdrive/
$ ./gradlew clean build

Running the app

To deploy, run and debug the application on an Android device attached to your system, on the command line enter the following:

  1. Install generated apk to device

    $ cd /path/to/gdrive
    $ ./gradlew installDebug
  2. Open app on device

How does it work?

MainActivity is invoked. The Activity life cycle onStart is called first invoking the authz method — attempting to display a Google login screen.

authzModule = AuthorizationManager.config("GoogleDriveAuthz", OAuth2AuthorizationConfiguration.class)
        .setBaseURL(new URL(AUTHZ_URL))
        .setAuthzEndpoint(AUTHZ_ENDPOINT)
        .setAccessTokenEndpoint(AUTHZ_TOKEN_ENDPOINT)
        .setAccountId(AUTHZ_ACCOUNT_ID)
        .setClientId(AUTHZ_CLIENT_ID)
        .setClientSecret(AUTHZ_CLIENT_SECRET)
        .setRedirectURL(AUTHZ_REDIRECT_URL)
        .setScopes(Arrays.asList("https://www.googleapis.com/auth/drive"))
        .setAdditionalAuthorizationParams(ImmutableSet.of(Pair.create("access_type", "offline")))
        .asModule();

authzModule.requestAccess(this, new Callback<String>() {
    @Override
    public void onSuccess(String o) {
        Log.d("TOKEN ++ ", o);
        retriveFiles();
    }

    @Override
    public void onFailure(Exception e) {
        Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
    }
});