【发布时间】:2015-02-17 02:38:21
【问题描述】:
好的,所以我的 Android 应用需要访问用户的 Google 云端硬盘帐户并在该帐户上编辑电子表格。我正在使用 Google Sheets API 3.0 版编辑电子表格和用于 Java 的 Drive API 客户端库以进行驱动器访问。
现在我知道用户需要授予我的应用程序访问其信息的权限。现在请纠正我,但这就是我理解的方式。
- 启动活动供用户选择帐户
- 在
onActivityResult方法上检查结果以查看用户是否授予权限。
现在由于我使用两个 API(驱动器和电子表格),用户必须授予两者的权限,所以我下面的代码执行以下操作。
- 启动活动供用户选择帐户
- 在
onActivityResult方法中选择 accountName,并将其设置为将要使用的帐户。 - 在
onActivityResult方法中,在我设置要使用的 accountName 之后,我在一个单独的线程(Asyn 任务)上进行调用,该线程将抛出UserRecoverableAuthIOException,然后使用此异常提示用户许可Drive API。 - 在
onActivityResult方法中,作为对用户授予权限的响应,我在与上一个类似的单独线程上再次调用,这将引发UserRecoverableAuthException以授权Sheets API。
基本上,用户选择他们想要使用的帐户,然后授予 Drive API 权限,然后授予 Sheets API 权限。 我的问题:这是应该如何授予权限?他们是一种在一个提示中同时授予 Drive 和 Sheets API 权限的方法吗?
公共类 PlannerActivity 扩展 ActionBarActivity {
ListPlannerFragment mListFragment;
DetailPlannerFragment mDetailFragment;
YIGDataController mController;
private Drive mDrive;
private SpreadsheetService mSpreadsheetService;
private GoogleAccountCredential mGoogleAccountCredential;
private static final String TAG = "PlannerActivity";
protected static final int CHOOSE_ACCOUNT_REQUEST_CODE = 1;
protected static final int AUTHORIZE_DRIVE_ACCESS_REQUEST_CODE = 2;
protected static final int AUTHORIZE_SHEETS_ACCESS_REQUEST_CODE = 3;
//Life Cycle
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Google
verifyGoogleAPIUse();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(TAG,"onActivity result called: result code-" + resultCode + ", resultCode-" + resultCode);
switch(requestCode) {
case CHOOSE_ACCOUNT_REQUEST_CODE:
Log.d(TAG,"Result of CHOOSE_ACCOUNT:");
if(resultCode == Activity.RESULT_OK) {
Log.d(TAG, "CHOOSE_ACCOUNT successful:");
String accountName = data.getExtras().getString(AccountManager.KEY_ACCOUNT_NAME);
mGoogleAccountCredential.setSelectedAccountName(accountName);
mDrive = new Drive.Builder(AndroidHttp.newCompatibleTransport(),new GsonFactory(), mGoogleAccountCredential).setApplicationName("YIG Manager").build();
AuthTryDrive authTryDrive = new AuthTryDrive();
authTryDrive.execute();
} else {
Log.d(TAG, "CHOOSE_ACCOUNT failure:");
finish();
}
break;
case AUTHORIZE_DRIVE_ACCESS_REQUEST_CODE:
Log.d(TAG,"Result of AUTHORIZE_DRIVE:");
if(resultCode == Activity.RESULT_OK) {
Log.d(TAG, "AUTHORIZE_DRIVE_ACCESS success:");
AuthTrySheets authTrySheets = new AuthTrySheets();
authTrySheets.execute();
} else {
Log.d(TAG, "AUTHORIZE_DRIVE_ACCESS failed, asking to choose new account:");
finish();
}
break;
case AUTHORIZE_SHEETS_ACCESS_REQUEST_CODE:
Log.d(TAG,"Result of AUTHORIZE_SHEETS:");
if(resultCode == Activity.RESULT_OK) {
Log.d(TAG, "AUTHORIZE_SHEETS_ACCESS success:");
} else {
Log.d(TAG, "AUTHORIZE_SHEETS_ACCESS failed, asking to choose new account:");
finish();
}
break;
}
}
//Util
private void verifyGoogleAPIUse(){
mGoogleAccountCredential =GoogleAccountCredential.usingOAuth2(this, Collections.singleton(DriveScopes.DRIVE));
Log.d(TAG,"Starting activity to choose account.");
startActivityForResult(mGoogleAccountCredential.newChooseAccountIntent(),CHOOSE_ACCOUNT_REQUEST_CODE);
}
private class AuthTryDrive extends AsyncTask<Void,Void,Void>{
@Override
protected Void doInBackground(Void... params) {
//Drive
try {
Log.d(TAG, "In try statement of authorizing drive access");
FileList f = mDrive.files().list().execute();
f.toString();
} catch (UserRecoverableAuthIOException e) {
Log.d(TAG, "Drive user recoverable error");
startActivityForResult(e.getIntent(),AUTHORIZE_DRIVE_ACCESS_REQUEST_CODE);
} catch (IOException e) {
Log.d(TAG,"io excpetion in drive");
}
return null;
}
}
private class AuthTrySheets extends AsyncTask<Void,Void,Void>{
@Override
protected Void doInBackground(Void... params) {
//SpreadSheets
try {
Log.d(TAG,"In try statement of authorizing sheets access");
mSpreadsheetService = new SpreadsheetService("YIG Manager");
mSpreadsheetService.setAuthSubToken(GoogleAuthUtil.getToken(PlannerActivity.this, mGoogleAccountCredential.getSelectedAccountName(), "oauth2:https://spreadsheets.google.com/feeds https://docs.google.com/feeds"));
URL SPREADSHEET_FEED_URL = new URL(
"https://spreadsheets.google.com/feeds/spreadsheets/private/full");
SpreadsheetFeed spreadsheetFeed = mSpreadsheetService.getFeed(SPREADSHEET_FEED_URL,SpreadsheetFeed.class);
List<SpreadsheetEntry> spreadsheetEntries = spreadsheetFeed.getEntries();
} catch (UserRecoverableAuthException e) {
Log.d(TAG,"Sheets user recoverable error");
startActivityForResult(e.getIntent(),AUTHORIZE_SHEETS_ACCESS_REQUEST_CODE);
} catch (GoogleAuthException e) {
Log.d(TAG,"GoogleAuthException");
} catch (IOException e) {
Log.d(TAG,"IOException");
} catch (ServiceException e) {
Log.d(TAG,"ServiceException" + e.toString());
}
return null;
}
}
}
【问题讨论】:
标签: java android google-drive-api google-sheets