使用服务帐户的 Gmail API
Gmail API with service account
在 API 控制台的 mygmailaccount@gmail.com
下,我创建了一个项目,并且成功地使用了 Analytics API
和 Search Console API
服务的服务帐户。我现在启用了 Gmail V1
API 和 运行 此代码:
gmail_scopes = [
"https://mail.google.com/",
"https://www.googleapis.com/auth/gmail.compose",
"https://www.googleapis.com/auth/gmail.readonly",
"https://www.googleapis.com/auth/gmail.send",
"https://www.googleapis.com/auth/gmail.readonly"
]
gmail = Google::Apis::GmailV1::GmailService.new
gmail.authorization = Google::Auth.get_application_default(gmail_scopes)
这与我用来授权 Analytics 和 Search Console 的代码相同(当然范围不同)API。
但是当我 运行 这个 BAD REQUEST
Failed Precondition
错误:
gmail.get_user_profile("mygmailaccount@gmail.com")
一些谷歌搜索告诉我这是因为 API 正在尝试访问不存在的服务帐户电子邮件地址的收件箱,并且在启动过程中我需要包含 mygmailaccount@gmail.com
以便API 服务将使用该收件箱。
我该怎么做?
我试过以下“...”始终是电子邮件地址
gmail.authorization = Google::Auth.get_application_default(gmail_scopes, username: '...')
gmail.authorization = Google::Auth.get_application_default(gmail_scopes, {username: '...'})
gmail.authorization.username = '...'
服务帐户无法访问@gmail.com 邮箱。您需要使用 OAuth2 UserAuthorizer。有关示例,请参见 Gmail API Ruby Quickstart guide。
在 API 控制台的 mygmailaccount@gmail.com
下,我创建了一个项目,并且成功地使用了 Analytics API
和 Search Console API
服务的服务帐户。我现在启用了 Gmail V1
API 和 运行 此代码:
gmail_scopes = [
"https://mail.google.com/",
"https://www.googleapis.com/auth/gmail.compose",
"https://www.googleapis.com/auth/gmail.readonly",
"https://www.googleapis.com/auth/gmail.send",
"https://www.googleapis.com/auth/gmail.readonly"
]
gmail = Google::Apis::GmailV1::GmailService.new
gmail.authorization = Google::Auth.get_application_default(gmail_scopes)
这与我用来授权 Analytics 和 Search Console 的代码相同(当然范围不同)API。
但是当我 运行 这个 BAD REQUEST
Failed Precondition
错误:
gmail.get_user_profile("mygmailaccount@gmail.com")
一些谷歌搜索告诉我这是因为 API 正在尝试访问不存在的服务帐户电子邮件地址的收件箱,并且在启动过程中我需要包含 mygmailaccount@gmail.com
以便API 服务将使用该收件箱。
我该怎么做?
我试过以下“...”始终是电子邮件地址
gmail.authorization = Google::Auth.get_application_default(gmail_scopes, username: '...')
gmail.authorization = Google::Auth.get_application_default(gmail_scopes, {username: '...'})
gmail.authorization.username = '...'
服务帐户无法访问@gmail.com 邮箱。您需要使用 OAuth2 UserAuthorizer。有关示例,请参见 Gmail API Ruby Quickstart guide。