AccountManager 似乎返回错误的 authToken
我正在使用新 API 开发 Google 任务应用程序,但无法获得工作授权。我正在使用 Android AccountManager 来获取 authToken,奇怪的是我收到一条错误,指出我使用了错误的凭据:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "invalid",
"message": "Invalid Credentials",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Invalid Credentials"
}
}
我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
transport = AndroidHttp.newCompatibleTransport();
transport.defaultHeaders = new GoogleHeaders();
gotAccount(false);
}
private void authenticated(String authToken) {
((GoogleHeaders) transport.defaultHeaders).setGoogleLogin(authToken);
Tasks service = new Tasks(transport, new GsonFactory());
try {
TaskLists taskLists = service.tasklists.list().execute();
} catch (Exception e) {
handleException(e);
}
}
private void gotAccount(final AccountManager manager, final Account account) {
new Thread() {
@Override
public void run() {
try {
final Bundle bundle = manager.getAuthToken(account, AUTH_TOKEN_TYPE, true,
null, null).getResult();
runOnUiThread(new Runnable() {
@Override
public void run() {
if (bundle.containsKey(AccountManager.KEY_INTENT)) {
Intent intent = bundle.getParcelable(AccountManager.KEY_INTENT);
int flags = intent.getFlags();
flags &= ~Intent.FLAG_ACTIVITY_NEW_TASK;
intent.setFlags(flags);
startActivityForResult(intent, REQUEST_AUTHENTICATE);
} else if (bundle.containsKey(AccountManager.KEY_AUTHTOKEN)) {
authenticated(bundle.getString(AccountManager.KEY_AUTHTOKEN));
}
}
});
} catch (Exception e) {
handleException(e);
}
};
}.start();
}
private void gotAccount(boolean tokenExpired) {
AccountManager manager = AccountManager.get(this);
Account account = manager.getAccountsByType("com.google")[0];
if (tokenExpired) {
manager.invalidateAuthToken("com.google", mAuthToken);
}
gotAccount(manager, account);
}
我尝试使用 HttpRequestInitializer
并应用使用 HttpExecuteInterceptor
中的 authToken 而不是使用(已弃用的)HttpTransport.defaultHeaders
,但这也不起作用。
new Tasks(transport, initializer, new GsonFactory());
我做错了什么?我的凭据显然是正确的,GMail 工作正常。
I'm working on a Google Tasks App with the new API, but I can't get the authorization to work. I'm using the Android AccountManager to get an authToken, the weird thing is I'm getting an error stating I'm using wrong credentials:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "invalid",
"message": "Invalid Credentials",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Invalid Credentials"
}
}
My code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
transport = AndroidHttp.newCompatibleTransport();
transport.defaultHeaders = new GoogleHeaders();
gotAccount(false);
}
private void authenticated(String authToken) {
((GoogleHeaders) transport.defaultHeaders).setGoogleLogin(authToken);
Tasks service = new Tasks(transport, new GsonFactory());
try {
TaskLists taskLists = service.tasklists.list().execute();
} catch (Exception e) {
handleException(e);
}
}
private void gotAccount(final AccountManager manager, final Account account) {
new Thread() {
@Override
public void run() {
try {
final Bundle bundle = manager.getAuthToken(account, AUTH_TOKEN_TYPE, true,
null, null).getResult();
runOnUiThread(new Runnable() {
@Override
public void run() {
if (bundle.containsKey(AccountManager.KEY_INTENT)) {
Intent intent = bundle.getParcelable(AccountManager.KEY_INTENT);
int flags = intent.getFlags();
flags &= ~Intent.FLAG_ACTIVITY_NEW_TASK;
intent.setFlags(flags);
startActivityForResult(intent, REQUEST_AUTHENTICATE);
} else if (bundle.containsKey(AccountManager.KEY_AUTHTOKEN)) {
authenticated(bundle.getString(AccountManager.KEY_AUTHTOKEN));
}
}
});
} catch (Exception e) {
handleException(e);
}
};
}.start();
}
private void gotAccount(boolean tokenExpired) {
AccountManager manager = AccountManager.get(this);
Account account = manager.getAccountsByType("com.google")[0];
if (tokenExpired) {
manager.invalidateAuthToken("com.google", mAuthToken);
}
gotAccount(manager, account);
}
I've tried to use a HttpRequestInitializer
and apply the authToken in a HttpExecuteInterceptor
instead of using the (deprecated) HttpTransport.defaultHeaders
but that doesn't work either.
new Tasks(transport, initializer, new GsonFactory());
What am I doing wrong? My credentials are obviously correct, GMail is working fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现了问题:我正在使用 authTokenType
goanna_mobile
,这是我在另一个应用程序中找到的。尽管 AccountManager 要求我允许我的应用程序访问 Google Tasks,但它的 authTokenType 是错误的。对于 Google 日历,正确的是
cl
。I found the problem: I was using the authTokenType
goanna_mobile
, which I found in another app. Even though the AccountManager asks me to allow my app to access Google Tasks, it is the wrong authTokenType.The correct one is
cl
for Google Calendar.