Android的getAuthToken返回一个空令牌

Android的getAuthToken返回一个空令牌

本文介绍了Android的getAuthToken返回一个空令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗯,我需要授权的用户谷歌日历的访问,首款谷歌标识工作正常,当我使用

Well i need to authorize Google Calendar's access for a user, first google Id works fine when i use

blockingGetAuthToken

和它得到一个道理,我通常会登录此令牌。所以,当我试图用其他账户我得到一个空令牌。我搜索了很多,发现,使用 getAuthToken 是pferred,因为它$ P $使用从活动的上下文调用它..然后我转换的全过程,以使用

and it gets a token, i usually log on this token.So when i tried to use other accounts i got a null token.I searched a lot and found out that using getAuthToken is preferred as it uses a context from the activity calling it.. then i converted the whole process to use it

 private static final String AUTH_TOKEN_TYPE = "oauth2:https://www.googleapis.com/auth/calendar";

public static String authorize(AndroidtestActivity parent, Account account) {
    AccountManager accountManager = AccountManager.get(parent);
    Bundle options= new Bundle();

    Log.d("MyAPP", "Get Authorization");
    try {
        AccountManagerFuture<Bundle> acc=accountManager.getAuthToken ( account, AUTH_TOKEN_TYPE,  options, true, null, null);
        Bundle authTokenBundle = acc.getResult();
        String authToken = authTokenBundle.get(AccountManager.KEY_AUTHTOKEN).toString();

        Log.d("MyAPP","Token= "+authToken);
        return authToken;


    } catch (Exception ex) {
        Logger.getLogger(GoogleAuthorize.class.getName()).log(Level.SEVERE,
                null, ex);
    }
    return null;
}

}

但仍然没有账户可以得到有效的标记,他们都得到一个空一个

but still no accounts could get a valid token, they all get a null one

然后我看到这个答案 http://stackoverflow.com/a/2021337/1280902 并随后用 invalidateAuthToken

then i saw this answer http://stackoverflow.com/a/2021337/1280902 and followed using invalidateAuthToken

private static final String AUTH_TOKEN_TYPE = "oauth2:https://www.googleapis.com/auth/calendar";

public static String authorize(AndroidtestActivity parent, Account account) {
    AccountManager accountManager = AccountManager.get(parent);
    Bundle options= new Bundle();

    Log.d("MyAPP", "Get Authorization");
    try {
        AccountManagerFuture<Bundle> acc=accountManager.getAuthToken ( account, AUTH_TOKEN_TYPE,  options, true, null, null);
        Bundle authTokenBundle = acc.getResult();
        String authToken = authTokenBundle.get(AccountManager.KEY_AUTHTOKEN).toString();
        accountManager.invalidateAuthToken("com.google",authToken);
        acc=accountManager.getAuthToken ( account, AUTH_TOKEN_TYPE,  options, true, null, null);
        authTokenBundle = acc.getResult();
        authToken = authTokenBundle.get(AccountManager.KEY_AUTHTOKEN).toString();

        Log.d("MyAPP","Token= "+authToken);
        return authToken;


    } catch (Exception ex) {
        Logger.getLogger(GoogleAuthorize.class.getName()).log(Level.SEVERE,
                null, ex);
    }
    return null;
}

}

但我对每个帐户我用的是同样的问题,甚至曾经工作在年初与 blockingGetAuthToken

but i had the same problem on every account i use, even the one that used to work at the beginning with blockingGetAuthToken

所以,我失去了一些东西?

So am i missing something?

推荐答案

确定它工作正常,当我使用

Ok it works fine when i use

getAuthToken (Account account, String authTokenType, Bundle options, Activity activity, AccountManagerCallback<Bundle> callback, Handler handler)

活动参数解决了这个问题。

The activity parameter solved the problem..

这篇关于Android的getAuthToken返回一个空令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 02:00