是否有关于如何使用 GMAIL API 读取 C++ 消息的示例?
我有 Google CPP 客户端并已运行日历示例。想知道是否有类似的 Gmail 示例。
我的问题被否决了,所以我正在重写它,希望这更简洁
我一直在编写一个 JavaScript 函数来列出收件箱中的邮件。
使用 - 官方“google-api-nodejs-client”、node.js、电子(和 javascript) 目标:在 gmail 收件箱中列出消息 为此,我需要先授权,然后请求消息
授权 -我从谷歌node.js快速入门复制了代码 - 这在电子中有效(实际上是node.js,因为它是一个命令行脚本)。
询问消息 -谷歌有一个例子,我复制了它,调整了一些部分,但不起作用。我认为我正在使用的示例不是为node.js“google-api-nodejs-client”设计的。- 也许需要不同的授权
这是谷歌示例中的 listmessages 函数,我似乎无法弄清楚如何使用列表标签的授权来使其工作。这就是我尝试过的
不使用客户端库。
Does not use a client library.
/**
* Retrieve Messages in user's mailbox matching query.
*
* @param {String} userId User's email address. The special value 'me'
* can be used to indicate the authenticated user.
* @param {String} query String used to filter the Messages listed.
* @param …Run Code Online (Sandbox Code Playgroud)我正在尝试通过 Python 中的 Gmail API 发送回复,但似乎无法让 Gmail Web 客户端对邮件进行线程化。然而,它们在 Outlook 中被线程化,这很奇怪。
以下是我创建消息的方法:
def create_message(sender, to, subject, message_text, reply_to, thread_id, message_id):
message = MIMEText(message_text, 'html')
message['to'] = to
message['from'] = sender
message['subject'] = subject
if reply_to:
message['threadId'] = thread_id
message['In-Reply-To'] = message_id
message['References'] = message_id
return {'raw': base64.urlsafe_b64encode(message.as_string())}
Run Code Online (Sandbox Code Playgroud)
其中 和thread_id都是message_idMIME 消息 ID,格式类似于<CAGvK4+WJmQGjg6R_QT4rJQApFPeH2xV14DnAVcksTtrc7giE8A@mail.gmail.com>.
我尝试将 更改为由(类似)message['threadId']返回的值,同时将和标头保留为 MIME 消息 ID,但这也不起作用(事实上,这使得邮件无法在 Gmail和Outlook 中线程化)。threadIdcreateMessage168f38ab8c831d11In-Reply-ToReferences
任何帮助表示赞赏!我已经尝试了几乎所有我能想到的设置这些标题的组合,但没有成功。谢谢你!
如何过滤掉没有附件的 Gmail 邮件?
我可以看到,为了包含带有附件的消息,我使用了以下查询:
has:attachment
Run Code Online (Sandbox Code Playgroud)
但是,我没有看到相反的方法。我寻求类似的东西:
!has:attachment
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
我在 Php 和 cURL 中使用 Google Api 发送邮件时遇到问题,
我尝试过这个:
// ENVOIE EMAIL
$message="To: test@example.com\r\nFrom: test@example.com\r\nSubject: GMail test.\r\n My message";
$email=base64_encode($message);
$url_email = 'https://www.googleapis.com/upload/gmail/v1/users/me/messages/send';
$curlPost = array(
'raw' => $email,
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_email);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '. $AccessToken, 'Accept: application/json','Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($curlPost));
$data = curl_exec($ch);
// $data = json_decode(curl_exec($ch), true);;
curl_close($ch);
echo '<br/><h2>Send email</h2>';
print_r($data);
Run Code Online (Sandbox Code Playgroud)
但我收到这样的错误消息:
{“error”:{“errors”:[{“domain”:“global”,“reason”:“badContent”,“message”:“不支持媒体类型“application/json”。有效媒体类型:[message /rfc822]" } ], "code": 400, "message": "不支持媒体类型“application/json”。有效媒体类型:[message/rfc822]" } …
我正在编写一个命令行工具,它使用 gmail api 从邮箱下载报告。我正在使用quickstart.py(转换为python3)进行身份验证。但是,当令牌过期一段时间后无法刷新时,这种方法会起作用。我得到:
文件“/home/ayoub/.local/lib/python3.6/site-packages/google/oauth2/_client.py”,第 248 行,refresh_grant response_data = _token_endpoint_request(request, token_uri, body) 文件“/home/ayoub /.local/lib/python3.6/site-packages/google/oauth2/_client.py”,第 124 行,在 _token_endpoint_request _handle_error_response(response_body) 文件“/home/ayoub/.local/lib/python3.6/site- packages/google/oauth2/_client.py",第 60 行,在 _handle_error_response 中引发异常。RefreshError(error_details, response_body) google.auth.exceptions.RefreshError: ('invalid_scope: 某些请求的范围无效。{invalid=[a, c , e, g, h, i, l, m, ., /, o, p, s, t, :]}', '{\n "error": "invalid_scope",\n "error_description": "一些请求的范围无效。{无效\u003d[a, c, e, g, h, i, l, m, ., /, o, p, s, t, :]}",\n "error_uri": " http://code.google.com/apis/accounts/docs/OAuth2.html“\n }”)
我已确保我不会更改范围并创建了一个新的credentials.json 文件。我还尝试升级所有使用的库。有没有人遇到过这个或知道解决方案?谢谢你!
我希望我的服务帐户能够模拟 GSuite 中的用户之一。我有
domain-wide delegation在服务帐户设置中启用GCPAPI Client添加了service account idGSuite在浏览文档(java)时,我看到了这个
GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream("MyProject-1234.json"))
.createScoped(Collections.singleton(SQLAdminScopes.SQLSERVICE_ADMIN))
.createDelegated("user@example.com");
Run Code Online (Sandbox Code Playgroud)
在这里,他们指定服务帐户应模拟哪个用户。这段代码是java中的。我需要在nodejs 中完成同样的事情。
在查看nodejs-clientfor的文档时googleapis,我发现了这一点:
const {google} = require('googleapis');
const auth = new google.auth.GoogleAuth({
keyFile: '/path/to/your-secret-key.json',
scopes: ['https://www.googleapis.com/auth/cloud-platform'],
});
Run Code Online (Sandbox Code Playgroud)
和
const {google} = require('googleapis');
const oauth2Client = new google.auth.OAuth2(
YOUR_CLIENT_ID,
YOUR_CLIENT_SECRET,
YOUR_REDIRECT_URL
);
// set auth as a global default
google.options({
auth: oauth2Client
});
Run Code Online (Sandbox Code Playgroud)
GoogleAuth和这里有什么区别OAuth2 …
node.js google-api-client google-api-nodejs-client gmail-api
根据下面引用的文档,消息应包含 MessagePart,而 MessagePart 又应包含 MessagePartBody。
https://developers.google.com/gmail/api/reference/rest/v1/users.messages#Message
当我运行下面的代码时(这只是此处找到的示例脚本的修改版本,其中消息替换为标签)
from __future__ import print_function
import pickle
import os.path
import openpyxl
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://mail.google.com/']
def main():
"""Shows basic usage of the Gmail API.
Lists the user's Gmail labels.
"""
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization …Run Code Online (Sandbox Code Playgroud) 在遵循Gmail API java 快速入门指南时, 我遇到了以下代码片段:
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
Run Code Online (Sandbox Code Playgroud)
在编辑器中使用它给了我一个警告,它已被弃用。我有哪些选择?
使用非mime message.get方法,我将取出正文/数据并尝试对其进行解码,以便简单地用英语显示消息.我遇到的问题是一些消息解码很好,然后突然我得到不正确的填充问题.我试过替换+和/无济于事.
为什么会发生这种情况,我该如何解决?
这是我的messages.get方法:
try:
message = service.users().messages().get(userId=user_id, id=msg_id).execute()
# Pull Raw Message Body from Response
message_raw = message['payload']['parts'][0]['body']['data']
# Decode the raw message
message_decoded = base64.b64decode(message_raw)
# Print the messages
print message_decoded
Run Code Online (Sandbox Code Playgroud)
更新
服务
gmail_service = build('gmail', 'v1', http=http)
Run Code Online (Sandbox Code Playgroud)
回溯错误
Traceback (most recent call last):
File "gmail.py", line 166, in <module>
GetMessage(gmail_service, 'me', message_id)
File "gmail.py", line 155, in GetMessage
message_decoded = base64.b64decode(message_raw)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/base64.py", line 76, in b64decode
raise TypeError(msg)
TypeError: Incorrect padding
Run Code Online (Sandbox Code Playgroud)
使用urlsafe时出现回溯错误
Traceback (most recent call …Run Code Online (Sandbox Code Playgroud) gmail-api ×10
gmail ×3
python ×3
google-api ×2
oauth-2.0 ×2
curl ×1
google-oauth ×1
jackson ×1
jackson2 ×1
java ×1
mime-message ×1
node.js ×1
php ×1
sendmail ×1