Gmail API-使用服务帐户放置手表

时间:2018-07-04 16:59:58

标签: google-api google-cloud-platform gmail-api google-cloud-pubsub

我们要在已获得访问权限的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),并将其设置为该主题的唯一发布者。 此服务帐户还在IAM上配置了角色Pub/Sub Publisher

现在,在代码中进行此更改并进行尝试时,我们会收到错误消息:

Google::Apis::ClientError (failedPrecondition: Bad Request)

这是代码,如下:

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."
}

我们在这里想念什么?

0 个答案:

没有答案