Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby Google adwords api如何使用服务帐户模拟进行授权_Ruby_Google Ads Api_Adwords Api V201802 - Fatal编程技术网

Ruby Google adwords api如何使用服务帐户模拟进行授权

Ruby Google adwords api如何使用服务帐户模拟进行授权,ruby,google-ads-api,adwords-api-v201802,Ruby,Google Ads Api,Adwords Api V201802,我正在尝试开发一个ruby脚本来使用服务帐户凭据查询GoogleAdWordsAPI。我知道json凭证文件在另一个不是ruby的脚本中工作,但在这个ruby脚本中我无法通过授权。我找不到任何使用此模式的示例。我不想使用OAuth2。我知道这个脚本并没有很好的开发,但我只是想得到一些基础知识。我做错了什么 我的公司有一个G套件帐户,我有完全的管理员权限 这是我的密码: #!/usr/bin/ruby require 'googleauth' require 'fileu

我正在尝试开发一个ruby脚本来使用服务帐户凭据查询GoogleAdWordsAPI。我知道json凭证文件在另一个不是ruby的脚本中工作,但在这个ruby脚本中我无法通过授权。我找不到任何使用此模式的示例。我不想使用
OAuth2
。我知道这个脚本并没有很好的开发,但我只是想得到一些基础知识。我做错了什么

我的公司有一个G套件帐户,我有完全的管理员权限

这是我的密码:

    #!/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

来自谷歌的文档:

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

似乎您必须使用
OAuth2
身份验证

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

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

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

你说你在其他地方做过这个,在那里你可以使用JSON文件,所以我稍微研究了一下源代码

Ruby API的源代码是和。似乎真的没有其他方法来管理凭据,至少在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中,如果不对其进行某种破解,这是不可能的。

似乎您声明了
授权人
,但从未使用过。