嗨,我知道我回答这个答案为时已晚,但我相信其他人会从中获得帮助,您可以执行以下操作:
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);
}
获取此实例后,请遵循此处的代码片段:Google SpreadSheets Api。不要忘记在 Google Developer 的控制台注册项目并启用 Drive 和 Google Plus Api,生成 Client 和 Api 密钥,设置同意屏幕。
对于 android studio,可以通过 build.gradle 在 dependencies 部分添加以下库:
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'
最后是非常重要的 AndroidManifest 文件:
<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" />