标签: google-api-ruby-client

Google ruby​​ API客户端中的模拟方法

我正在尝试模拟一些使用google-api-ruby-client的方法来进行一些测试,而无需实际调用api。身份验证以及客户端和活动方法取自github页上的示例(请参见上面的链接),这就是为什么我在这里跳过了。

该示例中的方法如下:

def activities
  result = client.execute(
    :api_method => plus.activities.list,
    :parameters => {'collection' => 'public', 'userId' => 'me'}
  )
  return result.data
end
Run Code Online (Sandbox Code Playgroud)

我以前曾尝试对客户端方法(甚至与execute链接)进行存根处理,但是这导致了对oauth的授权请求,gem在其下使用了加号,然后在plus.activities.list方法中使用了模拟。有没有一种方法可以直接模拟client.exectute以在跳过整个链时返回有用的东西?

ruby rspec google-api-ruby-client

5
推荐指数
1
解决办法
1668
查看次数

NameError:未初始化的常量Faraday :: FlatParamsEncoder

我使用的google_drive gem取决于google-api-client,而gem版本是:

google_drive (1.0.1)
google-api-client (0.7.1)
faraday (0.8.9)
faraday_middleware (0.9.0)
Run Code Online (Sandbox Code Playgroud)

现在,使用GoogleDrive:

session = GoogleDrive.login_with_oauth('access_token')
Run Code Online (Sandbox Code Playgroud)

我收到以下异常:

NameError: uninitialized constant Faraday::FlatParamsEncoder
from /Users/Admin/.rvm/gems/ruby-1.9.3-p551@############/gems/google-api-client-0.7.1/lib/google/api_client.rb:118:in `block in initialize'
Run Code Online (Sandbox Code Playgroud)

检查我正在使用的google-api-client版本的代码,在这里:

https://github.com/google/google-api-ruby-client/blob/google-api-client-0.7.1/lib/google/api_client.rb

找不到任何异常.任何帮助将不胜感激.谢谢

ruby ruby-on-rails faraday google-api-ruby-client

5
推荐指数
0
解决办法
924
查看次数

Google Calendar API V3 insert_calendar返回503,但日历已成功插入

我遇到了insert_calendar向Google Calendar API V3 发送请求的问题,我收到了以下回复:

Sending HTTP post https://www.googleapis.com/calendar/v3/calendars?

503

#<HTTP::Message:0x000000124eb008 
  @http_header=#<HTTP::Message::Headers:0x000000124eafe0 
  @http_version="1.1", 
  @body_size=0, 
  @chunked=false,
  @request_method="POST", 
  @request_uri=#<Addressable::URI:0x9275f20 URI:https://www.googleapis.com/calendar/v3/calendars?>, 
  @request_query=nil, 
  @request_absolute_uri=nil, 
  @status_code=503, 
  @reason_phrase="Service Unavailable", 
  @body_type=nil, 
  @body_charset=nil, 
  @body_date=nil, 
  @body_encoding=#<Encoding:UTF-8>, 
  @is_request=false, 
  @header_item=[
    ["Vary", "Origin"], 
    ["Vary", "X-Origin"], 
    ["Content-Type", "application/json; charset=UTF-8"], 
    ["Content-Encoding", "gzip"], 
    ["Date", "Fri, 25 Aug 2017 20:16:34 GMT"], 
    ["Expires", "Fri, 25 Aug 2017 20:16:34 GMT"], 
    ["Cache-Control", "private, max-age=0"], 
    ["X-Content-Type-Options", "nosniff"], 
    ["X-Frame-Options", "SAMEORIGIN"], 
    ["X-XSS-Protection", "1; mode=block"], 
    ["Server", "GSE"], ["Alt-Svc", "quic=\":443\"; ma=2592000; v=\"39,38,37,35\""], 
    ["Transfer-Encoding", "chunked"]
  ], 
  @dumped=false>, 
  @peer_cert=#<OpenSSL::X509::Certificate: 
    subject=#<OpenSSL::X509::Name:0x00000012600998>, 
    issuer=#<OpenSSL::X509::Name:0x000000126009c0>, 
    serial=#<OpenSSL::BN:0x000000126009e8>, 
    not_before=2017-08-15 16:06:52 …
