我在获取用于实例化Drive Service帐户的示例代码时遇到了一些问题。我已按照指示在API控制台中设置了服务帐户,并包含'https://www.googleapis.com/auth/drive'的范围,但运行此操作会生成以下错误:“授权失败。服务器消息:(Signet :: AuthorizationError)”。Ruby中的Google Apps API和服务帐户出现问题
奇怪的是,如果我省略了user_email地址,它不会产生错误。
我的目标是能够对存储在组织驱动器上的所有文件进行审计,并且我的理解是使用服务帐户可以获取所有存储文件的列表。
我错过了服务器端的一些特殊设置吗?
require 'google/api_client'
## Email of the Service Account #
SERVICE_ACCOUNT_EMAIL = '<service account email>@developer.gserviceaccount.com'
## Path to the Service Account's Private Key file #
SERVICE_ACCOUNT_PKCS12_FILE_PATH = '<private key file>-privatekey.p12'
def build_client(user_email)
key = Google::APIClient::PKCS12.load_key(SERVICE_ACCOUNT_PKCS12_FILE_PATH, 'notasecret')
asserter = Google::APIClient::JWTAsserter.new(SERVICE_ACCOUNT_EMAIL, 'https://www.googleapis.com/auth/drive', key)
client = Google::APIClient.new
client.authorization = asserter.authorize(user_email)
return client
end
client = build_client("<users email address>")
有趣,而且看起来更清洁。我终于把它和我的代码一起工作了。我在某个阶段肯定有一个错字,但我发誓我会三重检查一切。 – JRQ
奇怪,因为在[Google文档在这里](https://developers.google.com/drive/web/delegation)显示的ruby示例看起来就像OP的。你有Signet方法的文档吗? – mmcrae
太棒了,这个答案在这里提示了一个方便的拉动请求! https://github.com/northworld/google_calendar/pull/95 –