【发布时间】:2019-08-03 09:46:58
【问题描述】:
我已按照所有步骤设置域范围的授权 (https://developers.google.com/admin-sdk/reports/v1/guides/delegation) 并点击此链接 (https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority),最终我在 Ruby 中创建了这个类。
class Gsuite::ServiceAccount
def initialize(person: nil)
end
def authorized_client
Google::Auth::ServiceAccountCredentials.make_creds(
authorizer = Google::Auth::ServiceAccountCredentials.make_creds(
json_key_io: StringIO.new(File.read AppConfig[:gsuite_service_account]
[:credentials_file]),
scope: AppConfig[:gsuite_service_account][:scope])
client = authorizer.fetch_access_token!
end
end
这个类返回我这个哈希
{"access_token"=>"a_long_token_string_here", "expires_in"=>3600, "token_type"=>"Bearer"}
然后,我创建了这个方法(在我的 Admin 类中)以连接到 Gmail 服务
def gsuite_client_access
@client ||= Gsuite::ServiceAccount.new(person: self.email.to_s).authorized_client
authorized_client = Google::Apis::GmailV1::GmailService.new
authorized_client.authorization = @client
authorized_client
end
因此,当我尝试在代码的另一部分中使用这一行列出我的 Gmail 邮件时
inbox = current_admin.gsuite_client_access.list_user_messages('me', max_results: 10)
我收到以下错误消息 =>
Sending HTTP get https://www.googleapis.com/gmail/v1/users/me/messages?maxResults=10
401
#<Hurley::Response GET https://www.googleapis.com/gmail/v1/users/me/messages?maxResults=10 == 401 (238 bytes) 645ms>
Caught error Unauthorized
Error - #<Google::Apis::AuthorizationError: Unauthorized>
Retrying after authentication failure
Google::Apis::AuthorizationError: Unauthorized
任何想法这里缺少什么?
【问题讨论】:
标签: ruby google-api gmail gmail-api google-api-ruby-client