![](/img/trans.png)
[英]How do I use the Google drive API key to access drive content from android application?
[英]How do I switch accounts under the NEW Google Drive Android API
我在新版Google雲端硬盤Android API中的授權流程如下:
奇跡般有效。 現在重復以切換帳戶為目標:
在這里,我沒有機會進入AccountSelectDialog,因為我從未使用'result'來獲取onConnectionFailed()來調用startResolutionForResult() 。 這次我錯過了什么?
首先,添加Plus.API:
mGoogleApiClient = new GoogleApiClient.Builder(this).addApi(Drive.API).addApi(Plus.API).addScope(Drive.SCOPE_APPFOLDER).addConnectionCallbacks(this).addOnConnectionFailedListener(this).build();
然后你可以切換這樣的帳戶:
public void onClick(View view) {
if (view.getId() == R.id.sign_out_button) {
if (mGoogleApiClient.isConnected()) {
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
mGoogleApiClient.disconnect();
mGoogleApiClient.connect();
}
}
}
有關更多信息,請參閱此處
打電話吧
mGoogleApiClient.clearDefaultAccountAndReconnect();
看看文檔 。
這將調用onConnectionFailed
回調,該回調將顯示可在可用Google帳戶中進行選擇的布局:
@Override
public void onConnectionFailed(ConnectionResult connectionResult)
{
if (connectionResult.hasResolution()) {
try {
connectionResult.startResolutionForResult(this, RESOLVE_CONNECTION_REQUEST_CODE);
} catch (IntentSender.SendIntentException e) {
// Unable to resolve, message user appropriately
}
} else {
GooglePlayServicesUtil.getErrorDialog(connectionResult.getErrorCode(), this, 0).show();
}
}
通過打開兩個關於基本相同主題的SO問題,我意識到自己弄得一團糟。 所以,現在是鞏固答案的好時機。 我在GDAA中搜索直接getter / setter方法,但只發現'setter' - setAccountName()) - 所以問題21583828 (實際上沒有,但Burcu幫助了我)。
另一方面,可以通過從“onActivityResult()”獲取帳戶名來替換“getter” - 所以問題21501829
關於同一主題的另一個SO問題 - 這個問題也得到了解決。
所以結論是:
更新2014-11-04:
這是一個包裝器,用於在我的應用中保留和管理Google帳戶。
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import com.google.android.gms.auth.GoogleAuthUtil;
public class GooAccMgr {
private static final String ACC_NAME = "account_name";
public static final int FAIL = -1;
public static final int UNCHANGED = 0;
public static final int CHANGED = +1;
private String mCurrEmail = null; // cache locally
public Account[] getAllAccnts(Context ctx) {
return AccountManager.get(acx(ctx)).getAccountsByType(GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE);
}
public Account getPrimaryAccnt(Context ctx) {
Account[] accts = getAllAccnts(ctx);
return accts == null || accts.length == 0 ? null : accts[0];
}
public Account getActiveAccnt(Context ctx) {
return email2Accnt(ctx, getActiveEmail(ctx));
}
public String getActiveEmail(Context ctx) {
if (mCurrEmail != null) {
return mCurrEmail;
}
mCurrEmail = ctx == null ? null : pfs(ctx).getString(ACC_NAME, null);
return mCurrEmail;
}
public Account email2Accnt(Context ctx, String emil) {
if (emil != null) {
Account[] accounts =
AccountManager.get(acx(ctx)).getAccountsByType(GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE);
for (Account account : accounts) {
if (emil.equalsIgnoreCase(account.name)) {
return account;
}
}
}
return null;
}
/**
* Stores a new email in persistent app storage, reporting result
* @param ctx activity context
* @param newEmail new email, optionally null
* @return FAIL, CHANGED or UNCHANGED (based on the following table)
* OLD NEW SAVED RESULT
* ERROR FAIL
* null null null FAIL
* null new new CHANGED
* old null old UNCHANGED
* old != new new CHANGED
* old == new new UNCHANGED
*/
public int setEmail(Context ctx, String newEmail) {
int result = FAIL; // 0 0
String prevEmail = getActiveEmail(ctx);
if ((prevEmail == null) && (newEmail != null)) {
result = CHANGED;
} else if ((prevEmail != null) && (newEmail == null)) {
result = UNCHANGED;
} else if ((prevEmail != null) && (newEmail != null)) {
result = prevEmail.equalsIgnoreCase(newEmail) ? UNCHANGED : CHANGED;
}
if (result == CHANGED) {
mCurrEmail = newEmail;
pfs(ctx).edit().putString(ACC_NAME, newEmail).apply();
}
return result;
}
private Context acx(Context ctx) {
return ctx == null ? null : ctx.getApplicationContext();
}
private SharedPreferences pfs(Context ctx) {
return ctx == null ? null : PreferenceManager.getDefaultSharedPreferences(acx(ctx));
}
}
為亞歷克斯洛克伍德提供最初的靈感。 不幸的是,我找不到他原始代碼的參考。
聽起來您依賴於默認帳戶選擇。 在此設置中,系統會提示用戶選擇一個帳戶,並記住此狀態。
如果您想在應用中提供帳戶切換功能,則需要從自己的應用啟動帳戶選擇器,並提供實例化GoogleApiClient時選擇的帳戶名稱。
您可以在共享首選項中保留最后選擇的帳戶名,以便在下次用戶切換帳戶之前記住它。
如果您使用GoogleApiClient,只需調用mGoogleApiClient.clearDefaultAccountAndReconnect()
。
如果您將DriveClient與GoogleSignInAccount(驅動程序庫16.0.0)一起使用,請嘗試此操作。
// try connect Drive
fun startSignIn() {
val requiredScopes = HashSet<Scope>()
requiredScopes.add(Drive.SCOPE_FILE)
requiredScopes.add(Drive.SCOPE_APPFOLDER)
val account = GoogleSignIn.getLastSignedInAccount(this)
if (account != null && account.grantedScopes.containsAll(requiredScopes)) {
// TODO: Get DriveClient and DriveResourceClient
} else {
val option = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestScopes(Drive.SCOPE_FILE, Drive.SCOPE_APPFOLDER)
.build()
val client = GoogleSignIn.getClient(this, option)
startActivityForResult(client.signInIntent, REQUEST_CODE_SIGN_IN)
}
}
// try change account
fun changeAccount() {
val option = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.build()
val client = GoogleSignIn.getClient(activity, option)
client.signOut()
.addOnSuccessListener {
Log.d(TAG, "signOut success")
// Try again sign-in
startSignIn()
}
.addOnFailureListener {
Log.e(TAG, "signOut failed $it")
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.