简体   繁体   English

Google-api-client要求访问令牌

[英]Google-api-client asking for access token

My requirement is to get top 20 links for a search query in google.com. 我的要求是在google.com中获取搜索查询的前20个链接。 I am using the Google-api-client ! 我正在使用Google-api-client for ruby. 红宝石。

Here goes the code I am using, 这是我正在使用的代码,

require 'google/api_client'
client = Google::APIClient.new
response = client.execute(
    search.cse.list, 'key' => '<My Key>', 'cx' => '013036536707430787589%3A_pqjad5hr1a', 'alt' => 'json', 'q' => 'hello world'
)

Now I am facing three problems, 现在我面临三个问题,

  1. I want to use default Google search, so what should be the 'cx' value? 我想使用默认的Google搜索,那么“ cx”值应该是多少? One which I used, is from https://developers.google.com/custom-search/v1/using_rest#cx 我使用的一种是https://developers.google.com/custom-search/v1/using_rest#cx
  2. I am getting no results, instead getting the following warning "ArgumentError: Missing access token." 我没有任何结果,而是得到了以下警告“ ArgumentError:缺少访问令牌”。 I solved this issue using a dummy token, by defining "client.authorization.access_token = '123'" . 我通过定义“ client.authorization.access_token ='123'”使用虚拟令牌解决了此问题。 But I am not sure, if it is a correct solution or not. 但是我不确定这是否是正确的解决方案。
  3. After I define the access_token, still I am getting no result. 定义access_token之后,仍然没有任何结果。 Instead getting the warning "Invalid Credentials". 而是收到警告“无效凭据”。 But if I use the same URL(generated by the api), in the browser I am getting results. 但是,如果我使用相同的URL(由api生成),则在浏览器中会得到结果。

Instead of setting a dummy access token, just set the authorization mechanism to nil: 无需设置虚拟访问令牌,只需将授权机制设置为nil即可:

client.authorization = nil

This way it won't send an authorization header and will just rely on the API key for identifying your app. 这样,它就不会发送授权标头,而只会依靠API密钥来识别您的应用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails的google-api-client联系人api - rails google-api-client contact api Rails:设置google-api-client时出错 - Rails: error in setup google-api-client 从 google-api-client 0.8.3 迁移授权 Google::CalendarV3 - Migrating from google-api-client 0.8.3 Authorizing Google::CalendarV3 Rails Google Client API - 无法为访问令牌交换刷新令牌 - Rails Google Client API - unable to exchange a refresh token for access token 通过google-api-client gem使用Google自定义搜索引擎 - Using Google Custom Search Engine through the google-api-client gem Rails,OmniAuth,google_oauth2,google-api-client,Moments.insert ...... 401未经授权......为什么? - Rails, OmniAuth, google_oauth2, google-api-client, Moments.insert… 401 unauthorized… why? Google API客户端:令牌已被撤消 - Google API Client: Token has been revoked issue 客户端密钥与访问令牌Rails Instagram API - Client Secret vs Access Token Rails Instagram API Rails api不会通过devise令牌auth将访问令牌和客户端返回到外部URL - Rails api is not returning access-token and client to external url via devise token auth 无法获得Google AnalyticsAPI的oAuth2访问令牌 - Cannot get oAuth2 Access Token for Google Analytics API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM