ruby-on-rails-4oauth-2.0google-oauthgoogle-ads-api

Google AdWords API authorization raising AdsCommon::Errors::AuthError


I configured Google login in a Rails app, the login works fine, but when I try to autorize the AdWords API to get token, raises an exception

AdsCommon::Errors::AuthError Authorization error occured: Authorization failed. 
Server message: 
{"error" : "invalid_grant", "error_description" : "Code was already redeemed."}

this is the part of autorization

def google_oauth2_callback
  api = AdwordsApi::Api.new(Rails.application.config_for(:adwords_api))
  session[:token] = api.authorize \
    oauth2_callback: user_omniauth_callback_url(:google_oauth2),
    oauth2_verification_code: params[:code]

I'm using for login: devise + omniauth-google-oauth2

And also I added the adwords scope


Solution

  • See: https://groups.google.com/forum/#!topic/adwords-api/1J8n4bUnyh4

    In case you use 3rd party lib to obtain access and refresh token, you don't need to use verification code again (it is already used and no longer valid).

    Instead, pass the refresh token directly to the library as:

    token = { :access_token=>"AbCdEf", :refresh_token=>"gHiJkL",
    :issued_at=>(time access token issued), :expires_in=>3600 } adwords.authorize({:oauth2_token => token})

    Note, you don't need to provide scope as it is already requested by you with omniauth.