我正在尝试通过https://www.googleapis.com/auth/admin.directory.user插入用户帐户。我正在使用下面的gem 'google-api-client', '0.11.1'
。
我已启用下列各项,% 1,已启用api访问% 2,服务帐户全域委派已完成% 3,已授权此服务帐户客户端id的作用域urls
代码可以与其他域一起使用,但对于其他帐户,它会抛出错误。
{
"error": "unauthorized_client",
"error_description": "Client is unauthorized to retrieve access tokens using this method."
}
代码:
SCOPES = ['https://www.googleapis.com/auth/admin.directory.user'
, 'https://www.googleapis.com/auth/admin.directory.group.readonly']
ENV['GOOGLE_APPLICATION_CREDENTIALS'] = 'expa-email-c2938bce41f9.json'
service = Google::Apis::AdminDirectoryV1::DirectoryService.new
authorization = Google::Auth.get_application_default(SCOPES)
authorization.sub = user_email
authorization.fetch_access_token!
service.authorization = authorization
service
发布于 2017-05-25 16:44:54
基于此thread,服务帐户是一个虚拟用户。要使服务帐户能够访问您的帐户,需要对其进行预授权。您可以像对任何其他用户一样对服务帐户进行授权。您可以检查此sample code。
其他参考资料:
服务帐户需要授权,否则无法访问该域的电子邮件。
“客户端无权使用此方法检索访问令牌”
表示您未对其进行适当的授权检查Delegating domain-wide authority to the service account
https://stackoverflow.com/questions/44178291
复制相似问题