android oauth2 无法获得AuthToken


最近在做一个需要用到Gmail api的App,其中用到了oauth2,在获得AuthToken的时候,得到的AuthToken总是个null,也就是说获得不了AuthToken。我看抛出了IOException,初步猜测是不是因为功夫网拦截了Google的API,所以IOException,所以导致了获得不了AuthToken? (因为得到的AuthToken是在一个回调里完成的,我加了断点也不停,所以也没法看出来具体是哪里出的问题,只能根据异常猜测一下),各位有没有开发过相关的项目的,遇到过这个问题吗,该如何解决呢,是否是功夫网的原因呢,要是的话我就放弃了,不折腾了。

下面是代码片段:


 class AuthTokenTask extends AsyncTask<String, String, String> {

    String token = null;

    @Override
    protected String doInBackground(String... params) {

        AccountManager accountManager = AccountManager.get(getApplicationContext());
        Account me = accountManager.getAccountsByType("com.google")[0];

        accountManager.getAuthToken(me, "oauth2:https://mail.google.com/", null, true, new AccountManagerCallback<Bundle>() {

            @Override
            public void run(AccountManagerFuture<Bundle> future) {
                try {
                    //15, TimeUnit.SECONDS
                    token = future.getResult().getString(AccountManager.KEY_AUTHTOKEN);
                    Log.i(AuthTokenTask.class.getName(), token);
                } catch (OperationCanceledException e) {
                    e.printStackTrace();
                } catch (AuthenticatorException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }, null);

        return token;
    }

    @Override
    protected void onPostExecute(String result) {
        mailProperties.oauthToken = result;
        super.onPostExecute(result);
    }

}

LogCat截图:

请输入图片描述

Android oauth2 gmail-api

xzy50 10 years, 4 months ago

Your Answer