Here is a sample class that you can use to connect to Google Drive through Android in your application using Android Account Manager and OAuth2. You can easily build out this class to add methods for uploading files and creating folders on your Drive.
You will first need to create your application in the Google API Console and enable the Google Drive SDK & API. You will also need to obtain a Simple API Access key which you can find on the API Access tab within the console.
You will also need to include the Google Drive API in your project.
import java.io.IOException;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AuthenticatorException;
import android.accounts.OperationCanceledException;
import android.content.Context;
import android.util.Log;
import com.google.api.client.extensions.android2.AndroidHttp;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.json.JsonHttpRequest;
import com.google.api.client.http.json.JsonHttpRequestInitializer;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.DriveRequest;
public class GoogleDrive {
private static final String TAG = GoogleDrive.class.getSimpleName();
private static final JsonFactory JSON_FACTORY = new JacksonFactory();
private static final HttpTransport HTTP_TRANSPORT = AndroidHttp.newCompatibleTransport();
private static final GoogleCredential googleCredential = new GoogleCredential();
private static final String API_KEY = "your_api_key";
private static final String APP_NAME = "your_app_name";
private final AccountManager accountManager;
private Context context;
public GoogleDrive(Context context) {
this.context = context;
accountManager = AccountManager.get(context);
Drive drive = buildDriveService("accountname@gmail.com", "the_current_auth_token");
}
public Drive buildDriveService(String accountName, String authToken) {
// confirm the account you are using still exists on the device
if (doesGoogleAccountExistOnDevice(accountName) == false) {
return null;
}
try {
// always invalidate the auth token first
accountManager.invalidateAuthToken("com.google", authToken);
String token = AccountManager.get(context).blockingGetAuthToken(
getGoogleAccountByName(accountName),
"oauth2:https://www.googleapis.com/auth/drive",
true);
// Insert some code here to save the token to whichever
// data storage scheme you wish to use such as
// sharedpreferences
// setup the credentials with the access token
googleCredential.setAccessToken(token);
// Build the drive service
return new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, googleCredential)
.setApplicationName(APP_NAME)
.setJsonHttpRequestInitializer(new JsonHttpRequestInitializer() {
@Override
public void initialize(JsonHttpRequest request) throws IOException {
DriveRequest driveRequest = (DriveRequest) request;
driveRequest.setKey(API_KEY);
}
}).build();
} catch (OperationCanceledException e) {
e.printStackTrace();
return null;
} catch (AuthenticatorException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
if (e instanceof GoogleJsonResponseException) {
Log.d(TAG, "Code: " + ((GoogleJsonResponseException) e).getDetails().getCode() +
" Message: " + ((GoogleJsonResponseException) e).getDetails().getMessage());
} else {
e.printStackTrace();
}
return null;
}
}
private boolean doesGoogleAccountExistOnDevice(String accountName) {
for (Account account : accountManager.getAccountsByType("com.google")) {
if (accountName.equals(account.name)) {
return true;
}
}
return false;
}
private Account getGoogleAccountByName(String accountName) {
if (accountName == null) return null;
for (Account account : accountManager.getAccountsByType("com.google")) {
if (accountName.equals(account.name)) {
return account;
}
}
return null;
}
}