我们想在我们被授予访问权限的 gmail 帐户上放置一个手表(另请参阅https://developers.google.com/gmail/api/v1/reference/users/watch)。
放置手表需要一个 PUB/SUB 主题名称,我们有:projects/project-name/topics/topic-name. 在创建它时,我们为成员设置了发布权限allAuthenticatedUsers(授予登录到 Google 帐户的任何用户的访问权限)。
这很有效,我们能够在主题中获取观看通知并正确使用消息。
现在,我们想通过只允许服务帐户来限制发布权限。我们创建了一个服务帐户 ( gmail-watch@project-name.iam.gserviceaccount.com),并将其设置为该主题的唯一发布者。此服务帐户还配置了Pub/Sub Publisher
IAM 上的角色。
现在,在代码中进行此更改并尝试它时,我们得到了错误:
Google::Apis::ClientError (failedPrecondition: Bad Request)
这是代码,fwiw:
require 'googleauth'
require 'google/apis/gmail_v1'
scope = 'https://mail.google.com/'
authorizer = Google::Auth::ServiceAccountCredentials.make_creds(
json_key_io: File.open('./config/google-service-account.json'),
scope: scope
)
Gmail = ::Google::Apis::GmailV1
service = Gmail::GmailService.new
service.authorization = authorizer
service.watch_user(
'user-email@gmail.com',
{
topic_name: 'projects/project-name/topics/topic-name',
label_ids: ['INBOX','SENT'],
label_filter_action: 'include'
},
{}
)
我们也尝试sub在授权中设置参数:
authorizer.sub = 'user-mail@gmail.com'但错误是相同的,尽管在此之后我们无法再获取访问令牌:
> authorizer.fetch_access_token!
Signet::AuthorizationError (Authorization failed. Server message:)
{
"error": "unauthorized_client",
"error_description": "Client is unauthorized to retrieve access tokens using this method."
}
我们在这里缺少什么?