ruby - Google AdWords API 错误 : invalid grant

标签 ruby google-ads-api jwt

我在尝试使用服务帐户和带有 Ruby API library 的 JWT 通过 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':授权失败。服务器消息:(Signet::AuthorizationError) { “错误”:“无效授权” }

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: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9bf6e2dbfef6faf2f7b5f8f4f6" rel="noreferrer noopener nofollow">[email protected]</a>

  # 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:password@proxy_hostname:8080
  # :proxy: INSERT_PROXY_HERE
:library:
  :log_level: INFO

测试.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

最佳答案

这个问题很难明确回答,因为它是基于授权的,因此错误消息是一条美化的“未经授权”消息。

我真正能做的就是建议一些需要检查的事情(承认您可能已经经历过这些):

  • 您的开发者 token 确实显示为“已批准”? (您可以在客户中心检查这一点 - 通过设置 cog、帐户、adwords api 中心)
  • 您已通过 Google Developer Console 注册了申请
  • 您(或您尝试访问的帐户的所有者)已授权您的应用程序 - 可能通过以下方式 this guide并且肯定在某个时刻看到以下事情之一:

Google Auth Screen

如果您已经检查了所有这些,那么我唯一可以建议的就是在 official forum 上发帖。他们往往会提供帮助,并且经常将授权问题“离线”,以查看实际的 SOAP 请求等。(我发现这比尝试浏览 AdWords“支持”级别更快、更容易)

祝你好运!

关于ruby - Google AdWords API 错误 : invalid grant,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27066634/

相关文章:

javascript - 将 Google Adwords 脚本添加到 Vaadin 应用程序中

node.js - Node Passport 自定义回调作为中间件是否可以用于快速路由?

jwt - Keycloak 如何在 id token 中编码组成员身份

ruby-on-rails - Time.use_zone 未按预期工作

python - Mechanize 响应中没有 Content-Disposition header

css - Rails Bootstrap 字形图标

css - 如何根据 gclid 参数隐藏 header ?

reactjs - 为什么发布请求在开发模式下有效,但在生产模式或测试模式下不起作用?它返回未定义的 token

ruby-on-rails - 将哈希排序为 xml : Rails

ruby - 为什么ruby中有类变量?