Json 谷歌API客户端Rails
我正在使用GoogleAPI客户端gemforRuby,每次调用api时都会出现403AccessNotConfigured错误Json 谷歌API客户端Rails,json,api,google-api,google-custom-search,google-api-ruby-client,Json,Api,Google Api,Google Custom Search,Google Api Ruby Client,我正在使用GoogleAPI客户端gemforRuby,每次调用api时都会出现403AccessNotConfigured错误 require 'google/api_client' client = Google::APIClient.new client.authorization = nil search = client.discovered_api('customsearch') response = client.execute( search.cse.list
require 'google/api_client'
client = Google::APIClient.new
client.authorization = nil
search = client.discovered_api('customsearch')
response = client.execute( search.cse.list, 'key' => '<<MY KEY>>', 'cx' => '<<MY CX>>', 'alt' => 'json', 'q' => 'hello world')
需要“google/api\u客户端”
client=Google::APIClient.new
client.authorization=nil
search=client.discovered\u api('customsearch'))
response=client.execute(search.cse.list,'key'=>'','cx'=>'','alt'=>'json','q'=>'hello world')
我尝试在不使用OAuth的情况下进行搜索,并且只使用API键
任何帮助都将不胜感激。谢谢大家! 未配置访问意味着它是一个API,需要在API控制台中注册您的项目,并从“服务”选项卡打开自定义搜索API。您的客户机由OAuth令牌或API密钥标识。必须提供一个或两个。我怀疑您只是未能在“服务”选项卡中打开API,因为您的示例包含API键参数 您还可以尝试更明确地传递参数:
result = client.execute(
:api_method => search.cse.list,
:key => '<<MY KEY>>',
:parameters => {
'cx' => '<<MY CX>>',
'alt' => 'json',
'q' => 'hello world'
}
)
result=client.execute(
:api_method=>search.cse.list,
:key=>“”,
:参数=>{
“cx'=>”,
'alt'=>'json',
“q”=>“你好,世界”
}
)
未配置访问意味着它是一种API,需要在API控制台中注册您的项目,并从“服务”选项卡启用自定义搜索API。您的客户机由OAuth令牌或API密钥标识。必须提供一个或两个。我怀疑您只是未能在“服务”选项卡中打开API,因为您的示例包含API键参数
您还可以尝试更明确地传递参数:
result = client.execute(
:api_method => search.cse.list,
:key => '<<MY KEY>>',
:parameters => {
'cx' => '<<MY CX>>',
'alt' => 'json',
'q' => 'hello world'
}
)
result=client.execute(
:api_method=>search.cse.list,
:key=>“”,
:参数=>{
“cx'=>”,
'alt'=>'json',
“q”=>“你好,世界”
}
)
我遇到了同样的问题,我这样解决了它:
谷歌账户设置:
# creating client instance
client = Google::APIClient.new
# authenticating
key = Google::APIClient::PKCS12.load_key("#{Rails.root}/config/<STRANGE_LONG_FILENAME>.p12", 'notasecret')
client.authorization = Signet::OAuth2::Client.new(
:token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
:audience => 'https://accounts.google.com/o/oauth2/token',
:scope => '<SCOPE_URL>',
:issuer => '<HASH>@developer.gserviceaccount.com',
:signing_key => key)
client.authorization.fetch_access_token!
# API call
# NOTE: Check the documentation for API methods and parameters). The method discovered_api returns a service object. We can use to_h.keys to get the list of available keys of that object. Keys represents API methods (e.g. "analytics.management.accounts.list" the API method path is "management.accounts.list").
result = client.execute(
:api_method => client.discovered_api('<SERVICE_NAME>', 'v3').management.accounts.list,
:parameters => { accountId: '~all', webPropertyId: '~all'}
)
if result.success?
result.data
end
# creating client instance
client = Google::APIClient.new
# authenticating
key = Google::APIClient::PKCS12.load_key("#{Rails.root}/config/<STRANGE_LONG_FILENAME>.p12", 'notasecret')
client.authorization = Signet::OAuth2::Client.new(
:token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
:audience => 'https://accounts.google.com/o/oauth2/token',
:scope => '<SCOPE_URL>',
:issuer => '<HASH>@developer.gserviceaccount.com',
:signing_key => key)
client.authorization.fetch_access_token!
# API call
# NOTE: Check the documentation for API methods and parameters). The method discovered_api returns a service object. We can use to_h.keys to get the list of available keys of that object. Keys represents API methods (e.g. "analytics.management.accounts.list" the API method path is "management.accounts.list").
result = client.execute(
:api_method => client.discovered_api('<SERVICE_NAME>', 'v3').management.accounts.list,
:parameters => { accountId: '~all', webPropertyId: '~all'}
)
if result.success?
result.data
end
#创建客户端实例
client=Google::APIClient.new
#鉴定
key=Google::APIClient::PKCS12.load_key(“#{Rails.root}/config/.p12”,“notasecret”)
client.authorization=Signet::OAuth2::client.new(
:令牌\凭证\ uri=>'https://accounts.google.com/o/oauth2/token',
:观众=>'https://accounts.google.com/o/oauth2/token',
:范围=>'',
:issuer=>“@developer.gserviceaccount.com”,
:signing_key=>key)
client.authorization.fetch\u access\u令牌!
#API调用
#注意:检查API方法和参数的文档)。api发现的方法返回一个服务对象。我们可以使用to_h.keys来获取该对象的可用键列表。键表示API方法(例如,“analytics.management.accounts.list”API方法路径为“management.accounts.list”)。
结果=client.execute(
:api_method=>client.discovered_api(“”,'v3').management.accounts.list,
:parameters=>{accountId:'~all',webPropertyId:'~all'}
)
如果结果是成功?
结果数据
结束
我遇到了同样的问题,我这样解决了它:
谷歌账户设置:
# creating client instance
client = Google::APIClient.new
# authenticating
key = Google::APIClient::PKCS12.load_key("#{Rails.root}/config/<STRANGE_LONG_FILENAME>.p12", 'notasecret')
client.authorization = Signet::OAuth2::Client.new(
:token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
:audience => 'https://accounts.google.com/o/oauth2/token',
:scope => '<SCOPE_URL>',
:issuer => '<HASH>@developer.gserviceaccount.com',
:signing_key => key)
client.authorization.fetch_access_token!
# API call
# NOTE: Check the documentation for API methods and parameters). The method discovered_api returns a service object. We can use to_h.keys to get the list of available keys of that object. Keys represents API methods (e.g. "analytics.management.accounts.list" the API method path is "management.accounts.list").
result = client.execute(
:api_method => client.discovered_api('<SERVICE_NAME>', 'v3').management.accounts.list,
:parameters => { accountId: '~all', webPropertyId: '~all'}
)
if result.success?
result.data
end