简体   繁体   English

使用OmniAuth刷新令牌访问Google DFP API

[英]Accessing the Google DFP API with a OmniAuth refresh token

I am working on a rails app where I need to access users Google Double Click for Publisher Data. 我正在使用Rails应用程序,需要在其中访问用户Google Double Click for Publisher Data。 I am using the 'google-dfp-api' gem. 我正在使用“ google-dfp-api” gem。 I have set up OmniAuth for users to authenticate their DFP accounts and am storing the refresh token per the google documentation ( https://developers.google.com/doubleclick-publishers/docs/authentication ). 我已经为用户设置了OmniAuth,以验证其DFP帐户,并根据google文档( https://developers.google.com/doubleclick-publishers/docs/authentication )存储刷新令牌。 I can not figure out how to use the refresh token to access the DFP api. 我不知道如何使用刷新令牌来访问DFP广告管理系统API。

I am attempting to make the connection as shown below: 我正在尝试建立连接,如下所示:

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'
      }
    })

AnyTime I attempt to make a query after this I get the following error: 每当我尝试在此之后进行查询时,都会收到以下错误:

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

You do not use the refresh token to access the api, use your access_token. 您不使用刷新令牌来访问api,请使用access_token。 I am refreshing the access_token before I make a call. 在拨打电话之前,我正在刷新access_token。

Refresh your 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

Then the OAuth DFP API hash should look as below: 然后,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'
    }
  })

The user_agent is a unique string to your app, that should be the same every time you access the api. user_agent是应用程序的唯一字符串,每次访问api时都应相同。 See the link below for more info on that: 有关更多信息,请参见下面的链接:

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

You will also need to get the network_code from the the dfp account you are accessing by logging into the dfp account manually. 您还需要通过手动登录dfp帐户来从正在访问的dfp帐户中获取network_code。 To my knowledge this is not returned with OAuth authentication. 据我所知,OAuth身份验证未返回此内容。 See step 2 in the link below for more info on how to get the network_code: 有关如何获取network_code的更多信息,请参见下面的链接中的步骤2:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM