在公共模式下访问Google+(Ruby)API

时间:2012-01-09 23:33:00

标签: google-api google-plus google-api-client

我想做相当于例如this API explorer call使用google ruby API client

那应该是简单的:

require 'rubygems'
require 'google/api_client'
require 'httpadapter/adapters/net_http'

@client = Google::APIClient.new(
  :key => MY_SIMPLE_API_SERVER_KEY,
  :host => 'www.googleapis.com',
  :http_adapter => HTTPAdapter::NetHTTPAdapter.new, 
  :pretty_print => false
)
@plus = @client.discovered_api('plus', 'v1')

status, headers, body = @client.execute(
  @plus.people.list_by_activity,
  'activityId' => 'z12nspyxxufislit423eex442zqpdtqnk',
  'collection' => 'plusoners',
  'maxResults' => '100',
  'authenticated' => 'false'
)
public_activity = JSON.parse(body[0])

但是,execute调用会产生ArgumentError: Missing access token.

想要登录用户;我只想访问 public 数据。我该怎么做?

2 个答案:

答案 0 :(得分:3)

我在客户端代码中搜索并发现了几个选项。

首先,您可以创建一个API客户端,该客户端将在没有访问令牌的情况下发出所有请求。构造函数可能比指定默认值更具攻击性。要解决此问题,您可以在创建客户端后清除身份验证方法。您的代码将如下所示:

require 'rubygems'
require 'google/api_client'
require 'httpadapter/adapters/net_http'

@client = Google::APIClient.new(
  :key => 'SOME_KEY',
  :host => 'www.googleapis.com',
  :http_adapter => HTTPAdapter::NetHTTPAdapter.new, 
  :pretty_print => false
)
@client.authorization = nil

@plus = @client.discovered_api('plus', 'v1')

status, headers, body = @client.execute(
  @plus.people.list_by_activity,
  'activityId' => 'z12nspyxxufislit423eex442zqpdtqnk',
  'collection' => 'plusoners',
  'maxResults' => '100'
)
public_activity = JSON.parse(body[0])

或者,您可以基于每个请求覆盖身份验证方法。你非常接近这个!你有正确的选择,你只需要把它作为最终的参数传递给你:

require 'rubygems'
require 'google/api_client'
require 'httpadapter/adapters/net_http'

@client = Google::APIClient.new(
  :key => 'SOME_KEY',
  :host => 'www.googleapis.com',
  :http_adapter => HTTPAdapter::NetHTTPAdapter.new, 
  :pretty_print => false
)
@plus = @client.discovered_api('plus', 'v1')

status, headers, body = @client.execute(
  @plus.people.list_by_activity,
  {'activityId' => 'z12nspyxxufislit423eex442zqpdtqnk',
  'collection' => 'plusoners',
  'maxResults' => '100'}, '', [], {:authenticated => false}
)
puts status
puts body
public_activity = JSON.parse(body[0])

感谢Allen在Google+上引起我的关注:)

答案 1 :(得分:0)

唯一看起来与https://developers.google.com/+/api/latest/people/listByActivity#examples上的示例红宝石代码不同的是额外字段'已验证' => '假'