AccountManager 似乎返回错误的 authToken

发布于 2024-11-07 02:18:03 字数 2903 浏览 4 评论 0原文

我正在使用新 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

往事随风而去 2024-11-14 02:18:03

我发现了问题:我正在使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文