gpt4 book ai didi

google-api - 在公共(public)模式下访问 Google+ (Ruby) API

转载 作者:行者123 更新时间:2023-12-04 19:57:28 26 4
gpt4 key购买 nike

我想做相当于例如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)数据。我该怎么做呢?

最佳答案

我在客户端代码中四处寻找,发现了几个选项。

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

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+ 上注意到这一点 :)

关于google-api - 在公共(public)模式下访问 Google+ (Ruby) API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8796696/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com