2013-06-11 36 views

回答

1

我会建议从这里开始:https://developers.google.com/api-client-library/java/apis/gmail/v1

为了验证和使用Gmail的API,概括地说,你需要做以下几点:

  1. 获取的OAuth2令牌
  2. 生成凭证对象与获得的令牌
  3. 创建一个Gmail服务对象,该对象将成为所有API调用的处理程序
  4. 消耗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)在谷歌开发者控制台。

祝你好运!