2

我使用Ruby gem'google-api-client',並且確保添加了我的所有憑據,但由於某種原因,我一直收到來自Google的401錯誤。使用Ruby執行Google Fusion Tables API命令時,爲什麼會出現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 = '[email protected]' 
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" 
} 
} 

回答

1

用戶名和密碼不應該被提供。嘗試刪除這些開始。您只需要訪問令牌即可進行API調用。順便說一句,我不確定你從哪裏得到這些信息。通常你需要撥打fetch_access_token!的地方。它通常不是通過訪問器設置的。有些先進的案例你會 - 但你可能不是其中之一。

相關問題