Reputation: 21
I am developing an Android application that communicates with a Google spreadsheet in my personal account. Using Android Google API level 10, and the following libs:
android-support-v4.jar;
gdata-client-1.0.jar;
gdata-client-meta-1.0.jar;
gdata-core-1.0.jar;
gdata-spreadsheet-3.0.jar;
gdata-spreadsheet-meta-3.0.jar;
google-api-client-1.12.0-beta.jar;
google-api-client-android-1.12.0-beta.jar;
google-http-client-1.12.0-beta.jar;
google-http-client-android-1.12.0-beta.jar;
google-oauth-client-1.12.0-beta.jar;
gson-2.1.jar;
guava-13.0.1.jar;
jackson-core-asl-1.9.9.jar;
jsr305-1.3.9.jar;
protobuf-java-2.4.1.jar.
To send/receive data to/from the Google spreadsheet (using Google Gdata Spreadsheet 3.0 API) I need an object SpreadsheetService that is returned in the authentication phase using the following code:
/**
* Authentication to the account
* @return
* @throws AuthenticationException
*/
public static SpreadsheetService authenticate(final String username, final String password) throws AuthenticationException {
SpreadsheetService service = new SpreadsheetService("v1");
service.setProtocolVersion(SpreadsheetService.Versions.V3);
service.setUserCredentials(email, password);
return service;
}
This procedure, however, requires a username and password. I need to do a Google authentication without explicitly pass username and password.
The Android environment provides an Account object, drawing from account data which is stored on the device.
How can I, using Account object, do the authentication and obtain the SpreadsheetService object? Thank's a lot.
Upvotes: 2
Views: 803
Reputation: 1210
Hi I know I am too late for this answer but I am sure somebody else is going to get help from this, you can do the following:
private void chooseAccount() {
Intent intent = AccountManager.newChooseAccountIntent(null, null,
new String[] { "com.google" }, false, null, null, null, null);
startActivityForResult(intent, ACCOUNT_CODE);
}
String accessToken= "";
String accountName = "";
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == AUTHORIZATION_CODE) {
requestToken();
} else if (requestCode == ACCOUNT_CODE) {
accountName = data
.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
// invalidate old tokens which might be cached. we want a fresh
// one, which is guaranteed to work. Invalidate token based on your needs and the way your code is arranged.
invalidateToken();
requestToken();
}
}
}
private void invalidateToken() {
AccountManager accountManager = AccountManager.get(this);
if(accessToken!= null && !accessToken.equals(""){
accountManager.invalidateAuthToken("com.google",
accessToken);
}
}
private void requestToken() {
String SCOPE1 = "https://spreadsheets.google.com/feeds";
String SCOPE2 = "https://docs.google.com/feeds";
Account userAccount = null;
for (Account account : accountManager.getAccountsByType("com.google")) {
if (account.name.equals(accountName)) {
userAccount = account;
break;
}
}
accountManager.getAuthToken(userAccount, "oauth2:" + SCOPE1 + " " + SCOPE2, null, this,
new OnTokenAcquired(), null);
}
private class OnTokenAcquired implements AccountManagerCallback<Bundle> {
@Override
public void run(AccountManagerFuture<Bundle> result) {
try {
Bundle bundle = result.getResult();
Intent launch = (Intent) bundle.get(AccountManager.KEY_INTENT);
if (launch != null) {
startActivityForResult(launch, AUTHORIZATION_CODE);
} else {
accessToken= bundle
.getString(AccountManager.KEY_AUTHTOKEN);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
private void getSpreadSheetServiceInstance(){
spreadsheetService = new SpreadsheetService(applicationName);
spreadsheetService.setProtocolVersion(SpreadsheetService.Versions.V3);
spreadsheetService.setAuthSubToken(accessToken);
}
After getting this instance, follow the code snippets here: Google SpreadSheets Api. Don't forget to register project at Google Developer's console and enable Drive and Google Plus Api, generate Client and Api keys, set up consent screens.
For android studio, following libs can be added via build.gradle under dependencies section:
compile 'com.google.gdata:core:1.47.1'
compile 'com.google.android.gms:play-services-drive:7.5.0'
compile 'com.google.android.gms:play-services-plus:7.5.0'
And at last very important AndroidManifest file:
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
<!-- To retrieve the account name (email) as part of sign-in: -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<!-- To access Google+ Apis -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<!-- To retrieve OAuth 2.0 tokens or invalidate tokens to disconnect a user. -->
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<!-- To read Google play services -->
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
Upvotes: 1