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
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.
The project source code must be customized with your Google credentials.
-
Open
/path/to/gdrive/src/org/jboss/aerogear/android/example/gdrive/Constants.java
for editing. -
Enter the
client id
,client secret
andredirect 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
$ cd /path/to/gdrive/
$ ./gradlew clean build
To deploy, run and debug the application on an Android device attached to your system, on the command line enter the following:
-
Install generated apk to device
$ cd /path/to/gdrive $ ./gradlew installDebug
-
Open app on device
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();
}
});