目前,我的应用正在使用
我使用以下代码来执行GoogleApiClient连接.
public class GoogleApiClientFragment extends Fragment implements
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
public interface ConnectionCallbacks {
void onConnected(GoogleApiClient googleApiClient, int action);
void onCancel(int action);
}
public static GoogleApiClientFragment newInstance(String accountName, int action) {
GoogleApiClientFragment googleApiClientFragment = new GoogleApiClientFragment();
Bundle bundle = new Bundle();
bundle.putString(INTENT_EXTRA_ACCOUNT_NAME, accountName);
bundle.putInt(INTENT_EXTRA_ACTION, action);
googleApiClientFragment.setArguments(bundle);
return googleApiClientFragment;
}
/**
* Handles resolution callbacks.
*/
@Override
public void onActivityResult(int requestCode, int resultCode,
Intent data) {
super.onActivityResult(requestCode, resultCode, data); …Run Code Online (Sandbox Code Playgroud) 我连接GoogleApiClient以用于Google云端硬盘.我像这样构建客户端:
GoogleApiClient.Builder(this)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
Run Code Online (Sandbox Code Playgroud)
我的经验是,第一次为此客户端发出连接请求时,会显示AccountPicker对话框并显示Google云端硬盘的同意屏幕.如果用户选择一个帐户,同意并且连接成功完成,则AccountManager或某些相关功能会将所选帐户保存为默认帐户,并将驱动器范围的凭据(OAuth令牌?)保存.在后续连接请求中,为了方便用户,使用保存的值,并且用户没有看到用于帐户选择或同意的UI.
对于开发测试,我希望能够清除默认帐户和保存的凭据,以便我可以执行连接失败解决处理.我还没有办法做到这一点.我尝试了这个没有成功:
String driveScope = "https://www.googleapis.com/auth/drive.file";
String tokenType = "oauth2:" + driveScope;
AccountManager.get(this).invalidateAuthToken(
GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE, tokenType);
Run Code Online (Sandbox Code Playgroud)