Skip to content

Instantly share code, notes, and snippets.

@oradkovsky
Created March 9, 2020 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oradkovsky/915f0071ed1897423742de8d2aa61015 to your computer and use it in GitHub Desktop.
Save oradkovsky/915f0071ed1897423742de8d2aa61015 to your computer and use it in GitHub Desktop.
// You may have guessed this is body of suspend function as I'm using Kotlin coroutines.
// Service is com.google.api.services.gmail.Gmail.
try {
val executeResult: ListMessagesResponse? =
withContext(Dispatchers.IO) {
service.users().messages()?.list("me")?.setQ("to:me")?.execute()
}
// For the sake of test, let's get 1st message on the list
val message: Message? = executeResult?.messages?.get(0)
// Actual reading of the message, by message ID
val messageRead = withContext(Dispatchers.IO) {
service.users().messages()
?.get(FirebaseAuth.getInstance().currentUser?.email, message?.id)
?.setFormat("full")?.execute()
}
// At this point you can get to short version of your message body as messageRead?.snippet
} catch (e: UserRecoverableAuthIOException) {
startActivityForResult(e.intent, RQ_GMAIL_ACCESS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment