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

发布于 2024-12-26 05:38:13 字数 1153 浏览 0 评论 0原文

我想做相当于 此 API 资源管理器调用,使用 google ruby​​ API 客户端

这应该很简单:

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。

不想让用户登录;我只想访问公共数据。我该怎么做?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倒数 2025-01-02 05:38:13

我浏览了客户端代码并找到了几个选项。

首先,您可以创建一个 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+ 上引起我的注意:)

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+ :)

早茶月光 2025-01-02 05:38:13

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'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文