Seb*_*ian 6 ruby ruby-on-rails oauth google-api
我正在尝试获取Google云端硬盘电子表格的内容,但我似乎找不到可以轻松完成的宝石.我尝试了google-drive-ruby,但它涉及到我必须从Google网站获取身份验证令牌的步骤.这不是很有帮助,因为我必须做所有这些服务器端.显然在以前版本的gem中有一个登录方法,但是被删除了.
有没有办法做到这一点?我是否必须使用OAuth获取令牌并将该令牌传递到google-drive-ruby?
Seb*_*ian 12
我设法得到了Reddit的答案.您需要一个服务帐户,并与服务应用程序的电子邮件共享该文档.p12键也必须存储在某处.
# gem install google-api-client -v 0.8.6 # upper versions are not compatible with this code
require "google/api_client"
@email = "email@developer.gserviceaccount.com"
@key = "path/to/key.p12"
key = Google::APIClient::KeyUtils.load_from_pkcs12(@key, 'notasecret')
auth = Signet::OAuth2::Client.new(
token_credential_uri: 'https://accounts.google.com/o/oauth2/token',
audience: 'https://accounts.google.com/o/oauth2/token',
scope: ["https://www.googleapis.com/auth/drive", "https://spreadsheets.google.com/feeds/"].join(' '),
issuer: @email,
access_type: 'offline',
signing_key: key
)
auth.fetch_access_token!
puts auth.access_token
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1331 次 |
| 最近记录: |