我正在为 ruby 使用 google api,但不知道如何开始,请给我一个 ABC 示例,非常感谢?
最佳答案
如果您要创建服务帐户应用程序以访问 Google Analytics。
- 通过 https://code.google.com/apis/console 向 Google 注册. 在 API Access 选项卡上,单击 Create client ID,选择 Service Account。存储 Google 将生成的 key 文件,并记住该 key 的密码。
这里有一些代码可以帮助您入门
require 'rubygems' require 'google/api_client' api_client = Google::APIClient.new path_to_key_file ="/path/to/key/file-privatekey.p12" passphrase = "google_generated_password" key = Google::APIClient::PKCS12.load_key(path_to_key_file, passphrase)
一旦 key 可用,使用您的客户端 ID(API 控制台中的电子邮件)初始化断言者 和授权范围。
asserter = Google::APIClient::JWTAsserter.new(
'super_long_client_id_from_api_console@developer.gserviceaccount.com',
'https://www.googleapis.com/auth/analytics.readonly',
key)
# To request an access token, call authorize:
api_client.authorization = asserter.authorize()
puts api_client.authorization.access_token
http://code.google.com/p/google-api-ruby-client/wiki/ServiceAccounts
关于ruby - 如何授权使用 google-api-ruby-client,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7493499/