Author: Daniel Passos (dpassos) & Summers Pittman (supittma)
Level: Beginner
Technologies: Java, Android
Summary: A basic example of OAuth2 : Login and retrieve data.
Target Product: -
Product Versions: -
Source: https://github.com/aerogear/aerogear-android-cookbook/tree/master/GDrive
The AGDroid 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.
- Java 7
- Gradle 2.1
- Latest Android SDK and Platform version 19
First, you must authenticate with Google. You will need a Google Account for this. Now open the Google Console.
If you haven't created a project, click "Create Project" and give the new project a name:
Now you need to enable the Drive API. To do that, navigate to APIs & auth > APIs and scroll down to the Drive API item, which you need to turn on:
Now you need create new credentials to access your Drive accounts from the app. Go to APIs & auth > Credentials and inside OAuth area click the blue Create new Client ID
button. This launches the following popup, where you select Installed application
and Other
:
On the last screen finally get to see the actual value of the generated client id
, secret id
and redirect uris
, which you will use later:
You also edit the 'Consent screen'. Go to API > Consent screen and edit the project informations.
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 = "";
-
Save the file.
-
Build the application
$ cd /path/to/gdrive/
$ gradle clean build
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
$ gradle installDebug
2. Open app on device
Application output is displayed in the command line window.
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();
}
});