当我使用GMail API发送电子邮件时,我获得了要向其发送消息的用户的访问令牌,然后在发送消息时,我在From字段中添加了一些值.
但是无论我放在from字段中,电子邮件中的消息都显示为From"Me",并且位于Sent邮件和收件箱中.
有没有办法,我可以使用服务帐户发送电子邮件,这样它就不会显示为From"Me",也不会出现在Sent Mails中.
在下面的代码中,我登录,授权应用程序并通过GMail API获取控制台输出。我相信我正在获取线程和线程ID,但是在控制台中看不到消息。
我没有收到任何错误并且得到了输出,就像没有值的键一样。
控制台输出如下所示: 
这是代码:
var CLIENT_ID = 'YOUR_CLIENT_ID';
var SCOPES = ['https://www.googleapis.com/auth/gmail.readonly'];
var USER = 'me';
/**
* Called when the client library is loaded to start the auth flow.
*/
function handleClientLoad() {
window.setTimeout(checkAuth, 1);
}
/**
* Check if the current user has authorized the application.
*/
function checkAuth() {
gapi.auth.authorize(
{'client_id': CLIENT_ID, 'scope': SCOPES, 'immediate': true},
handleAuthResult);
}
/**
* Called when authorization server replies.
*
* @param {Object} authResult Authorization result.
*/
function handleAuthResult(authResult) { …Run Code Online (Sandbox Code Playgroud) 根据这个SO 线程,我应该能够直接转到使用 Gmail REST API 创建的 Gmail 草稿邮件,使用格式如下的超链接:
https://mail.google.com/mail/#drafts?compose={ThreadId}
Run Code Online (Sandbox Code Playgroud)
但是,它不起作用。我在响应 Gmail API 调用时获得的线程 ID 不会打开创建的草稿邮件。真正奇怪的是,当我从 Gmail(我使用 API 创建的)打开草稿时,线程 ID 确实在 URL 中匹配了几秒钟,然后发生了变化。
如何使用 message.threadId 直接打开草稿消息?我也希望它是一条弹出消息,并且应该可以根据此线程进行,但让草稿完全显示是我的首要任务。

我最近开始看到gmail API出现以下错误:
{
"code" : 400,
"errors" : [ {
"domain" : "global",
"message" : "Mail service not enabled",
"reason" : "failedPrecondition"
} ],
"message" : "Mail service not enabled"
}
Run Code Online (Sandbox Code Playgroud)
我测试的每个gmail api调用都会导致错误,但以下代码显示了一个正常的示例:
public static List<String> getThreadIdsFromRFC822MessageIds(Collection<String> messageIds, User u) throws IOException, NoOauthCredentialsException {
List<String> queryTerms = new ArrayList<>();
for (String messageId: messageIds) {
queryTerms.add("rfc822msgid:" + messageId);
}
String queryString = Joiner.on(" OR ").join(queryTerms);
String fieldSelectionString = "messages/threadId";
ListMessagesResponse messages = executeMessageQuery(u, queryString, fieldSelectionString);
List<String> threadIds = new ArrayList<>();
if …Run Code Online (Sandbox Code Playgroud) 我试图让我refresh_token生成一个新的access_token. 我正在使用请求模块发出请求,但它返回一个错误,内容类似于“找不到页面”。
var request = require('request');
module.exports = function(callback){
console.log('here');
request('https://googleapis.com/oauth2/v3/token?client_id=NotID&client_secret=Not_Secret&refresh_token=NotRefresh&grant_type=refresh_token', function (error, response, body) {
if (!error && response.statusCode == 200) {
callback(response)
}
});
}
Run Code Online (Sandbox Code Playgroud) 我正在通过 Python 中的 Gmail API 发送电子邮件,按照https://developers.google.com/gmail/api/v1/reference/users/messages/send#examples
我有一个域别名和收件箱设置,其中包含所有地址,因此发送到 nothing@sub.example.com 和anything@alias.example.com 的电子邮件最终都会出现在 tech@sub.example.com 的收件箱中。
tech@sub.example.com 和 tech@alias.example.com 在 Gmail 设置中都设置为“我拥有的帐户”,并且我在添加第二个帐户时无需验证其所有权(大概是因为它的域是别名)。
使用我正在处理的 API 处理收到的电子邮件,然后从它们被发送到的帐户重新发送它们,在 MIMEMessage 中设置“发件人”标头。不幸的是,它们总是从拥有邮箱的电子邮件地址收到。
其他问题(例如 [1]、[2])表明这应该“正常工作”,至少通过 SMTP,我也尝试过。我认为这一定是使用域别名的怪癖。
在 rails 4 应用程序中使用较新的 google-api-ruby-client 发送 gmail。
require 'google/apis/gmail_v1'
Gmail = Google::Apis::GmailV1
class MailService
def initialize(params)
@params = params
end
def call
message = Gmail::Message.new
service = Gmail::GmailService.new
message.raw = (redacted)
service.request_options.authorization = current_user.token.fresh_token
result = service.send_user_message(current_user.email, message)
end
end
Run Code Online (Sandbox Code Playgroud)
这是调用 API 的结果:
Sending HTTP post https://www.googleapis.com/gmail/v1/users/me/messages/send?
200
#<Hurley::Response POST https://www.googleapis.com/gmail/v1/users/me/messages/send == 200 (63 bytes) 858ms>
Success - #<Google::Apis::GmailV1::Message:0x007fc9cf9b52dd
@id="15096369c05cdb1d",
@thread_id="15096369c05cdb1d">
Run Code Online (Sandbox Code Playgroud)
原始消息从 API Explorer 发送没有问题,但是当从我的应用程序执行时,我的收件箱中收到一封退回电子邮件。在上面的示例中,编辑后的示例是一个有效的 RFC 2822 格式的 base-64 url 安全字符串,而 fresh_token 代表当前用户的 oauth2 访问令牌。
查看退回的邮件
Bounce <nobody@gmail.com>
2:43 PM …Run Code Online (Sandbox Code Playgroud) 我正在尝试通过Gmail发送电子邮件。我几乎将所有范围授予该服务。但是我仍然收到错误消息,权限不足。
Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
"code" : 403,
"errors" : [ {
"domain" : "global",
"message" : "Insufficient Permission",
"reason" : "insufficientPermissions"
} ],
"message" : "Insufficient Permission"
}
at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1056)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
at GmailQuickstart.main(GmailQuickstart.java:133)
Run Code Online (Sandbox Code Playgroud)
:运行失败
private static final List<String> SCOPES =
Arrays.asList(GmailScopes.GMAIL_LABELS, GmailScopes.GMAIL_COMPOSE, GmailScopes.GMAIL_INSERT, GmailScopes.GMAIL_MODIFY, GmailScopes.GMAIL_READONLY, GmailScopes.MAIL_GOOGLE_COM);
static {
try {
HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);
} catch (Throwable t) { …Run Code Online (Sandbox Code Playgroud) 我的代码如下,(上面的代码与google开发者网站中nodejs示例中给出的代码相同.)
function listLabels(auth) {
var gmail = google.gmail({ auth: auth, version: 'v1' });
var emails = gmail.users.messages.list({
includeSpamTrash: false,
maxResults: 500,
q: "",
userId: 'me'
}, function (err, results) {
console.log(results.messages);
});
}
Run Code Online (Sandbox Code Playgroud)
我正在获取包含ID和threadIds的对象数组
现在,如果我输入这些ID
进入这些
function getMessage(messageId,auth) {
var requestt = google.gmail({ auth: auth, version: 'v1' }).users.messages.get({
'userId': 'me',
'id': messageId
});
console.log(requestt)
requestt.execute(function(response){
console.log(response);
});
}
Run Code Online (Sandbox Code Playgroud)
我收到了错误,
TypeError: requestt.execute is not a function
at getMessage (/home/jay/Projects/gmailwebapi/index.js:122:11)
at /home/jay/Projects/gmailwebapi/index.js:113:7
at OAuth2Client._postRequest (/home/jay/Projects/gmailwebapi/node_modules/google-auth-library/lib/auth/oauth2client.js:381:3)
at postRequestCb (/home/jay/Projects/gmailwebapi/node_modules/google-auth-library/lib/auth/oauth2client.js:343:10)
at Request._callback (/home/jay/Projects/gmailwebapi/node_modules/google-auth-library/lib/transporters.js:103:7)
at …Run Code Online (Sandbox Code Playgroud) 在我工作的地方,我们使用Google Apps for Work.在过去9个月中,我们一直在使用Gmail API(每天约2,000个请求)为我们的支持电子邮件帐户提取新电子邮件.
这就是我们最初的设置方式:
我们为每个支持电子邮件帐户执行此操作,以避免在管理控制台中为其中任何一个启用域范围委派.然后,我们可以使用官方支持的npm库对令牌进行身份验证googleapis,类似于:
var google = require('googleapis');
var jwtClient = new google.auth.JWT(
token.client_email,
null,
token.private_key,
['https://www.googleapis.com/auth/gmail.readonly'],
'supportemail@mycompany.com'
);
jwtClient.authorize(function(err, tokens) {
if (err) {
return cb(err);
}
var gmail = google.gmail('v1');
var requestOptions = {
auth: jwtClient,
userId: 'me',
id: messageId,
format: 'raw'
};
gmail.users.messages.get(requestOptions, function(err, response) {
if (err) {
return cb(err);
}
// do stuff with the response
}); …Run Code Online (Sandbox Code Playgroud) google-api jwt service-accounts google-api-nodejs-client gmail-api