1

Google タスクのユーザー認証に問題があります。

最初に、ユーザーを認証し、完璧に処理します。しかし、2回目の旅行ではエラーがスローされます。

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

2 番目の要求を実行する際の問題は何ですか?

更新: これは、ユーザーの同意へのリンクとパラメーターです。

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
4

2 に答える 2

4

問題は修正されました。

解決:

コールバック関数では、ユーザーの同意によって提供されたコードを介して受け取ったトークンがデータベースに保存されます。

次に、他の関数でそれらのトークンをデータベースから取得し、Google タスク API に対して必要なものを処理するために使用します。

get '/callbackfunction' do
  code = params[:code]
  c = api_client code
  c.authorization.fetch_access_token!
  # store the tokens in the database.
end

get '/tasklists' do
  # Retrieve the codes from the database and create a client
  result = client.execute("tasks.tasklists.list",{"UserId"=>"me"})
  unless result.response.status == 401
    p "#{JSON.parse(result.body)}"
  else
    redirect "/oauth2authorize"
  end
end
于 2012-08-24T08:02:32.937 に答える
0

I am using rails, and i store the token only inside DB. then using a script i am setting up new client before calling execute, following is the code.

client = Google::APIClient.new(:application_name => 'my-app', :application_version => '1.0')
client.authorization.scope = 'https://www.googleapis.com/auth/analytics.readonly'
client.authorization.client_id = Settings.ga.app_key
client.authorization.client_secret = Settings.ga.app_secret
client.authorization.access_token = auth.token
client.authorization.refresh_token = true
client.authorization.update_token!({access_token: auth.token})

client.authorization.fetch_access_token!

if client.authorization.refresh_token && client.authorization.expired?
    client.authorization.fetch_access_token!
end

puts "Getting accounts list..."

result = client.execute(:api_method => analytics.management.accounts.list)
puts " ===========> #{result.inspect}"
items = JSON.parse(result.response.body)['items']

But,it gives same error you are facing,

    /signet-0.4.5/lib/signet/oauth_2/client.rb:875:in `fetch_access_token': Authorization  failed.  Server message: (Signet::AuthorizationError)
{
  "error" : "invalid_grant"
}
    from /signet-0.4.5/lib/signet/oauth_2/client.rb:888:in `fetch_access_token!'

Please suggest why it is not able to use the given token? I have used oauth2, so user is already authorized. Now i want to access the api and fetch the data...

===================UPDATE ===================

Ok, two issues were there,

  1. Permission is to be added to devise.rb,

    config.omniauth :google_oauth2, Settings.ga.app_key,Settings.ga.app_secret,{ 
      access_type: "offline", 
      approval_prompt: "" ,
      :scope => "userinfo.email, userinfo.profile, plus.me, analytics.readonly"
    

    }

  2. refresh_token must be passed to the API call, otherwise its not able to authorize.

I hope this helps to somebody, facing similar issue.

于 2013-07-31T12:56:22.053 に答える