Android: Read Gmail Message Body From a Service with Gmail api

蹲街弑〆低调 提交于 2021-02-05 12:17:29

问题


What I want that, from a service, I want to read the gmail message body when a gmail notification has arrived. When The Gmail Notification has arrived an alarm will occure and I get the full body text in alarmReceiver.

I got the android quick start here gsuits api : https://developers.google.com/gsuite/guides/android . But there only describes about Android Sdk And Dependencies. I did not find the whole procedure for capturing gmail body.

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:25.0.1'
compile 'com.google.android.gms:play-services-auth:15.0.1'
compile 'pub.devrel:easypermissions:0.3.0'
compile('com.google.api-client:google-api-client-android:1.23.0') {
    exclude group: 'org.apache.httpcomponents'
}
compile('com.google.apis:google-api-services-<API>-<VERSION>') {
    exclude group: 'org.apache.httpcomponents'
}}

After that, what is the step by step whole procedure, that I can retrieve/get the gmail body in my android app?


回答1:


Update: Extended version if anyone needs (Medium): https://shorturl.at/zKQR7


First you need to make sure you are authenticated using Google. You can do that with Firebase Auth (before use, you will have to look how it's configured, there is good documentation available) :

            val providers = arrayListOf(
                AuthUI.IdpConfig.GoogleBuilder().build()
            )

            startActivityForResult(
                AuthUI.getInstance()
                    .createSignInIntentBuilder()
                    .setAvailableProviders(providers)
                    .build(),
                RQ_FIREBASE_AUTH
            )

Once authenticated, you can use FirebaseAuth.getInstance().currentUser.

Next step would be to setup Gmail service:

            val credential = GoogleAccountCredential.usingOAuth2(
                applicationContext, listOf(GmailScopes.GMAIL_LABELS, GmailScopes.GMAIL_READONLY)
            )
                .setBackOff(ExponentialBackOff())
                .setSelectedAccountName(FirebaseAuth.getInstance().currentUser?.email)

            val service = Gmail.Builder(
                NetHttpTransport(), AndroidJsonFactory.getDefaultInstance(), credential
            )
                .setApplicationName("YourAppName")
                .build()

Please note the above is not production ready stuff of course.

Finally, here is reading part:

val messageRead = service.users().messages()?.get(FirebaseAuth.getInstance().currentUser?.email, message?.id)?.setFormat("raw")?.execute()

So you can get a glimps into body of your message like this messageRead?.snippet

There are two not obvious things to note though:

  1. You must expect and handle UserRecoverableAuthIOException exception. This is the point when user has to explicitly grant your app to do certain stuff with messages

  2. All these execute calls not be handle don main thread.

Hope it helps! (sorry don't have time to write detailed how-to).



来源:https://stackoverflow.com/questions/60578461/android-read-gmail-message-body-from-a-service-with-gmail-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!