Google帐户获取令牌

sma*_*133 7 android google-api-client

我有一个kitkat api的问题,同时tringy获取谷歌帐户服务的访问令牌,在我的情况下谷歌音乐.因此,如果用户首先使用next方法尝试获取令牌:

 public String getAuthToken(Account account)
            throws AuthenticatorException, IOException {
        String s1;
        if (account == null) {
            Log.e("MusicAuthInfo", "Given null account to MusicAuthInfo.getAuthToken()", new Throwable());
            throw new AuthenticatorException("Given null account to MusicAuthInfo.getAuthToken()");
        }
        String s = getAuthTokenType(mContext);
        try {
            s1 = AccountManager.get(mContext).blockingGetAuthToken(account, s, true);
        } catch (OperationCanceledException operationcanceledexception) {
            throw new AuthenticatorException(operationcanceledexception);
        }
        if (s1 == null) {
            throw new AuthenticatorException("Received null auth token.");
        }

        return s1;
    }
Run Code Online (Sandbox Code Playgroud)

在这里,我得到s1 == null和系统推送通知:

通知

当用户点击通知时,会出现下一个对话框:

对话

当用户单击"确定"时,获取令牌的所有下一次迭代都会成功.

问题:如何绕过此确认或仅显示对话框,而不点击通知?

小智 4

这不是您问题的直接答案,但您可以使用 Google Play 服务。

String token = GoogleAuthUtil.getToken(context, userEmail, "oauth2:https://mail.google.com/");
Run Code Online (Sandbox Code Playgroud)

您只需指定所需的 oauth2 范围。例如,对于 Google+,您需要“ https://www.googleapis.com/auth/plus.login ”,而不是我在 Gmail 代码段中发布的内容。您还可以在一个令牌请求中指定多个范围。立即弹出权限请求。

您可以在这里阅读所有相关内容:向 Google 授权 REST API登录范围