Google adwords api如何通过服务帐户模拟进行授权

问题描述 投票:1回答:1

我正在尝试使用服务帐户凭据开发一个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 = "[email protected]"

    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

ruby google-adwords adwords-api-v201802
1个回答
1
投票

来自Google的文档here

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

看来你必须使用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中就不可能实现这一点。

© www.soinside.com 2019 - 2024. All rights reserved.