Bài viết này mình memo lại những kinh nghiệm và khó khăn khi làm việc với Google Gmail API.Những ai đang làm việc liên quan đến Gmail API có thể tham khảo. Google Developers ConsoleĐây là nơi chứa những thiết lập cần thiết đối với lập trình viên khi làm việc liên quan
tới Google API. Tạo Project mới ở Google Developers Consolehttps://console.developers.google.com/project Đầu tiên bạn cần phải có 1 tài khoản Google.
Truy cập vào link trên sẽ có button tạo Project , ta có thể tạo bao nhiêu Project tùy ý . Sau khi tạo
Project xong sẽ xuất hiện danh sách các API của Google , chọn API mà bạn muốn sử dung , trong bài viết này mình chỉ chọn Gmail API. Mặc định trạng thái của Gmail API là Disable, click vào Enable , để Enable Gmail API. Tạo CredentialsTạo Credentials theo các bước sau : Credentials ->Create credentials -> Oauth client ID Tùy theo mục đích sử dụng mà
có những lựa trọn thích hợp , trong bào viết này tôi chạy ở Background nên tôi trọn Application type là Other. Kết thúc bước này là bạn sẽ có được Client ID và Client secret. Lấy OAuth Codehttps://accounts.google.com/o/oauth2/auth?client_id={Oauth client ID}&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=https://www.googleapis.com/auth/gmail.readonly&response_type=code&approval_prompt=force&access_type=offline
Bằng cách request vào link trên với tham số
client_id chính là client_id mình lấy được ở bước trên ,bạn sẽ được Google cung cấp một mã code , và dùng mã code này cho các thiết lập kế tiếp . Còn một tham số quan trong khác nữa là scope.Bạn có thể can thiệp viết mail , sửa mail , hay xóa mail được hay không chính là nhờ vào tham số scope nầy . Theo như hình trên thì với scope=https://mail.google.com/ thì bạn có quyền hạn cao nhất ,
có thể can thiệp đến mọi thứ trên Gmail của bạn. Tùy theo mục đích sử dụng mà nên có những lựa trọn phù hợp. Kết quả sau khi request: Lấy Access TokenĐể truy cập vào Gmail API cần phải có Access Token. Resquest: curl -d client_id={OAuth Client ID} -d client_secret={OAuth Client secret} -d redirect_uri=urn:ietf:wg:oauth:2.0:oob -d grant_type=authorization_code -d code={cái code vừa lấy phía trên} https://accounts.google.com/o/oauth2/token
Response {
"access_token" : "xxxxxxxxxxxxxxxxx",
"token_type" : "Bearer",
"expires_in" : 3600,
"refresh_token" : "xxxxxxxxxxxxxxxxxxxxx"
}
Lấy Access Token bằng Refresh TokenDo
access__token chỉ có hiệu lực trong vòng 60 phút , quá thời gian đó ta phải refresh lại access token mới để truy cập Gmail API bằng refresh token. Resquest: curl -d "client_id={ OAuth Client ID }&client_secret={ OAuth Client secret }&refresh_token={ refresh_token}&grant_type=refresh_token" https://accounts.google.com/o/oauth2/token
Response {
"access_token" : "xxxxxxxxxxxxxxxxxxxxxxxx",
"token_type" : "Bearer",
"expires_in" : 3600
}
Thao tác với API GmailVới Access Token đã lấy được ở trên ta có thể truy cập và thao tác với Gmail API. Lấy danh sách Mail Resquest: curl -H "Authorization: Bearer {access_token}" https://www.googleapis.com/gmail/v1/users/me/messages/
Response: {
"messages": [
{
"id": "xxxxxx",
"threadId": "xxxx"
},
{
"id": "xxxxxx",
"threadId": "xxxx"
},
{
"id": "xxxxxx",
"threadId": "xxxx"
},
.
.
.
{
"id": "xxxxxx",
"threadId": "xxxx"
}
],
"nextPageToken": "xxxxxxxx",
"resultSizeEstimate": 520
}
Dựa vào id ở trên ta có thể lấy được nội dung chi tiết của mail. Lấy
nội dung Mail Resquest: curl -H "Authorization: Bearer {access_token}" https://www.googleapis.com/gmail/v1/users/me/messages/{id lấy được phía trên}
Response: "id": "{メール一覧で取得したid}",
"threadId": "メールのスレッドID",
"labelIds": [分類されているラベルID1,分類されているラベルID2],
"snippet": "本文のスニペット",
"historyId": "9052307",
"payload": {
"partId": "",
"mimeType": "text/plain",
"filename": "",
"headers": [
{
"name": "Delivered-To",
"value": "受信者のメールアドレス"
},
{
"name": "Received",
"value": "by xx.xx.xx.xxx with SMTP id xxxxxx; Thu, 25 Dec 2014 07:06:06 -0800 (PST)"
},
{
"name": "X-Received",
"value": "by xx.xx.xx.xxx with SMTP id xxxxxx; Thu, 25 Dec 2014 07:05:32 -0800 (PST)"
},
{
"name": "Return-Path",
"value": "返信先のメールアドレス"
},
{
"name": "From",
"value": "メールアドレス(From)"
},
{
"name": "to",
"value": "メールアドレス(To)"
},
{
"name": "Cc",
"value": ""
},
{
"name": "Date",
"value": "Fri, 26 Dec 2014 00:05:26 +0900"
},
{
"name": "MIME-Version",
"value": "1.0"
},
{
"name": "Content-Type",
"value": "text/plain; charset=\"iso-2022-jp\""
},
{
"name": "Content-Transfer-Encoding",
"value": "7bit"
},
{
"name": "Subject",
"value": "メールの件名"
},
{
"name": "Reply-To",
"value": "返信先メールアドレス"
},
{
"name": "X-Mail-Count",
"value": "2476689"
},
{
"name": "Precedence",
"value": "bulk"
},
{
"name": "X-QuickML",
"value": "true"
}
],
"body": {
"size": 1288,
"data": "本文でベース64エンコードされた本文"
}
},
"sizeEstimate": 3933
}
Kết luậnCòn nhiều thao tác khác các bạn có thể tự tìm hiểu thêm ở địa chỉ bên dưới : https://developers.google.com/gmail/api/v1/reference/users/messages/get |