+1

Lập trình Android sử dụng Gmail API

Quickstart

Lấy SHA1 từ keystore keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore -list -v

Trong đó androiddebugkey chính là tên alias của keystore của bạn tạo ký app Còn ~/.android/debug.keystore là đường dẫn tới file keystore của bạn Còn bạn copy paste nguyên trên thì nó lấy SHA từ keystore debug của bạn.

Ok, giờ bạn có SHA1 với package của project Android bạn rồi, vào console.developers.google.com setting thôi

Từng bước setting thế nào bạn không biết xem ở link quickstart của google: https://developers.google.com/gmail/api/quickstart/android (Step 2 đó, trên là mình giải thích về Step 1 của link trên)

Còn bạn làm tiếp Step 3+4+5 bạn sẽ có 1 project đơn giản lấy danh sách các Label hiển thị vào TextView (Mỗi Label cộng ký tự xuống dòng với cho nó Scroll :v Lừa tình tưởng là ListView)

Còn 2 điều chú ý nhỏ:

  • Gmail API chỉ hỗ trợ từ API 11: Android 3.0 (Honeycomb) trở lên nhé
  • Scope: private static final String[] SCOPES = { GmailScopes.GMAIL_LABELS };

Đoạn code này khai báo chỉ được lấy Label của Gmail. Tức nếu bạn chỉ khai báo trên muốn send mail hay lấy mail về là không được. Muốn dùng bạn phải khai báo thêm Scope.

Đây là danh sách các scope: (mình lấy trong class GmailScopes đó, muốn xem kỹ hơn, mở file đó mà xem nhé)

MAIL_GOOGLE_COM: View and manage your mail. GMAIL_COMPOSE: Manage drafts and send emails. GMAIL_INSERT: Insert mail into your mailbox. GMAIL_LABELS: Manage mailbox labels. GMAIL_METADATA: View your email message metadata such as labels and headers, but not the email body. GMAIL_MODIFY: View and modify but not delete your email. GMAIL_READONLY: View your emails messages and settings. GMAIL_SEND: Send email on your behalf. GMAIL_SETTINGS_BASIC:Manage your basic mail settings. GMAIL_SETTINGS_SHARING: Manage your sensitive mail settings, including who can manage your mail.

Test API

Để dùng thử API bạn vào link sau: https://developers.google.com/gmail/api/v1/reference/ Ở bên phải có mục: Try this API để mình test API giống như test Postman đó. Test API list trước để lấy danh sách Rồi mới cái dưới nhé. À userId của chính mình là me nhé

Demo

Sau đây là demo của mình: Làm ứng dụng email client đơn giản (lấy danh sách mail đến, gửi mail, trả lời mail - trả lời mail của mình chính là gửi mail luôn thôi). Để làm ứng dụng đơn giản này, mình chỉ sử dụng scope GmailScopes.MAIL_GOOGLE_COM thôi nhé

  • Nhận danh sách các mail: (Hộp Inbox)
    public static List<Message> listAllMessages(Gmail service, String userId, long max) throws IOException {
        ListMessagesResponse response;
        List labelIds = new ArrayList();
        labelIds.add("INBOX");
        if (max < 1) {
            response = service.users().messages().list(userId).setLabelIds(labelIds).execute();
        } else {
            response = service.users().messages().list(userId).setLabelIds(labelIds).setMaxResults(max).execute();
        }
        List<Message> messages = new ArrayList<Message>();
        if (response.getMessages() != null) {
            messages.addAll(response.getMessages());
        }
        return messages;
    }
  • Gửi mail:
    public static Message sendMessage(Gmail service, String userId, MimeMessage emailContent) throws MessagingException, IOException {
        Message message = createMessageWithEmail(emailContent);
        message = service.users().messages().send(userId, message).execute();
        return message;
    }

Chi tiết để hiểu hơn, download source code mình về mà xem nhé: https://github.com/QuizSystem/GmailAPI


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí