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 に答える