访问 Google+ (Ruby) 公共模式下的 API
我想做相当于 此 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我浏览了客户端代码并找到了几个选项。
首先,您可以创建一个 API 客户端,该客户端将在没有访问令牌的情况下发出所有请求。构造函数可能比指定默认值更激进一些。要解决此问题,您可以在创建客户端后取消身份验证方法。您的代码将如下所示:
或者,您可以根据每个请求重写身份验证方法。你和这个人很接近!您有正确的选项,只需将其作为最终参数传递即可,如下所示:
感谢 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:
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:
Thanks to Allen for bringing this one to my attention on Google+ :)
唯一看起来与 https://developers 上的示例 ruby 代码不同的地方.google.com/+/api/latest/people/listByActivity#examples 是额外字段 'authenticated' => '错误的'。
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'.