为什么AccountAuthenticator#getAuthToken()不叫? [英] Why is AccountAuthenticator#getAuthToken() not called?
问题描述
我通过扩展<一创建了自己的Android帐户身份验证href="http://developer.android.com/reference/android/accounts/AbstractAccountAuthenticator.html"><$c$c>AbstractAccountAuthenticator$c$c>贯彻<一href="http://developer.android.com/reference/android/accounts/AbstractAccountAuthenticator.html#addAccount%28android.accounts.AccountAuthenticatorResponse,%20java.lang.String,%20java.lang.String,%20java.lang.String%5B%5D,%20android.os.Bundle%29"><$c$c>addAccount()$c$c>和<一href="http://developer.android.com/reference/android/accounts/AbstractAccountAuthenticator.html#getAuthToken%28android.accounts.AccountAuthenticatorResponse,%20android.accounts.Account,%20java.lang.String,%20android.os.Bundle%29"><$c$c>getAuthToken()$c$c>.一些在它的方法是由<被叫href="http://developer.android.com/reference/android/accounts/AccountManager.html"><$c$c>AccountManager$c$c>,但其他人都没有。
这个伟大的工程:
<一个href="http://developer.android.com/reference/android/accounts/AccountManager.html#addAccount%28java.lang.String,%20java.lang.String,%20java.lang.String%5B%5D,%20android.os.Bundle,%20android.app.Activity,%20android.accounts.AccountManagerCallback%3Candroid.os.Bundle%3E,%20android.os.Handler%29"><$c$c>AccountManager#addAccount()$c$c>
的AccountManager的AccountManager = AccountManager.get(活动);
accountManager.addAccount(MyAccountAuthenticator.ACCOUNT_TYPE,
MyAccountAuthenticator.AUTHTOKEN_TYPE_FULL_ACCESS,NULL,NULL,
活动中,回调,空);
当我称之为<一的问题发生href="http://developer.android.com/reference/android/accounts/AccountManager.html#getAuthToken%28android.accounts.Account,%20java.lang.String,%20android.os.Bundle,%20android.app.Activity,%20android.accounts.AccountManagerCallback%3Candroid.os.Bundle%3E,%20android.os.Handler%29"><$c$c>AccountManager#getAuthToken()$c$c>在我的活动
。该的AccountManager不会调用 getAuthToken()
方法,我在 AccountAuthenticator
定义。它要求一些其他的默认方法只检查是否存在的的authToken
开始之前 AuthenticatorActivity
。
这是行不通的。它不叫我的 getAuthToken()
方法:
<一个href="http://developer.android.com/reference/android/accounts/AccountManager.html#getAuthToken%28android.accounts.Account,%20java.lang.String,%20android.os.Bundle,%20android.app.Activity,%20android.accounts.AccountManagerCallback%3Candroid.os.Bundle%3E,%20android.os.Handler%29"><$c$c>AccountManager#getAuthToken()$c$c>
的AccountManager的AccountManager = AccountManager.get(活动);
accountManager.getAuthToken(
mAccount,MyAccountAuthenticator.AUTHTOKEN_TYPE_FULL_ACCESS,空,
活动中,回调,处理程序);
AuthenticatorService
我创造了我的服务,并定义 onBind()
。 addAccount()
不应该工作,否则。
公开的IBinder onBind(意向意图){
返回intent.getAction()。等于(ACTION_AUTHENTICATOR_INTENT)?新MyAccountAuthenticator(本).getIBinder():空;
}
编辑:我称之为 addAccountExplicitly
在 MyAuthenticatorActivity
应用程序后,得到一个身份验证令牌回给用户。
从类摘录 MyAuthenticatorActivity延伸AccountAuthenticatorActivity
:
如果(getIntent()。getBooleanExtra(KEY_IS_ADDING_NEW_ACCOUNT,FALSE)){
//创建设备上的帐户,并设置我们:收到的身份验证令牌
accountManager.addAccountExplicitly(帐户,NULL,NULL);
}
您的评论茅塞顿开极大 - 如果你设置了身份验证令牌的帐户,那么你的 getAuthToken
方法将不会被调用,直到令牌无效。你通常通过调用<一href="http://developer.android.com/reference/android/accounts/AccountManager.html#invalidateAuthToken%28java.lang.String,%20java.lang.String%29">invalidateAuthToken当接收到401或403或你有你的Web服务。
从的Javadoc getAuthToken
方法:
如果pviously生成身份验证令牌一个$ P $是缓存此帐户和类型,然后返回。否则,如果一个保存的密码是可用的,它被发送到服务器,以生成一个新的身份验证令牌。否则,系统将提示用户输入密码。
由于您的令牌在缓存中,则直接返回,你的身份验证是没有咨询。
I created my own Android account authenticator by extending AbstractAccountAuthenticator
and implementing addAccount()
and getAuthToken()
. Some of the methods in it are called by AccountManager
, but others are not.
This works great:
AccountManager accountManager = AccountManager.get(activity);
accountManager.addAccount(MyAccountAuthenticator.ACCOUNT_TYPE,
MyAccountAuthenticator.AUTHTOKEN_TYPE_FULL_ACCESS, null, null,
activity, callback, null);
The problem happens when I call AccountManager#getAuthToken()
in my Activity
. The AccountManager does not call the getAuthToken()
method I define in my AccountAuthenticator
. It calls some other default method that only checks for existence of an authToken
before starting the AuthenticatorActivity
.
This does not work. It does not call my getAuthToken()
method:
AccountManager accountManager = AccountManager.get(activity);
accountManager.getAuthToken(
mAccount, MyAccountAuthenticator.AUTHTOKEN_TYPE_FULL_ACCESS, null,
activity, callback, handler);
AuthenticatorService
I created my service and defined onBind()
. addAccount()
should not work otherwise.
public IBinder onBind(Intent intent) {
return intent.getAction().equals(ACTION_AUTHENTICATOR_INTENT) ? new MyAccountAuthenticator(this).getIBinder() : null;
}
EDIT: I call addAccountExplicitly
in MyAuthenticatorActivity
after the app gets an auth token back for the user.
Snippet from class MyAuthenticatorActivity extends AccountAuthenticatorActivity
:
if (getIntent().getBooleanExtra(KEY_IS_ADDING_NEW_ACCOUNT, false)) {
// Creating the account on the device and setting the auth token we recieved
accountManager.addAccountExplicitly(account, null, null);
}
Your comment cleared things up immensely -- if you set the auth token for the account, then your getAuthToken
method will not be called until the token is invalidated. You generally do this by calling invalidateAuthToken upon receiving a 401 or 403 or what have you from the web service.
From the Javadoc for the getAuthToken
methods:
If a previously generated auth token is cached for this account and type, then it is returned. Otherwise, if a saved password is available, it is sent to the server to generate a new auth token. Otherwise, the user is prompted to enter a password.
Since your token is in the cache, it is returned directly and your authenticator is not consulted.
这篇关于为什么AccountAuthenticator#getAuthToken()不叫?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!