[英]java.lang.IllegalStateException: GoogleApiClient must be connected
[英]java.lang.IllegalStateException: Must not set scopes in GoogleApiClient.Builder when using Auth.GOOGLE_SIGN_IN_API
我正在將驅動器api與身份驗證api一起使用。 我需要先登錄到Google帳戶,然后將文件上傳到驅動器。 問題是當我僅使用未經身份驗證的驅動器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應用程序 ,該文檔還提供了如何處理用戶先前未授權該應用程序的信息。
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.