Giter VIP home page Giter VIP logo

Comments (1)

mustmodify avatar mustmodify commented on May 30, 2024

Updated first step -- use the oauth2_state parameter when initializing AdManagerApi::Api. First step code below:

class GamAccessRequest
  CLIENT_ID = ENV['GAM_CLIENT_ID']
  SECRET_ID = ENV['GAM_SECRET_ID']
  APP_URL = some way to figure out 'https://www.whatever.com'

  def initialize(some_id: nil)
    @some_id = some_id
  end

  def valid?
    CLIENT_ID.present? && @some_id.present?
  end

  def params
    {
      method: 'OAuth2',
      oauth2_client_id: CLIENT_ID,
      oauth2_client_secret: SECRET_ID,
      oauth2_state: encoded_state,
      application_name: 'Google Ad Manager Client',

      # https://stackoverflow.com/a/10857806
      oauth2_prompt: 'consent',
      oauth2_access_type: 'offline'
    }
  end

  def ad_manager
    AdManagerApi::Api.new({
      :authentication => params,
      :service => {
        :environment => 'PRODUCTION'
      }
    })
  end

  def callback_url
    APP_URL + '/partners/google/auths/grant'
  end

  def wonky_url_acquisition
    begin
      ad_manager.authorize(oauth2_callback: callback_url)
    rescue AdsCommon::Errors::OAuth2VerificationRequired => e
      e.oauth_url.to_s
    end
  end

  def url
    @url ||= wonky_url_acquisition

    if @url.nil?
      Sentry.capture_message("Expecting a URL here but got none. params: #{params.to_json}")
    end

    @url
  end

  def state_data
    {network_id: @network_id}
  end

  def encoded_state
    encryptor = ActiveSupport::MessageEncryptor.new(SMALL_SECRET_KEY)
    secret = state_data.to_json
    encryptor.encrypt_and_sign(secret)
  end
end

from google-api-ads-ruby.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.