好的,我的Android应用需要访问用户的Google云端硬盘帐户并修改该帐户的电子表格。我正在使用Google表格API 3.0版来编辑电子表格和Drive API Client Library for Java以便驱动器访问。
现在我知道用户需要授予我的应用访问其信息的权限。现在请纠正我,但这就是我理解这一点的方法。
onActivityResult
方法上检查结果以查看用户是否授予了权限。由于我使用两个API(驱动器和电子表格),因此用户必须同时授予这两个API,因此我在下面的代码执行以下操作。
onActivityResult
方法上选择get accountName,并将其设置为将要使用的帐户。onActivityResult
方法中,我在将抛出UserRecoverableAuthIOException
的单独线程(Asyn Task)上进行调用,然后使用此异常来提示用户获得Drive API的许可。onActivityResult
方法中,我在与最后一个类似的单独线程上进行另一次调用,该线程会抛出UserRecoverableAuthException
来授权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;
}
}
}
答案 0 :(得分:0)
当您致电GoogleAuthUtil.getToken时,您可以提供您要授权的所有范围的空格分隔列表。只需包含驱动器和工作表范围。其他一些地方在技术上仍然可以抛出一个auth异常,但如果你先这样做,它就不应该引起提示。