的OAuth和Java(连接到Gmail帐户) [英] OAuth and Java (connecting to GMail)

查看:399
本文介绍了的OAuth和Java(连接到Gmail帐户)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参照此主题:

<一个href=\"http://stackoverflow.com/questions/6099935/empty-secret-returned-in-google-gmail-oauth/6102579#6102579\">Empty秘密谷歌Gmail的OAuth 返回

有人可以告诉我在哪里必要的罐子?

Can someone please tell me where the necessary jars are?

此外,两年,这是仍在使用的API连接到Gmail的最佳方式。

Also, two years on, is this still the best way to connect to GMail using an API.

在简单地说我正在寻找最简单的方法从Java连接到Gmail。

In a nutshell I'm looking for the easiest way to connect to GMail from Java.

感谢

丹尼尔

推荐答案

我会推荐这里开始的:的 https://developers.google.com/api-client-library/java/apis/gmail/v1

I would recommend starting here: https://developers.google.com/api-client-library/java/apis/gmail/v1

要进行身份验证和使用Gmail的API,概括地说,你需要做以下几点:

To authenticate and consume the Gmail API, in a nutshell you need to do following:


  1. 获取OAuth2用户令牌

  2. 生成的凭证与所获得的令牌
  3. 对象
  4. 创建,这将是你的所有API调用处理程序中的Gmail服务对象

  5. 消耗GMAIL API

下面是一些伪code:

Here is some pseudo code:

...
mActivity = your Activity class (i.e., context)
mEmail = the end-user's username (e.g., enduser@<google>.com
mScope = how you intent to use the end user's gmail account (for more go to https://developers.google.com/gmail/api/auth/scopes)
...
//Generate an OAuth2 token using GoogleAuthUtil
token = GoogleAuthUtil.getToken(mActivity, mEmail, mScope); //You will need to catch various exceptions from this call and initiate intents where applicable for end-user action/consent.

//Create a credentials object with the token above
GoogleCredential cr = new GoogleCredential().setAccessToken(token);

//Initialize your Gmail API handler
Gmail service = new Gmail.Builder(HttpTransport obj, JsonFactory obj, cr)
                        .setApplicationName("App Name").build();

//Consume the Gmail API. E.g., how to retrieve all the end-user's labels ...
ListLabelsResponse responseLabels = service.users().labels().list(mEmail).execute();
List<Label> labels = responseLabels.getLabels();
for (Label label : labels) {
    print label.getName());
}

不要忘了启用Gmail的API( https://开头的开发。 google.com/gmail/api/quickstart/quickstart-java )在谷歌开发者控制台。

Dont forget to "Enable the Gmail API" (https://developers.google.com/gmail/api/quickstart/quickstart-java) in the Google Developer Console.

祝你好运!

这篇关于的OAuth和Java(连接到Gmail帐户)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