Run Code Online (Sandbox Code Playgroud)

calendar google-calendar-api google-api google-api-ruby-client

5
推荐指数
1
解决办法
328
查看次数

如何使用google-api-ruby-client使用Admin Directory api创建用户?

我一直在尝试一些组合,但似乎无法想出一些有用的东西.有关我要问的API的更多信息,请访问https://developers.google.com/admin-sdk/directory/v1/reference/users/insert.我有一种感觉,我只是没有正确设置请求.已知下面的代码可以工作.我用它来设置能够查询所有用户的客户端.

client = Google::APIClient.new(:application_name => "myapp", :version => "v0.0.0")
client.authorization = 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/admin.directory.user",
     :issuer => issuer,
     :signing_key => key,
     :person => user + "@" + domain)
client.authorization.fetch_access_token!
api = client.discovered_api("admin", "directory_v1")
Run Code Online (Sandbox Code Playgroud)

当我尝试使用以下代码时

parameters = Hash.new
parameters["password"] = "ThisIsAPassword"
parameters["primaryEmail"] = "tstacct2@" + domain
parameters["name"] = {"givenName" => "Test", "familyName" => "Account2"}
parameters[:api_method] = api.users.insert
response = client.execute(parameters)
Run Code Online (Sandbox Code Playgroud)

我总是得到相同的错误"代码":400,"消息":"无效的给定/姓氏:FamilyName"

在研究这个特定的API时,我发现了一些事情.如果我打印出列表和插入函数的参数,例如

puts "--- Users List ---"
puts api.users.list.parameters
puts "--- Users Insert …
Run Code Online (Sandbox Code Playgroud)

google-api-ruby-client google-admin-sdk

4
推荐指数
1
解决办法
1815
查看次数

使用ruby gmail api v0.9发送电子邮件

有没有人有一个简单的例子,说明如何使用v0.9 API从头开始发送电子邮件.

只想要一个发送以下内容的例子:

m = Mail.new(
to: "test1@test.com", 
from: "test2@test.com", 
subject: "Test Subject",
body:"Test Body")
Run Code Online (Sandbox Code Playgroud)

现在要创建需要发送的消息对象,我们可以使用:

msg = Base64.urlsafe_encode64 m.to_s
Run Code Online (Sandbox Code Playgroud)

然后尝试发送(其中message_object = msg):

client = Google::Apis::GmailV1::GmailService.new #Appropriately authorised
client.send_user_message("me", message_object)
Run Code Online (Sandbox Code Playgroud)

客户端需要RFC822兼容的编码字符串,以上应该是.

我试过了:

