Google AdWords API 授权提高 AdsCommon::Errors::AuthError

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

我在 Rails 应用程序中配置了 Google 登录,登录工作正常,但是当我尝试自动执行 AdWords API 以获取令牌时,引发异常

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

这是授权的部分

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]

我正在使用登录: 设计 + omniauth-google-oauth2

我还添加了 adwords 范围

参见: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.