// 원하는 코드 입력 private static final String MAIL_SCOPE = "https://mail.google.com/";
Gmail에서는 메일 폴더들을 Label이라고 지칭.
메일 리스트를 가져오기위해선 먼저 Label들의 정보를 알아야 함.
private static final String MAIL_SCOPE = "https://mail.google.com/"; private static final HttpTransport HTTP_TRANSPORT = AndroidHttp.newCompatibleTransport(); private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); public static void loadLabels(Context context, Account account, GmailNetworkListener listener) { try { GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(context, Collections.singleton(MAIL_SCOPE)); credential.setSelectedAccount(account); Gmail service = new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).build(); new GetLabelsTask(account, service, listener).execute(); } catch (Exception e) { e.printStackTrace(); } } private static class GetLabelsTask extends AsyncTask<Void, Void, List<Label>> { private Account account; private Gmail service; private GmailNetworkListener listener; private List<Label> labelList; public GetLabelsTask(Account account, Gmail service, GmailNetworkListener listener) { this.account = account; this.service = service; this.listener = listener; this.labelList = new ArrayList<>(); } final JsonBatchCallback<Label> callback = new JsonBatchCallback<Label>() { @Override public void onSuccess(Label label, HttpHeaders responseHeaders) throws IOException { labelList.add(label); } @Override public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) throws IOException { } }; @Override protected List<Label> doInBackground(Void... voids) { if (listener == null) return null; try { ListLabelsResponse response = service.users().labels().list(account.name).execute(); BatchRequest batch = service.batch(); for (Label item : response.getLabels()) { service.users().labels().get(account.name, item.getId()).queue(batch, callback); } batch.execute(); return labelList; } catch (IOException e) { e.printStackTrace(); listener.onFailure(e.getCause().getMessage()); } return null; } @Override protected void onPostExecute(List<Label> result) { super.onPostExecute(result); if (listener != null) { listener.onSuccess(result); } } }