【问题标题】:Accessing the Google DFP API with a OmniAuth refresh token使用 OmniAuth 刷新令牌访问 Google DFP API
【发布时间】:2015-01-22 22:38:37
【问题描述】:

我正在开发一个 Rails 应用程序,我需要在其中访问用户 Google Double Click 以获取发布商数据。我正在使用“google-dfp-api”gem。我已经为用户设置了 OmniAuth 来验证他们的 DFP 帐户,并按照谷歌文档 (https://developers.google.com/doubleclick-publishers/docs/authentication) 存储刷新令牌。我不知道如何使用刷新令牌来访问 DFP api。

我正在尝试建立如下所示的连接:

dfp = DfpApi::Api.new({
      :authentication => {
        :method => 'OAuth2',
        :oauth2_client_id => GOOGLE_CLIENT_ID,
        :oauth2_client_secret => GOOGLE_CLIENT_SECRET,
        :user_agent => USER_AGENT,
        :application_name => APP_NAME,
        :oauth2_token => {
          :refresh_token => GOOGLE_DFP_REFRESH_TOKEN
        }
      },
      :service => {
        :environment => 'PRODUCTION'
      }
    })

在此之后我尝试进行查询的任何时候都会收到以下错误:

DfpApi::V201411::UserService::ApiException: [AuthenticationError.AUTHENTICATION_FAILED @ ]

【问题讨论】:

  • 嘿,你解决了你的问题吗,我遇到了同样的错误

标签: ruby-on-rails omniauth google-dfp


【解决方案1】:

您不使用刷新令牌来访问 api,请使用您的 access_token。我在拨打电话之前正在刷新 access_token。

刷新你的令牌:

 def refresh_access_token
   refresh_token = self.refresh_token
   google_refresh_url = "https://accounts.google.com/o/oauth2/token"
   response = RestClient.post google_refresh_url, :grant_type => 'refresh_token', :refresh_token => refresh_token, :client_id => ENV['GOOGLE_CLIENT_ID'], :client_secret => ENV['GOOGLE_CLIENT_SECRET']
   response_hashed = JSON.parse(response)
   new_access_token = response_hashed['access_token']
   self.save_new_access_token(new_access_token)
 end

那么 OAuth DFP API 哈希应该如下所示:

@api = DfpApi::Api.new({
  :authentication => {
    :method => 'OAuth2',
    :oauth2_client_id => ENV['GOOGLE_CLIENT_ID'],
    :oauth2_client_secret => ENV['GOOGLE_CLIENT_SECRET'],
    :application_name => ENV['GOOGLE_APP_NAME'],
    :client_customer_id => google_dfp_credential.google_client_customer_id,
    :network_code => google_dfp_credential.network_code,
    :user_agent => ENV['GOOGLE_DFP_USER_AGENT'],
    :oauth2_token => {
      :access_token => google_dfp_credential.access_token,
     },
   },
   :service => {
     :environment => 'PRODUCTION'
    }
  })

user_agent 是您应用程序的唯一字符串,每次访问 api 时都应相同。有关详细信息,请参阅下面的链接:

http://googleadsdeveloper.blogspot.com/2013/11/please-set-user-agent-or-application.html

您还需要通过手动登录 dfp 帐户从您正在访问的 dfp 帐户中获取 network_code。据我所知,这不会通过 OAuth 身份验证返回。有关如何获取 network_code 的更多信息,请参阅下面链接中的第 2 步:

https://developers.google.com/doubleclick-publishers/docs/start

【讨论】:

    猜你喜欢
    • 2019-06-29
    • 1970-01-01
    • 2014-07-15
    • 2013-06-14
    • 2015-11-28
    • 2014-12-04
    • 1970-01-01
    • 2021-12-17
    • 2015-11-26
    相关资源
    最近更新 更多