简体   繁体   English

Android:使用 Gmail api 从服务读取 Gmail 邮件正文

[英]Android: Read Gmail Message Body From a Service with Gmail api

What I want that, from a service, I want to read the gmail message body when a gmail notification has arrived.我想要的是,从服务中,我想在 gmail 通知到达时阅读 gmail 邮件正文。 When The Gmail Notification has arrived an alarm will occure and I get the full body text in alarmReceiver.当 Gmail 通知到达时,会发生警报,​​我会在 alarmReceiver 中获取全文。

I got the android quick start here gsuits api : https://developers.google.com/gsuite/guides/android .我在这里 gsuits api 获得了 android 快速入门: https : //developers.google.com/gsuite/guides/android But there only describes about Android Sdk And Dependencies.但是只描述了 Android Sdk 和依赖项。 I did not find the whole procedure for capturing gmail body.我没有找到捕获 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?在那之后,我可以在我的android应用程序中检索/获取gmail正文的分步整个过程是什么?

Update: Extended version if anyone needs (Medium): https://shorturl.at/zKQR7更新:如果有人需要扩展版本(中): https : //shorturl.at/zKQR7


First you need to make sure you are authenticated using Google.首先,您需要确保使用 Google 进行身份验证。 You can do that with Firebase Auth (before use, you will have to look how it's configured, there is good documentation available) :您可以使用 Firebase Auth(在使用之前,您必须查看它的配置方式,有很好的文档可用)来做到这一点:

            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 .通过身份验证后,您可以使用FirebaseAuth.getInstance().currentUser

Next step would be to setup Gmail service:下一步是设置 Gmail 服务:

            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因此,您可以像这样messageRead?.snippet一瞥消息正文

There are two not obvious things to note though:不过,有两件不明显的事情需要注意:

  1. You must expect and handle UserRecoverableAuthIOException exception.您必须预期并处理UserRecoverableAuthIOException异常。 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.所有这些execute调用都不会在主线程中处理。

Hope it helps!希望能帮助到你! (sorry don't have time to write detailed how-to). (抱歉没有时间写详细的操作方法)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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