- 浏览: 2552112 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
Mail Server Solution(4)gmail API Demo
1. API Introduction
It should be working. Here is some API I may need later.
gmail.users.messages.list
userId-email address
labelIds-INBOX
q-job.craigslist.org
gmail.users.messages.get
userId-email address
id-message id I get in the previous API
response-playload, sizeEstimate
gmail.users.messages.attachments.get
userId-email address
messageId-message id I get in the first API
id-attachment file id I get in the previous API
fields-data and size
2. Admin Console Configuration
Visit the google developer’s console
https://console.developers.google.com
Go to APIs&Auth —> Credentials —> I select the installed application for demo
After that I download a JSON file which will have some keys there with name similar to xxxx.json
Actually, I should use this one later
https://developers.google.com/identity/protocols/OAuth2?hl=en_US#serviceaccount
3. Set up Sample with gradle and JAVA
Install Gradle Again with Latest Version
That is amazing, the version is gradle 2.5 now. I was using 1.1 or 1.2 before.
http://sillycat.iteye.com/blog/1074642
http://sillycat.iteye.com/blog/2090147
Place it in the right place and add to the path.
mkdir and prepare the working directory
> pwd
/Users/carl/work/gradle/gmailapi
> gradle init --type basic
Generate the source directory
> mkdir -p src/main/java src/main/resources
Copy and prepare the JSON key file under src/main/resources
Copy and Place the JAVA data there. It is working pretty well.
build.gradle
apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'eclipse'
mainClassName = 'com.sillycat.gmailapi.GmailQuickstart'
sourceCompatibility = 1.7
targetCompatibility = 1.7
version = '1.0'
repositories {
jcenter()
mavenCentral()
}
dependencies {
compile 'com.google.api-client:google-api-client:1.20.0'
compile 'com.google.oauth-client:google-oauth-client-jetty:1.20.0'
compile 'com.google.apis:google-api-services-gmail:v1-rev29-1.20.0'
compile 'org.slf4j:slf4j-api:1.7.12'
testCompile 'junit:junit:4.12'
}
Here is the codes
package com.sillycat.gmailapi;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.gmail.GmailScopes;
import com.google.api.services.gmail.model.*;
import com.google.api.services.gmail.Gmail;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;
public class GmailQuickstart {
/** Application name. */
private static final String APPLICATION_NAME = "Gmail API Java Quickstart";
/** Directory to store user credentials for this application. */
private static final java.io.File DATA_STORE_DIR = new java.io.File(
System.getProperty("user.home"),
".credentials/gmail-api-quickstart");
/** Global instance of the {@link FileDataStoreFactory}. */
private static FileDataStoreFactory DATA_STORE_FACTORY;
/** Global instance of the JSON factory. */
private static final JsonFactory JSON_FACTORY = JacksonFactory
.getDefaultInstance();
/** Global instance of the HTTP transport. */
private static HttpTransport HTTP_TRANSPORT;
/** Global instance of the scopes required by this quickstart. */
private static final List<String> SCOPES = Arrays
.asList(GmailScopes.GMAIL_LABELS);
static {
try {
HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);
} catch (Throwable t) {
t.printStackTrace();
System.exit(1);
}
}
/**
* Creates an authorized Credential object.
*
* @return an authorized Credential object.
* @throws IOException
*/
public static Credential authorize() throws IOException {
// Load client secrets.
InputStream in = GmailQuickstart.class
.getResourceAsStream("/client_secret.json");
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(
JSON_FACTORY, new InputStreamReader(in));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(DATA_STORE_FACTORY)
.setAccessType("offline").build();
Credential credential = new AuthorizationCodeInstalledApp(flow,
new LocalServerReceiver()).authorize("user");
System.out.println("Credentials saved to "
+ DATA_STORE_DIR.getAbsolutePath());
return credential;
}
/**
* Build and return an authorized Gmail client service.
*
* @return an authorized Gmail client service
* @throws IOException
*/
public static Gmail getGmailService() throws IOException {
Credential credential = authorize();
return new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME).build();
}
public static void main(String[] args) throws IOException {
// Build a new authorized API client service.
Gmail service = getGmailService();
// Print the labels in the user's account.
String user = "me";
ListLabelsResponse listResponse = service.users().labels().list(user)
.execute();
List<Label> labels = listResponse.getLabels();
if (labels.size() == 0) {
System.out.println("No labels found.");
} else {
System.out.println("Labels:");
for (Label label : labels) {
System.out.printf("- %s\n", label.getName());
}
}
}
}
Generate the eclipse structure and import to eclipse
> gradle eclipse
Run the application
> gradle run --stacktrace
References:
https://developers.google.com/gmail/api/
https://developers.google.com/gmail/api/quickstart/java
https://developers.google.com/api-client-library/java/apis/gmail/v1
https://developers.google.com/apis-explorer/#p/gmail/v1/
1. API Introduction
It should be working. Here is some API I may need later.
gmail.users.messages.list
userId-email address
labelIds-INBOX
q-job.craigslist.org
gmail.users.messages.get
userId-email address
id-message id I get in the previous API
response-playload, sizeEstimate
gmail.users.messages.attachments.get
userId-email address
messageId-message id I get in the first API
id-attachment file id I get in the previous API
fields-data and size
2. Admin Console Configuration
Visit the google developer’s console
https://console.developers.google.com
Go to APIs&Auth —> Credentials —> I select the installed application for demo
After that I download a JSON file which will have some keys there with name similar to xxxx.json
Actually, I should use this one later
https://developers.google.com/identity/protocols/OAuth2?hl=en_US#serviceaccount
3. Set up Sample with gradle and JAVA
Install Gradle Again with Latest Version
That is amazing, the version is gradle 2.5 now. I was using 1.1 or 1.2 before.
http://sillycat.iteye.com/blog/1074642
http://sillycat.iteye.com/blog/2090147
Place it in the right place and add to the path.
mkdir and prepare the working directory
> pwd
/Users/carl/work/gradle/gmailapi
> gradle init --type basic
Generate the source directory
> mkdir -p src/main/java src/main/resources
Copy and prepare the JSON key file under src/main/resources
Copy and Place the JAVA data there. It is working pretty well.
build.gradle
apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'eclipse'
mainClassName = 'com.sillycat.gmailapi.GmailQuickstart'
sourceCompatibility = 1.7
targetCompatibility = 1.7
version = '1.0'
repositories {
jcenter()
mavenCentral()
}
dependencies {
compile 'com.google.api-client:google-api-client:1.20.0'
compile 'com.google.oauth-client:google-oauth-client-jetty:1.20.0'
compile 'com.google.apis:google-api-services-gmail:v1-rev29-1.20.0'
compile 'org.slf4j:slf4j-api:1.7.12'
testCompile 'junit:junit:4.12'
}
Here is the codes
package com.sillycat.gmailapi;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.gmail.GmailScopes;
import com.google.api.services.gmail.model.*;
import com.google.api.services.gmail.Gmail;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;
public class GmailQuickstart {
/** Application name. */
private static final String APPLICATION_NAME = "Gmail API Java Quickstart";
/** Directory to store user credentials for this application. */
private static final java.io.File DATA_STORE_DIR = new java.io.File(
System.getProperty("user.home"),
".credentials/gmail-api-quickstart");
/** Global instance of the {@link FileDataStoreFactory}. */
private static FileDataStoreFactory DATA_STORE_FACTORY;
/** Global instance of the JSON factory. */
private static final JsonFactory JSON_FACTORY = JacksonFactory
.getDefaultInstance();
/** Global instance of the HTTP transport. */
private static HttpTransport HTTP_TRANSPORT;
/** Global instance of the scopes required by this quickstart. */
private static final List<String> SCOPES = Arrays
.asList(GmailScopes.GMAIL_LABELS);
static {
try {
HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);
} catch (Throwable t) {
t.printStackTrace();
System.exit(1);
}
}
/**
* Creates an authorized Credential object.
*
* @return an authorized Credential object.
* @throws IOException
*/
public static Credential authorize() throws IOException {
// Load client secrets.
InputStream in = GmailQuickstart.class
.getResourceAsStream("/client_secret.json");
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(
JSON_FACTORY, new InputStreamReader(in));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(DATA_STORE_FACTORY)
.setAccessType("offline").build();
Credential credential = new AuthorizationCodeInstalledApp(flow,
new LocalServerReceiver()).authorize("user");
System.out.println("Credentials saved to "
+ DATA_STORE_DIR.getAbsolutePath());
return credential;
}
/**
* Build and return an authorized Gmail client service.
*
* @return an authorized Gmail client service
* @throws IOException
*/
public static Gmail getGmailService() throws IOException {
Credential credential = authorize();
return new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME).build();
}
public static void main(String[] args) throws IOException {
// Build a new authorized API client service.
Gmail service = getGmailService();
// Print the labels in the user's account.
String user = "me";
ListLabelsResponse listResponse = service.users().labels().list(user)
.execute();
List<Label> labels = listResponse.getLabels();
if (labels.size() == 0) {
System.out.println("No labels found.");
} else {
System.out.println("Labels:");
for (Label label : labels) {
System.out.printf("- %s\n", label.getName());
}
}
}
}
Generate the eclipse structure and import to eclipse
> gradle eclipse
Run the application
> gradle run --stacktrace
References:
https://developers.google.com/gmail/api/
https://developers.google.com/gmail/api/quickstart/java
https://developers.google.com/api-client-library/java/apis/gmail/v1
https://developers.google.com/apis-explorer/#p/gmail/v1/
发表评论
-
Stop Update Here
2020-04-28 09:00 316I will stop update here, and mo ... -
NodeJS12 and Zlib
2020-04-01 07:44 476NodeJS12 and Zlib It works as ... -
Docker Swarm 2020(2)Docker Swarm and Portainer
2020-03-31 23:18 369Docker Swarm 2020(2)Docker Swar ... -
Docker Swarm 2020(1)Simply Install and Use Swarm
2020-03-31 07:58 370Docker Swarm 2020(1)Simply Inst ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 337Traefik 2020(1)Introduction and ... -
Portainer 2020(4)Deploy Nginx and Others
2020-03-20 12:06 431Portainer 2020(4)Deploy Nginx a ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 436Private Registry 2020(1)No auth ... -
Docker Compose 2020(1)Installation and Basic
2020-03-15 08:10 374Docker Compose 2020(1)Installat ... -
VPN Server 2020(2)Docker on CentOS in Ubuntu
2020-03-02 08:04 455VPN Server 2020(2)Docker on Cen ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 385Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 478NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 423Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 337Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 248GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 451GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 328GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 314Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 319Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 294Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 312Serverless with NodeJS and Tenc ...
相关推荐
**Macallan Mail Solution** 是一个综合性的邮件服务器软件,专为个人和企业用户提供高效、安全的邮件通信服务。这款软件支持多种邮件协议,包括POP3、IMAP、SMTP以及HTTP,这意味着用户可以通过传统的邮件客户端...
《SDACourse_Demo_System_Solution_myeclipse_DEMO_》是针对软件体系与设计结构的一个Demo演示解决方案,它利用了强大的集成开发环境——MyEclipse。本项目旨在通过具体的实例,帮助开发者理解和掌握软件系统的设计...
Macallan Mail Solution 是一款 POP3 、IMAP、SMTP、HTTP (webMail) 邮件服务器软件 ,它还包括了防垃圾邮件机制。 Macallan Mail Solution 免费邮件服务器 (SMTP/POP3/IMAP/HTTP/NEWS/SSL/Tunnel) 支持 ...
HP Server Solution for SAP (work processes).pdf
THR2171 - Deploying a highly available SQL Server solution
IBM公司于2006年推出的TPC-R DR Solution Demo是一款关于容灾解决方案的演示,旨在介绍如何通过软件管理来确保企业信息系统的高可用性和业务连续性。随着新法规、安全威胁和服务中断不断出现,IT业务的可靠性变得...
4. **API探索**:Swagger UI使得测试API变得简单,你可以填写请求参数,然后点击"尝试它"按钮,查看返回的结果。这对于快速测试和调试API非常有帮助。 5. **Swagger配置**:在ASP.NET Core项目中,Swagger配置通常...
本教程“SSAS Simple Demo Solution”提供了一个入门级的实例,帮助初学者理解SSAS的基本概念和操作。 **SSAS核心概念** 1. **多维数据模型与Tabular模型**:SSAS支持两种主要的数据模型类型。多维模型基于星型或...
DLL地狱及其解决方案 ,Dll Hell Solution,此为测试Demo 原文地址:https://www.codeproject.com/Articles/4896/The-DLL-Hell-Problems-and-Solutions#_articleTop
这个"Demo_BitmapOOM_Solution.rar"压缩包文件很可能是为了解决Bitmap内存溢出问题提供的一种解决方案。现在,我们将深入探讨Bitmap OOM的成因、影响以及如何有效地解决这一问题。 Bitmap是Android系统中用于存储和...
标题“Solution-Challenge-Demo”很可能是一个编程挑战或者项目演示的解决方案,这通常涉及到一系列步骤和技术,用于解决特定的IT问题或实现一个功能。描述中提到的“安装从安装依赖项”表明这是一个需要先安装必要...
It places a much heavier burden on the mail server, requiring the server to receive the new messages, deliver them to users when requested, and maintain them in multiple folders for each user. ...
### SQL Server 2005 T-SQL Recipes: A Problem-Solution Approach #### 简介 《SQL Server 2005 T-SQL Recipes: A Problem-Solution Approach》是一本针对SQL Server 2005数据库管理和开发的专业书籍。本书以实际...
在"SL4Demo Solution"这个压缩包中,包含了SilverLight 4的演示项目。这个解决方案可能包含了多个项目,如: - 主要的SilverLight应用程序项目,展示SL4的新特性和功能。 - 服务器端项目,可能使用ASP.NET或WCF来...
名称:Apic - Complete API solution -------------------- 版本:3.2.1 作者:https://apic.app 分类:开发者工具 -------------------- 概述:满足您所有 API 设计、文档和测试需求的唯一工具。 描述: 满足您所有...
在“学习NAnt小Demo_Build CS+Solution+MSBuild+SVN+NUnit+NUnitReport”这个项目中,你可能会学到如何: 1. 使用NAnt创建和执行构建脚本,自动编译C#项目。 2. 配置和理解MSBuild项目文件,了解如何利用它进行项目...
2. **RabbitMQ客户端应用(iMQ.Demo)**:`iMQ.Demo`可能是一个包含RabbitMQ客户端代码的项目,用于演示如何使用RabbitMQ的API来发送和接收消息。 - **生产者部分**:在代码中,生产者会创建消息,并连接到...