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