我有問題來驗證谷歌任務的用戶。谷歌任務API認證問題ruby
起初它認證用戶並做完美的事情。但是在第二次旅行中會引發錯誤。
Signet::AuthorizationError - Authorization failed. Server message:
{
"error" : "invalid_grant"
}:
以下是代碼:
def api_client code=""
@client ||= (begin
client = Google::APIClient.new
client.authorization.client_id = settings.credentials["client_id"]
client.authorization.client_secret = settings.credentials["client_secret"]
client.authorization.scope = settings.credentials["scope"]
client.authorization.access_token = "" #settings.credentials["access_token"]
client.authorization.redirect_uri = to('/callbackfunction')
client.authorization.code = code
client
end)
end
get '/callbackfunction' do
code = params[:code]
c = api_client code
c.authorization.fetch_access_token!
result = c.execute("tasks.tasklists.list",{"UserId"=>"me"})
unless result.response.status == 401
p "#{JSON.parse(result.body)}"
else
redirect ("/oauth2authorize")
end
end
get '/oauth2authorize' do
redirect api_client.authorization.authorization_uri.to_s, 303
end
什麼是在進行第二次請求的問題?
更新: 這是用戶同意的鏈接和參數。
https://accounts.google.com/o/oauth2/auth?
access_type=offline&
approval_prompt=force&
client_id=somevalue&
redirect_uri=http://localhost:4567/oauth2callback&
response_type=code&
scope=https://www.googleapis.com/auth/tasks
我只是隨機地產生了這個異常 - 不得不啓動腳本五次,直到它工作。 – Nakilon