2

我正在使用 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"
 }
} 
4

1 回答 1

1

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

于 2013-03-27T22:31:55.450 回答