使用 googleapiclient 通过 Id 发送电子邮件草稿
Using googleapiclient to send an email draft by Id
我正在使用 google 的 API 客户端与 Gmail API 进行交互。假设我有草稿的不可变 ID,我想发送关联的草稿。
我试过了:
service.users().drafts().send(
userId='me', id=draft_id).execute(http=http)
这里 draft_id
是我要发送的草稿的 ID,http
是 Http
的一个实例,适用于其他请求(因此已正确验证).
尝试上面的方法,我得到 TypeError
:
Traceback (most recent call last):
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/Users/mgilson/git/spider-web/app/pattern/handlers/ajax/email.py", line 77, in post
'success': int(client.send_draft(draft_id))
File "/Users/mgilson/git/spider-web/app/pattern/services/email/gmail.py", line 601, in send_draft
userId='me', id=draft_id) \
File "/Users/mgilson/git/spider-web/app/third_party/googleapiclient/discovery.py", line 669, in method
raise TypeError('Got an unexpected keyword argument "%s"' % name)
TypeError: Got an unexpected keyword argument "id"
文档有 Java example,但没有 python 示例。
我尝试过的其他变体:
service.users().drafts().send(
userId='me', draftId=draft_id).execute(http=http)
service.users().drafts().send(
userId='me', draft_id=draft_id).execute(http=http)
service.users().drafts().send(
userId='me', body={'draft': {'id': draft_id}}).execute(http=http)
最后一个居然给出了不同的错误:
Traceback (most recent call last):
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/Users/mgilson/git/spider-web/app/pattern/handlers/ajax/email.py", line 77, in post
'success': int(client.send_draft(draft_id))
File "/Users/mgilson/git/spider-web/app/pattern/services/email/gmail.py", line 603, in send_draft
.execute(http=self._http)
File "/Users/mgilson/git/spider-web/app/third_party/oauth2client/util.py", line 140, in positional_wrapper
return wrapped(*args, **kwargs)
File "/Users/mgilson/git/spider-web/app/third_party/googleapiclient/http.py", line 729, in execute
raise HttpError(resp, content, uri=self.uri)
HttpError: <HttpError 400 when requesting https://www.googleapis.com/gmail/v1/users/me/drafts/send?alt=json returned "Invalid draft">
这让我觉得 可能 正朝着正确的方向前进......(注意,我已经能够使用 ID 从 API 上面链接的资源管理器,所以我确信我正在使用有效的 ID)
发送此数据的正确方法是什么?
我不确定这有多相关,但是发现 API 对 GMAIL Api 的表示可以在 https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest
找到
具体来说,我认为我正在使用的方法由以下 JSON:
定义
"send": {
"id": "gmail.users.drafts.send",
"path": "{userId}/drafts/send",
"httpMethod": "POST",
"description": "Sends the specified, existing draft to the recipients in the To, Cc, and Bcc headers.",
"parameters": {
"userId": {
"type": "string",
"description": "The user's email address. The special value me can be used to indicate the authenticated user.",
"default": "me",
"required": true,
"location": "path"
}
},
"parameterOrder": [
"userId"
],
"request": {
"$ref": "Draft"
},
"response": {
"$ref": "Message"
},
"scopes": [
"https://mail.google.com/",
"https://www.googleapis.com/auth/gmail.compose",
"https://www.googleapis.com/auth/gmail.modify"
],
"supportsMediaUpload": true,
"mediaUpload": {
"accept": [
"message/rfc822"
],
"maxSize": "35MB",
"protocols": {
"simple": {
"multipart": true,
"path": "/upload/gmail/v1/users/{userId}/drafts/send"
},
"resumable": {
"multipart": true,
"path": "/resumable/upload/gmail/v1/users/{userId}/drafts/send"
}
}
}
},
查看 Gmail API Python docs, it looks like the second parameter is the body
of the request. According to the Gmail API reference,您需要在正文中有一个 id
字段,其中包含您要发送的草稿的 draftId
:
service.users().drafts().send(
userId='me', body={ 'id': draft_id }).execute(http=http)
我正在使用 google 的 API 客户端与 Gmail API 进行交互。假设我有草稿的不可变 ID,我想发送关联的草稿。
我试过了:
service.users().drafts().send(
userId='me', id=draft_id).execute(http=http)
这里 draft_id
是我要发送的草稿的 ID,http
是 Http
的一个实例,适用于其他请求(因此已正确验证).
尝试上面的方法,我得到 TypeError
:
Traceback (most recent call last):
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/Users/mgilson/git/spider-web/app/pattern/handlers/ajax/email.py", line 77, in post
'success': int(client.send_draft(draft_id))
File "/Users/mgilson/git/spider-web/app/pattern/services/email/gmail.py", line 601, in send_draft
userId='me', id=draft_id) \
File "/Users/mgilson/git/spider-web/app/third_party/googleapiclient/discovery.py", line 669, in method
raise TypeError('Got an unexpected keyword argument "%s"' % name)
TypeError: Got an unexpected keyword argument "id"
文档有 Java example,但没有 python 示例。
我尝试过的其他变体:
service.users().drafts().send(
userId='me', draftId=draft_id).execute(http=http)
service.users().drafts().send(
userId='me', draft_id=draft_id).execute(http=http)
service.users().drafts().send(
userId='me', body={'draft': {'id': draft_id}}).execute(http=http)
最后一个居然给出了不同的错误:
Traceback (most recent call last):
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/Users/mgilson/git/spider-web/app/pattern/handlers/ajax/email.py", line 77, in post
'success': int(client.send_draft(draft_id))
File "/Users/mgilson/git/spider-web/app/pattern/services/email/gmail.py", line 603, in send_draft
.execute(http=self._http)
File "/Users/mgilson/git/spider-web/app/third_party/oauth2client/util.py", line 140, in positional_wrapper
return wrapped(*args, **kwargs)
File "/Users/mgilson/git/spider-web/app/third_party/googleapiclient/http.py", line 729, in execute
raise HttpError(resp, content, uri=self.uri)
HttpError: <HttpError 400 when requesting https://www.googleapis.com/gmail/v1/users/me/drafts/send?alt=json returned "Invalid draft">
这让我觉得 可能 正朝着正确的方向前进......(注意,我已经能够使用 ID 从 API 上面链接的资源管理器,所以我确信我正在使用有效的 ID)
发送此数据的正确方法是什么?
我不确定这有多相关,但是发现 API 对 GMAIL Api 的表示可以在 https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest
找到具体来说,我认为我正在使用的方法由以下 JSON:
定义 "send": {
"id": "gmail.users.drafts.send",
"path": "{userId}/drafts/send",
"httpMethod": "POST",
"description": "Sends the specified, existing draft to the recipients in the To, Cc, and Bcc headers.",
"parameters": {
"userId": {
"type": "string",
"description": "The user's email address. The special value me can be used to indicate the authenticated user.",
"default": "me",
"required": true,
"location": "path"
}
},
"parameterOrder": [
"userId"
],
"request": {
"$ref": "Draft"
},
"response": {
"$ref": "Message"
},
"scopes": [
"https://mail.google.com/",
"https://www.googleapis.com/auth/gmail.compose",
"https://www.googleapis.com/auth/gmail.modify"
],
"supportsMediaUpload": true,
"mediaUpload": {
"accept": [
"message/rfc822"
],
"maxSize": "35MB",
"protocols": {
"simple": {
"multipart": true,
"path": "/upload/gmail/v1/users/{userId}/drafts/send"
},
"resumable": {
"multipart": true,
"path": "/resumable/upload/gmail/v1/users/{userId}/drafts/send"
}
}
}
},
查看 Gmail API Python docs, it looks like the second parameter is the body
of the request. According to the Gmail API reference,您需要在正文中有一个 id
字段,其中包含您要发送的草稿的 draftId
:
service.users().drafts().send(
userId='me', body={ 'id': draft_id }).execute(http=http)