无需用户concent处理过期的访问令牌中的Andr​​oid令牌、用户、oid、concent

2023-09-05 04:49:26 作者:三寸海屿日光

我想使用新的Gmail API在测试Android应用程序。 后,与授权进度多对抗我们已经发现,具体地讲,该GoogleAuthUtil.getToken()方法不返回可用于访问的Gmail API特性的足够访问令牌(将试图做任何动作用时返回403错误该API)。 我还是设法得到足够的令牌说,唯一的办法就是做使用的WebView和认证过程(安装的应用程序 - >在API控制台的其他客户端ID)。 所以,以一种方式是一个双赢的局面,因为我终于可以使用的API,但剩下的问题是,当使用的访问令牌的API调用将过期或受到影响发生了什么? 有没有办法接受没有弹出了屏幕同意一个新的令牌?在所有的用户都已经批准该申请。

非常感谢。

编辑:这是你如何使用刷新标记获得新的令牌:由于@Noam

 的HttpClient =新DefaultHttpClient();
        httpPost =新HttpPost(地址);
        params.add(新BasicNameValuePair(refresh_token,令牌));
        params.add(新BasicNameValuePair(CLIENT_ID,CLIENT_ID));
        params.add(新BasicNameValuePair(client_secret,client_secret));
        params.add(新BasicNameValuePair(grant_type,refresh_token));
        httpPost.setHeader(内容类型,应用程序/ x-WWW的形式urlen codeD);
        httpPost.setEntity(新UrlEn codedFormEntity(PARAMS));
        HTT presponse HTT presponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = HTT presponse.getEntity();
        是= httpEntity.getContent();
 

解决方案

您可以使用GoogleAuthUtil.getToken(),你只需要添加的范围的 https://www.googleapis.com/auth/gmail.readonly 与builder.addScope()。

I am trying to use the new Gmail API in a test Android Application. After much battling with the authorization progress I have discovered that specifically, The GoogleAuthUtil.getToken() method does not return a sufficient access token that can be used to access the Gmail API features (will return a 403 error when trying to do any action with the API). That said, the only way I did manage to receive a sufficient token is by doing the authentication process using a WebView and a (Installed application -> Other client ID on the API Console). So in one way it's a win win situation cause I can finally use the API, but the question that remains is what happens when the access token used for the API calls will expire or be compromised? Is there a way to receive a new token without popping out a consent screen? After all the User have already approved the application.

Many thanks.

EDIT: here's how you get a new token using a refresh token: Thanks to @Noam

        httpClient = new DefaultHttpClient();
        httpPost = new HttpPost(address);
        params.add(new BasicNameValuePair("refresh_token", token));
        params.add(new BasicNameValuePair("client_id", client_id));
        params.add(new BasicNameValuePair("client_secret", client_secret));
        params.add(new BasicNameValuePair("grant_type", "refresh_token"));
        httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
        httpPost.setEntity(new UrlEncodedFormEntity(params));
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();

解决方案

you can use GoogleAuthUtil.getToken() you just need to add the scope "https://www.googleapis.com/auth/gmail.readonly" with builder.addScope().