Gmail API access using Android

后端 未结 4 671
谎友^
谎友^ 2021-01-02 02:20

I am trying to access the Gmail API using an Android application. I have successfully requested and received an access token using all available scope combinations. But it s

4条回答
  •  一整个雨季
    2021-01-02 02:54

    Well here's the deal. So far Tokens given by GoogleAuthUtil && AccountManager do not work with the Gmail API. Don't know if it's a Google thing or not but my solution is this:

    Create a Client Id (Applicaion->Other) in the API console. Use the Client Id & Client secret to obtain an Access Token through a WebView. Save the result Refresh Token. Now whenever you want to refresh the Access Token use this request:

        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();
    

    Works for me. Hope That in the future a valid token will be provided with the GoogleAuthUtil.getToken() option.

提交回复
热议问题