GoogleApiClient.blockingConnect() 不适用于 GOOGLE_SIGN_IN_API
GoogleApiClient.blockingConnect() does not work with GOOGLE_SIGN_IN_API
我有一个 SyncAdapter
(代码在 UI 线程上没有 运行)写入 Google 驱动器上的应用程序文件夹。这在 GoogleApiClient
没有 Google 登录的情况下工作正常,但我正在尝试使用 Google 登录 GoogleApiClient
。
没有Google登录的工作代码:
mGoogleApiClient = new GoogleApiClient.Builder(context)
.setAccountName(accountName)
.addApi(Drive.API)
.addScope(Drive.SCOPE_APPFOLDER)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.blockingConnect();
现在我已经改用 Google 登录,它不再有效。以下代码是我正在尝试使用的代码:
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()
时出现以下错误而失败:
java.lang.IllegalStateException: Cannot use SIGN_IN_MODE_REQUIRED with GOOGLE_SIGN_IN_API. Use connect(SIGN_IN_MODE_OPTIONAL) instead.
有没有办法通过 Google 登录 API 执行 blockingConnect()
?
请注意,在 SyncAdapter
尝试上述代码之前,我在 UI 线程上执行了初始登录。
这是我让 API 从我的 SyncAdapter
工作的唯一方法 - 新的 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();
}
}
我有一个 SyncAdapter
(代码在 UI 线程上没有 运行)写入 Google 驱动器上的应用程序文件夹。这在 GoogleApiClient
没有 Google 登录的情况下工作正常,但我正在尝试使用 Google 登录 GoogleApiClient
。
没有Google登录的工作代码:
mGoogleApiClient = new GoogleApiClient.Builder(context)
.setAccountName(accountName)
.addApi(Drive.API)
.addScope(Drive.SCOPE_APPFOLDER)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.blockingConnect();
现在我已经改用 Google 登录,它不再有效。以下代码是我正在尝试使用的代码:
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()
时出现以下错误而失败:
java.lang.IllegalStateException: Cannot use SIGN_IN_MODE_REQUIRED with GOOGLE_SIGN_IN_API. Use connect(SIGN_IN_MODE_OPTIONAL) instead.
有没有办法通过 Google 登录 API 执行 blockingConnect()
?
请注意,在 SyncAdapter
尝试上述代码之前,我在 UI 线程上执行了初始登录。
这是我让 API 从我的 SyncAdapter
工作的唯一方法 - 新的 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();
}
}