【发布时间】:2018-04-19 18:27:05
【问题描述】:
我是第一次使用 gmail api。我正在尝试使用 gmail api 保存草稿消息。 源码类似如下,
String urlLink = "https://www.googleapis.com/upload/gmail/v1/users/" + emailSetting.getEmailId() + "/drafts?uploadType=media";
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(urlLink);
httpPost.setHeader("Content-Type", "message/rfc822");
httpPost.setHeader("Authorization", "Bearer " + access_token);
StringEntity params = new StringEntity(message.toString());
httpPost.setEntity(params);
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
这里的消息是我用来准备消息json的JSONObject,如下所示,
JSONObject jsonObject = new JSONObject();
JSONObject message =new JSONObject();
jsonObject.put("threadId", 001);
jsonObject.put("snippet", msg.getSubject());
// Prepare Header start
JSONArray jsonArray = new JSONArray();
JSONObject jsonObj = new JSONObject();
jsonObj.put("name", "Delivered-To");
jsonObj.put("value", msg.getTo());
jsonArray.put(jsonObj);
jsonObj = new JSONObject();
jsonObj.put("name", "To");
//jsonObj.put("value", "<"+msg.getTo()+">");
jsonObj.put("value", msg.getTo());
jsonArray.put(jsonObj);
jsonObj = new JSONObject();
jsonObj.put("name", "From");
jsonObj.put("value", emailSetting.getEmailId());
jsonArray.put(jsonObj);
jsonObj = new JSONObject();
jsonObj.put("name", "Subject");
jsonObj.put("value", msg.getSubject());
jsonArray.put(jsonObj);
jsonObj = new JSONObject();
jsonObj.put("name", "Date");
jsonObj.put("value", new java.util.Date());
jsonArray.put(jsonObj);
JSONObject headerJSONObject = new JSONObject();
headerJSONObject.put("headers", jsonArray);
jsonObject.put("payload", headerJSONObject);
message.put("message", jsonObject);
message.put("id", msg.getMessageId());
上面的代码以状态 200 执行。但草稿消息是空的,即没有主题,没有“to”,也没有正文。如果有人对此源代码有任何建议,请分享,谢谢。
【问题讨论】: