Вопрос

I want to do the equivalent of e.g. this API explorer call using the google ruby API client.

That ought to be simply:

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])

However, that execute call results in ArgumentError: Missing access token.

I don't want to log users in; I only want to access public data. How do I do this?

Это было полезно?

Решение

I poked around in the client code and found a couple of options.

First, you can create an API client that will make all requests without an access token. The constructor is probably a bit more aggressive than it should be about specifying a default. To work around this you can nil out the authentication method after you've created the client. Your code will look like this:

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])

Alternatively, you can override the authentication method on a per-request basis. You were pretty close to this one! You had the correct option, you just need to pass it in as the final argument like this:

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])

Thanks to Allen for bringing this one to my attention on Google+ :)

Другие советы

The only thing that looks different than the sample ruby code at https://developers.google.com/+/api/latest/people/listByActivity#examples is the extra field 'authenticated' => 'false'.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top