message_object = msg
=> Google::Apis::ClientError: invalidArgument: 'raw' RFC822 payload message string or uploading message via /upload/* URL required
message_object = raw:msg
=>ArgumentError: unknown keyword: raw
message_object = {raw:msg}
=>ArgumentError: unknown keyword: raw 
message_object = Google::Apis::GmailV1::Message.new(raw:msg)
=> #<Google::Apis::GmailV1::Message:0x007f9158e5b4b0 @id="15800cd7178d69a4", @thread_id="15800cd7178d69a4">
#But then I get Bounce <nobody@gmail.com> - An error …
Run Code Online (Sandbox Code Playgroud)

ruby gmail google-api google-api-ruby-client gmail-api

4
推荐指数
1
解决办法
886
查看次数


Ruby实时谷歌分析API

我试图通过google-api-ruby-client获得activeVisitors.客户端在此处列出了实时谷歌分析API文档,但我在文档中没有看到任何关于将其用于实时api的内容.

我看到函数discovered_api但是我没有看到API名称的posisble参数列表.

常规分析API示例:

# Get the analytics API
analytics = client.discovered_api('analytics','v3')
Run Code Online (Sandbox Code Playgroud)

有谁知道如何使用此客户端来获得实时活跃的访问者?

这是我尝试使用的代码:

require 'google/api_client'
require 'date'

# Update these to match your own apps credentials
service_account_email = 'xxxxxxxxxxxxx@developer.gserviceaccount.com' # Email of service account
key_file = '/path/to/key/privatekey.p12' # File containing your private key
key_secret = 'notasecret' # Password to unlock private key
profileID = '111111111' # Analytics profile ID.

# Get the Google API client
client = Google::APIClient.new(:application_name => '[YOUR APPLICATION NAME]',
:application_version => '0.01')

# …
Run Code Online (Sandbox Code Playgroud)

ruby google-analytics google-api-ruby-client

3
推荐指数
1
解决办法
2188
查看次数

谷歌加上插入活动

将活动插入谷歌加流的艰难时间.在引用谷歌开发者指南之后.我找到了一个java的例子 - https://developers.google.com/+/domains/posts/creating

是否有类似的示例activites.insert使用google-api-ruby-client执行查询.

我按照以下步骤:

通过omn​​iauth-google-oauth2定义对应用的访问权限

GOOGLE_CONSUMER_KEY      = google_config['KEY']
GOOGLE_CONSUMER_SECRET   = google_config['SECRET']
google_scope = "userinfo.email,
                userinfo.profile,
                plus.login,
                plus.me,
                plus.media.upload,
                plus.profiles.read,
                plus.stream.read,
                plus.stream.write,
                plus.circles.read,
                plus.circles.write"

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :google_oauth2, GOOGLE_CONSUMER_KEY, GOOGLE_CONSUMER_SECRET,
    {
        name: 'google',
      scope: google_scope,
      prompt: 'consent'
    }
end
Run Code Online (Sandbox Code Playgroud)

使用令牌和刷新令牌执行api调用google-api-ruby-client.能够使用"加号"列出活动,但是要插入一个我应该使用plusDomains的活动.

client = Google::APIClient.new(:application_name =>'Demo GooglePlus',:application_version => '1.0.0')

plus = client.discovered_api('plus')

plusd = client.discovered_api('plusDomain')

client_secrets = Google::APIClient::ClientSecrets.load 

auth=client_secrets.to_authorization

auth.update_token!(access_token: 'aRandomToken', refresh_token: 'aRandomRefreshToken')

result = client.execute(:api_method => plus.activities.list,:parameters => {'collection' => …
Run Code Online (Sandbox Code Playgroud)

google-api google-plus ruby-on-rails-3.2 ruby-2.0 google-api-ruby-client

3
推荐指数
1
解决办法
3702
查看次数

Google日历活动的#next_sync_token始终返回nil

我在Calendar中创建了3个事件,然后运行

response = @client.execute!(
   :api_method => @calendar_api.events.list,
   :parameters => {
     :calendarId => "primary",
     :maxResults => 10,
     :singleEvents => true,
     :orderBy => 'startTime'}
 )
Run Code Online (Sandbox Code Playgroud)

我收到3个项目但是#next_sync_token调用的方法response.data返回nil(#next_page_token也返回nil,但在这种情况下它是正确的).我试着将:fields参数设置为"nextSyncToken",但仍然没有.我也对这些事件做了一些更新,但#next_sync_token仍然没有.我错了吗?也许我犯了一些错误?仅使用calendarIdin参数的调用也是如此.

google-calendar-api google-api-ruby-client

3
推荐指数
1
解决办法
439
查看次数

google-api-ruby-client:请求序列化导致空请求

replacement_requests = [
  Google::Apis::DocsV1::ReplaceAllTextRequest.new(contains_text: "{{name}}", replace_text: "Joe"),
  Google::Apis::DocsV1::ReplaceAllTextRequest.new(contains_text: "{{age}}", replace_text: "34"),
  Google::Apis::DocsV1::ReplaceAllTextRequest.new(contains_text: "{{address}}", replace_text: "Westwood"),
]

batch_request = Google::Apis::DocsV1::BatchUpdateDocumentRequest.new(requests: replacement_requests)
Run Code Online (Sandbox Code Playgroud)

鉴于上述代码,当我将此 BatcUpdateDocumentRequest 实例传递到我的 service.batch_update_document 函数时,我收到一个 400 错误的请求。这似乎与批处理请求的序列化方式有关。

为了说明,如果我们打电话,batch_request.to_json我们会收到以下信息:

"{\"requests\":[{},{},{}]}"
Run Code Online (Sandbox Code Playgroud)

这告诉我在序列化过程中出了点问题,但是我的代码似乎相当规范。

关于为什么我的请求无法序列化的任何想法?

ruby google-api-ruby-client

3
推荐指数
1
解决办法
177
查看次数