参照这个线程:的OAuth和Java(连接到Gmail帐户)
Empty secret returned in Google Gmail OAuth
是否有人可以告诉我在哪里必要的罐子?
另外,两年后,这仍然是使用API连接到GMail的最佳方式。
简而言之,我正在寻找从Java连接到GMail的最简单方法。
感谢
丹尼尔
参照这个线程:的OAuth和Java(连接到Gmail帐户)
Empty secret returned in Google Gmail OAuth
是否有人可以告诉我在哪里必要的罐子?
另外,两年后,这仍然是使用API连接到GMail的最佳方式。
简而言之,我正在寻找从Java连接到GMail的最简单方法。
感谢
丹尼尔
我会建议从这里开始:https://developers.google.com/api-client-library/java/apis/gmail/v1
为了验证和使用Gmail的API,概括地说,你需要做以下几点:
下面是一些伪代码:
...
mActivity = your Activity class (i.e., context)
mEmail = the end-user's username (e.g., [email protected]<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://developers.google.com/gmail/api/quickstart/quickstart-java)在谷歌开发者控制台。
祝你好运!
此代码需要 “谷歌数据API客户端库”。
现在这已经过时了。也许你想转移到“Google帐户身份验证和授权”。 (https://developers.google.com/accounts/)