我正在使用带有身份验证 API 的驱动器 api。我需要先登录谷歌帐户,然后将文件上传到云端硬盘。问题是当我只使用没有身份验证的驱动器 api 时,说无法登录。我首先使用
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, 4);
它已成功登录。但是当我尝试使用此驱动器 api 时,它会出错。以下是代码
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, 4);
任何解决方案如何先登录然后将文件添加到驱动器?
试试这段代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstance);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
然后设置要进行授权的连接代码:
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
我正在遵循文档 - 授权 Android 应用程序,本文档还提供了如何处理用户以前未授权该应用程序的 ff 的给定说明。
希望这有帮助。