我在获取用于实例化Drive Service帐户的示例代码时遇到了一些麻烦.我已按照指示在API控制台中设置服务帐户,并包含" https://www.googleapis.com/auth/drive " 的范围,但运行此帐户会生成以下错误:"授权失败.服务器消息:(Signet :: AuthorizationError)".
奇怪的是,如果我省略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>")
Run Code Online (Sandbox Code Playgroud) 这可能是一个非常愚蠢的问题,但是如何确定在客户端"discovered_api"调用中传递的参数?什么是执行的"api_method"
例如,我正在尝试调用"GET https://www.googleapis.com/admin/directory/v1/users " 的Admin SDK用户列表调用.
似乎没有一种明确的方法从API参考中提取这个,或者我只是在错误的地方寻找?
我一直在尝试找出如何将收件人自动添加到通过其Ruby库使用Gmail API创建的草稿电子邮件中。我可以毫无问题地创建草稿,但设置收件人会给我造成麻烦,而且我还找不到任何显示添加电子邮件特定内容的最佳方法的良好示例。
使用Google API操场并拉入已经创建的草稿,看起来结构应类似于下面所示,但是一旦创建草稿,便没有收件人。
@result = client.execute(
:api_method => gmail.users.drafts.create,
:parameters => {
'userId' => "me"
},
:body_object => {
'message' => {
'raw' => Base64.urlsafe_encode64('Test Email Message'),
'payload' => {
'headers' =>
[
{
'name' => "To",
'value' => "John Smith <john_smith.fake@gmail.com>"
}
]
}
}
}
)
Run Code Online (Sandbox Code Playgroud) 我正在使用google api ruby 客户端将 webhook 添加到日历中,但我遇到了Google::Apis::ClientError: pushWebhookBadDomain.
我已经设置了一个 ngrok 实例并在网站管理员工具上验证了它,以便我可以将其作为允许的域添加到处理所有日历身份验证等的项目中。我找不到任何对该特定错误的引用,所以我'我想知道使用 ngrok 是否有什么奇怪的地方,以及是否有更好的方法可以在本地测试 webhook。
这是为了防止我做任何愚蠢的事情而进行的通话(为了清晰起见,进行了精简):
require 'google/apis/calendar_v3'
client = Google::Apis::CalendarV3::CalendarService.new
authorization = Signet::OAuth2::Client.new(
:authorization_uri =>
'https://accounts.google.com/o/oauth2/auth',
:token_credential_uri =>
'https://accounts.google.com/o/oauth2/token'
)
authorization.client_id = ENV['GOOGLE_CLIENT_ID']
authorization.client_secret = ENV['GOOGLE_CLIENT_SECRET']
authorization.grant_type = 'refresh_token'
authorization.refresh_token = refresh_token
authorization.fetch_access_token!
client.authorization = authorization
channel = Google::Apis::CalendarV3::Channel.new(address: success_callback_url, id: channel_id, type: "web_hook")
binding.pry
webhook = client.watch_event('primary', channel, single_events: true, time_min: Time.now.iso8601)
Run Code Online (Sandbox Code Playgroud) ruby-on-rails google-calendar-api google-api-client google-api-ruby-client
红宝石:2.3.1
导轨:5.0.0
我需要通过 Google Sheets API 访问和更新我的私人 Google Sheet。最终这将通过我的 Rails 应用程序中的重复后台作业来完成。因此,我在 Google Cloud Console 中设置了一个服务帐户。
但是要开始使用 Google Sheets API,我创建了一个脚本:
require 'google/apis/sheets_v4'
ENV["GOOGLE_ACCOUNT_TYPE"] = 'service_account'
ENV["GOOGLE_CLIENT_EMAIL"] = '<client_email_from_downloaded_json_here>'
ENV["GOOGLE_PRIVATE_KEY"] = "<private_key_from_downloaded_json_here>"
SCOPE = Google::Apis::SheetsV4::AUTH_SPREADSHEETS
authorization = Google::Auth.get_application_default(SCOPE)
# Initialize the API
service = Google::Apis::SheetsV4::SheetsService.new
service.authorization = authorization
spreadsheet_id = '<MY SPREADSHEET ID HERE>'
sheet_name = 'Sheet1'
range = "#{sheet_name}!A2:B"
response = service.get_spreadsheet_values(spreadsheet_id, range)
puts 'No data found.' if response.values.empty?
response.values.each do |row|
# Print columns A and B, which correspond to …Run Code Online (Sandbox Code Playgroud) ruby-on-rails google-oauth google-api-ruby-client google-sheets-api
我正在使用(alpha)Ruby google-api-client与我们组织的Google Apps实例的各种服务进行交互.
我想发现配置服务 API,使用服务帐户验证用户身份,并更新其密码.
至今...
require 'google/api_client'
class GoogleProvisioningConnection
def initialize(user_email=nil)
@client = Google::APIClient.new
@provisioning = @client.discovered_api('???', 'v2') # what's it called? user?
key_file_name = 'mykey-privatekey.p12'
key = Google::APIClient::PKCS12.load_key(key_file_name, 'notasecret')
asserter = Google::APIClient::JWTAsserter.new(
'...@developer.gserviceaccount.com',
'???', # which url allows me access to their user?
key)
@client.authorization = asserter.authorize(user_email)
end
end
Run Code Online (Sandbox Code Playgroud)
使用哪个字符串@client.discovered_api来获取配置API?
当使用JWT断言器时,应该请求哪个服务URL?
谢谢
ruby google-api google-apps google-provisioning-api google-api-ruby-client