Checking new emails from Gmail with the Gmail API and OAuth2 on Android

I'm really stumped. I was trying to figure out how to get to my Gmail inbox from an Android app with an existing OAuth2 token that I got from Google earlier. I can verify myself with a token:

URL url = new URL("https://www.googleapis.com/oauth2/v1/userinfo?access_token=" +token);
HttpURLConnection con = (HttpURLConnection) url.openConnection();

      

But it seems to me that I am going to solve this problem from the wrong angle. I tried to decrypt the Gmail API but got nothing. Maybe someone pushed me in the right direction here?

Or, if the code I posted is correct, how would I proceed from this?

EDIT: So I figured out how to use the Gmail API to get the last 100 emails with this code:

HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
GoogleTokenResponse response = new GoogleTokenResponse();
response.setAccessToken(myExistingToken);
GoogleCredential credential = new GoogleCredential().setFromTokenResponse(response);
Gmail service = new Gmail.Builder(httpTransport, jsonFactory, credential).setApplicationName(myApplicationName).build();
ListThreadsResponse threadsResponse = service.users().threads().list("voxcommunis@gmail.com").execute();
List<com.google.api.services.gmail.model.Thread> threads = threadsResponse.getThreads();
for ( com.google.api.services.gmail.model.Thread thread : threads ) {
    Log.d(LOG_TAG, "Thread ID: "+ thread.getId());
}

      

So, I'll continue this path to get new emails: =)

+3


source to share


1 answer


Check out: Handling Expired Access Token in Android Without User Interaction and Accessing the Gmail API with Android



They just had an answer to a similar question on how to get oauth2 to work on android (for any google API).

+1


source







All Articles