【问题标题】:Google adwords api how to authorize with service account impersonationGoogle adwords api如何通过服务帐户模拟进行授权
【发布时间】:2019-08-17 06:44:51
【问题描述】:

我正在尝试开发一个 ruby​​ 脚本来使用服务帐户凭据查询 Google Adwords API。我知道 json 凭证文件在另一个不是 ruby​​ 的脚本中工作,但我无法在这个 ruby​​ 脚本中获得过去的授权。我找不到任何使用这种模式的例子。我不想使用OAuth2。我知道这个脚本没有很好地开发,但我只是想了解基础知识。我在这里做错了什么?

我的公司有一个 G Suite 帐户,我拥有完整的管理员权限。

这是我的代码:

    #!/usr/bin/ruby

    require 'googleauth'
    require 'fileutils'
    require 'adwords_api'

    API_VERSION = :v201809

    scopes = ["https://www.googleapis.com/auth/adwords"]
    credential_file = File.open('credentials/adwords-production.json')
    prn = "adwords@mycompany.com"

    authorizer = Google::Auth::ServiceAccountCredentials.make_creds(json_key_io: credential_file, scope: scopes, prn: prn)

    def get_report_fields(report_type)

      adwords = AdwordsApi::Api.new

      report_def_srv = adwords.service(:ReportDefinitionService, API_VERSION)

      # Get report fields.
      fields = report_def_srv.get_report_fields(report_type)
      if fields
        puts "Report type '%s' contains the following fields:" % report_type
        fields.each do |field|
          puts ' - %s (%s)' % [field[:field_name], field[:field_type]]
          puts '  := [%s]' % field[:enum_values].join(', ') if field[:enum_values]
        end
      end
    end

    begin
        report_type = 'ACCOUNT_PERFORMANCE_REPORT'
        get_report_fields(report_type)
    end

【问题讨论】:

  • 好像你声明了authorizer,但从不使用。

标签: ruby google-ads-api


【解决方案1】:

来自 Google 的文档here

所有 AdWords API 调用必须通过 OAuth2 进行授权。 OAuth2 使您的 AdWords API 客户端应用能够访问用户的 Google Ads 帐户,而无需处理或存储用户的登录信息。

看来您必须使用OAuth2 身份验证。

进一步阅读here 证实了这一点:

您的应用需要代表您访问用户数据并联系其他 Google 服务。通过 OAuth2 进行的身份验证允许您的应用代表您的帐户运行。

要使您的应用能够访问 API,您需要 OAuth2 客户端 ID 和客户端密码。

您说您已经在其他可以使用 JSON 文件的地方执行此操作,所以我稍微研究了源代码。

Ruby API 的来源是herehere。似乎真的没有其他方法可以管理凭据,至少在 Ruby API 中是这样。看这里:

    # Retrieve correct soap_header_handler.
    #
    # Args:
    # - auth_handler: instance of an AdsCommon::Auth::BaseHandler subclass to
    #   handle authentication
    # - version: intended API version
    # - header_ns: header namespace
    # - default_ns: default namespace
    #
    # Returns:
    # - SOAP header handler
    #
    def soap_header_handler(auth_handler, version, header_ns, default_ns)
      auth_method = @config.read('authentication.method', :OAUTH2)
      handler_class = case auth_method
        when :OAUTH2, :OAUTH2_SERVICE_ACCOUNT
          AdsCommon::SavonHeaders::OAuthHeaderHandler
        else
          raise AdsCommon::Errors::AuthError,
              "Unknown auth method: %s" % auth_method
        end
      return handler_class.new(@credential_handler, auth_handler, header_ns,
                                  default_ns, version)
    end

请注意,除非使用 OAuth,否则它会引发错误。确实没有其他方法可以进行身份​​验证。即使您设法通过其他方式进行身份验证并尝试将其传递给凭据管理器,它也会拒绝它。

简短的回答:如果不以某种方式破解它,这在 Ruby API 中是不可能的。

【讨论】:

    猜你喜欢
    • 2020-02-22
    • 1970-01-01
    • 2013-08-21
    • 2020-01-06
    • 1970-01-01
    • 2018-07-07
    • 2014-12-18
    • 1970-01-01
    • 2014-02-01
    相关资源
    最近更新 更多