简体   繁体   中英

Check for new emails from Gmail with Gmail API and OAuth2 in Android

I'm really at a dead end. I've been trying to figure out how to reach my Gmail-inbox from an Android-app with an existing OAuth2-token that I have received from Google moments before. I can auth myself with a token with this:

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

But it feels like I'm going at this problem at a wrong angle, somehow. I've been trying to decipher Gmail's API but haven't gotten anywhere. Can somebody push me in the right direction here?

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

EDIT: So I've figured out how to use Gmail's API to get the latest 100 mails 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 will continue on this path to get new emails some how =)

Check out: Handling expired access token in Android without user concent and Gmail API access using Android

They just had their similar question answered about getting oauth2 working on android (for any google API).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM