Google Adwords API错误:无效的授权

问题描述:

我收到此错误,尝试使用服务帐户和JWT与Ruby API library通过Adwords API进行身份验证。Google Adwords API错误:无效的授权

我在复制example provided,但它似乎并不奏效。

/home/michael/.rvm/gems/ruby-2.1.2/gems/signet-0.5.1/lib/signet/oauth_2/client.rb:941:in`fetch_access_token':授权失败。服务器消息:(徽记:: AuthorizationError) { “错误”: “invalid_grant” }

adwords_api.yml

--- 
# This is an example configuration file for the AdWords API client library. 
# Please fill in the required fields, and copy it over to your home directory. 
:authentication: 
    # Authentication method, methods currently supported: OAUTH2, OAUTH2_JWT. 
    :method: OAUTH2_JWT 


    # Auth parameters for OAUTH2_JWT method. See: 
    # https://developers.google.com/accounts/docs/OAuth2ServiceAccount 
    :oauth2_issuer: 43242...apps.googleusercontent.com 
    :oauth2_secret: 'notasecret' 
    # You can provide path to a file with 'oauth2_keyfile' or the key itself with 
    # 'oauth2_key' option. 
    :oauth2_keyfile: /home/.../google-api-key.p12 
    # To impersonate a user set prn to an email address. 
    :oauth2_prn: [email protected] 

    # Other parameters. 
    :developer_token: ua...w 
    :client_customer_id: 123-123-1234 
    :user_agent: test-agent 
:service: 
    # Only production environment is available now, see: http://goo.gl/Plu3o 
    :environment: PRODUCTION 
:connection: 
    # Enable to request all responses to be compressed. 
    :enable_gzip: false 
    # If your proxy connection requires authentication, make sure to include it in 
    # the URL, e.g.: http://user:[email protected]_hostname:8080 
    # :proxy: INSERT_PROXY_HERE 
:library: 
    :log_level: INFO 

test.rb

#!/usr/bin/env ruby 

require 'adwords_api' 

def use_oauth2_jwt() 
    adwords = AdwordsApi::Api.new 

    adwords.authorize() 

    campaign_srv = adwords.service(:CampaignService, API_VERSION) 

    selector = { 
    :fields => ['Id', 'Name', 'Status'], 
    :ordering => [ 
     {:field => 'Name', :sort_order => 'ASCENDING'} 
    ] 
    } 

    response = campaign_srv.get(selector) 
    if response and response[:entries] 
    campaigns = response[:entries] 
    campaigns.each do |campaign| 
     puts "Campaign ID %d, name '%s' and status '%s'" % 
      [campaign[:id], campaign[:name], campaign[:status]] 
    end 
    else 
    puts 'No campaigns were found.' 
    end 
end 

if __FILE__ == $0 
    API_VERSION = :v201409 

    begin 
    use_oauth2_jwt() 

    # HTTP errors. 
    rescue AdsCommon::Errors::HttpError => e 
    puts "HTTP Error: %s" % e 

    # API errors. 
    rescue AdwordsApi::Errors::ApiException => e 
    puts "Message: %s" % e.message 
    puts 'Errors:' 
    e.errors.each_with_index do |error, index| 
     puts "\tError [%d]:" % (index + 1) 
     error.each do |field, value| 
     puts "\t\t%s: %s" % [field, value] 
     end 
    end 
    end 
end 

经过几个小时的摆弄之后,我最终通过将oauth2_prn设置为MCC和Google Apps for Business帐户的主电子邮件来完成工作。

这将是因为它是基于授权的,所以难以明确地回答,所以错误消息是荣耀的“未授权”消息。

所有我能做的,只能是表明几件事来检查(承认你可能通过这些去的话):

  • 您的开发人员令牌肯定显示为“已批准”? (你可以在客户端中心检查这一点 - 通过设置COG然后帐户,然后AdWords API中心)
  • 您已经通过Google Developer Console
  • 您(或您尝试访问该帐户的拥有者)已经批准注册的申请您的应用程序 - 可能是由以下this guide肯定看到这些东西在somepoint:

Google Auth Screen

如果已经检查了所有这些,那么唯一的其他东西我可以发uggest是official forum的帖子,他们往往很有帮助,并且经常会将“授权”问题视为“脱机”,以查看实际的soap请求等。(我发现这比通过AdWords级别更快更简单'support')

祝你好运!