ruby - 为什么在使用 Ruby 执行 Google Fusion Tables API 命令时会出现 401 错误?

标签 ruby google-api oauth-2.0 google-api-client google-api-ruby-client

我正在使用 Ruby gem“google-api-client”,并确保添加我的所有凭据,但出于某种原因,我不断收到来自 Google 的 401 错误。

这是我的代码:

['google/api_client','json'].each{|g| require g}

client_secrets = File.open('client_secrets.json','r').read # My client secrets are stored in this .JSON file
client_secrets_hsh = JSON.parse(client_secrets)

client = Google::APIClient.new
client.authorization.client_id = client_secrets_hsh['installed']['client_id']
client.authorization.client_secret = client_secrets_hsh['installed']['client_secret']
client.authorization.redirect_uri = client_secrets_hsh['installed']['redirect_uris'][0]
client.authorization.access_token = 'MY_ACCESS_TOKEN'
client.authorization.username = 'MY_USER_NAME@gmail.com'
client.authorization.password = 'MY_GOOGLE_ACCOUNT_PASSWORD'
client.authorization.scope = 'https://www.googleapis.com/auth/fusiontables'

request_body = ''
headers = []
headers << ['Content-Type','application/json']

api = client.discovered_api('fusiontables','v1')

result = client.execute(
    :api_method => api.to_h['fusiontables.table.list'],
    :parameters => {'maxResults' => 1000},
    :merged_body => request_body,
    :headers => headers
)

puts result.response.body

这是我从 puts results.response.body 行得到的响应:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "authError",
    "message": "Invalid Credentials",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Invalid Credentials"
 }
} 

最佳答案

不应提供用户名和密码。尝试放下那些开始。您只需要访问 token 即可进行 API 调用。顺便说一句,我不确定你是从哪里得到的。通常您需要在某处调用 fetch_access_token!。它通常不是您通过访问器设置的内容。在某些高级案例中,您可能会这样做——但您可能不是其中之一。

关于ruby - 为什么在使用 Ruby 执行 Google Fusion Tables API 命令时会出现 401 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15642905/

相关文章:

android - 为 android 应用程序的 linkedin 登录生成调试 key 哈希值

security - 没有浏览器的独立应用授权?

Ruby 未定义方法 '+' 为 nil :NilClass

ruby - 使用 RVM 的 Hadoop Streaming 找不到 Gem

ruby - 用 Cucumber 编写 Ruby gem 规范

ruby-on-rails - Rails 使用 API

javascript - 监控 Google 电子表格单元格值并撤消不需要的更改

android - 如何在 android 中使用访问 token 和 api 在 youtube 上上传视频?

java - 如何在 Calendar Api 中禁用 Google 的 “Video Call” 默认值?

Android:GoogleAuthUtil.getToken - 帐户对象应该来自哪里?