我有一个ObjectId
(代码无法在UI线程上运行)写入Google云端硬盘上的应用文件夹。对于没有Google登录的SyncAdapter
,此功能正常,但我尝试使用Google登录进入GoogleApiClient
。
没有Google登录的工作代码:
GoogleApiClient
现在我已经开始使用Google登录,它已不再有效。以下代码是我尝试使用的:
mGoogleApiClient = new GoogleApiClient.Builder(context)
.setAccountName(accountName)
.addApi(Drive.API)
.addScope(Drive.SCOPE_APPFOLDER)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.blockingConnect();
我的程序在调用 GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestScopes(new Scope(Scopes.DRIVE_APPFOLDER))
.build();
mGoogleApiClient = new GoogleApiClient.Builder(mApplication)
.addApi(Drive.API)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.blockingConnect();
时出现以下错误:
blockingConnect()
有没有办法使用Google登录API执行java.lang.IllegalStateException: Cannot use SIGN_IN_MODE_REQUIRED with GOOGLE_SIGN_IN_API. Use connect(SIGN_IN_MODE_OPTIONAL) instead.
?
请注意,我会在blockingConnect()
尝试上述代码之前在UI线程上执行初始登录。
答案 0 :(得分:0)
这是我从SyncAdapter
开始使用API的唯一方法 - 新API拒绝使用blockingConnect()
:
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestScopes(new Scope(Scopes.DRIVE_APPFOLDER))
.build();
mGoogleApiClient = new GoogleApiClient.Builder(mApplication)
.addApi(Drive.API)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
long start = System.currentTimeMillis();
mGoogleApiClient.connect(GoogleApiClient.SIGN_IN_MODE_OPTIONAL);
while (mGoogleApiClient.isConnecting() && System.currentTimeMillis() - start < 5000) {
try {
Thread.sleep(250, 0);
} catch (InterruptedException e) {
}
}
if (mGoogleApiClient.isConnected()) {
try {
// Do stuff with Google Drive.
} finally {
mGoogleApiClient.disconnect();
}
}