API从服务中读取Gmail邮件正文

API从服务中读取Gmail邮件正文

本文介绍了Android:使用Gmail API从服务中读取Gmail邮件正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从服务中获得的信息是,当Gmail通知到达时,我想阅读Gmail消息正文.当Gmail通知到达时,将会发生警报,​​我将在AlarmReceiver中获取全文.

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.

我在这里找到了Android快速入门gsuits api: https://developers.google.com/gsuite/guides/android .但是这里仅介绍有关Android Sdk和依赖项的信息.我没有找到捕获gmail正文的整个过程.

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'
}}

在那之后,我可以在我的android应用程序中检索/获取gmail主体的逐步操作的整个过程是什么?

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

推荐答案

更新:如果有人需要扩展版本(中): https://shorturl.at/zKQR7

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

首先,您需要确保使用Google进行身份验证.您可以使用Firebase Auth来做到这一点(在使用前,您必须查看它的配置方式,这里有很好的文档资料):

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
            )

一旦通过身份验证,就可以使用FirebaseAuth.getInstance().currentUser.

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

下一步是设置Gmail服务:

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()

这样您就可以瞥见邮件正文,例如messageRead?.snippet

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. 您必须期望并处理UserRecoverableAuthIOException异常.这是用户必须明确授权您的应用程序对消息执行某些操作的时间点

  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

所有这些execute调用都不是主线程的句柄.

All these execute calls not be handle don main thread.

希望有帮助! (对不起,您没有时间写详细的操作方法).

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

这篇关于Android:使用Gmail API从服务中读取Gmail邮件正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 18:30