我正在使用新的API处理Google Tasks App,但我无法获得授权。我正在使用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工作正常。
答案 0 :(得分:2)
我发现了问题:我使用的是authTokenType goanna_mobile
,我在另一个应用中找到了它。即使AccountManager要求我允许我的应用访问Google Tasks,也是错误的authTokenType。
Google日历的正确值为cl
。