Android 使用 Ruby 验证 IAP 订阅服务器端

标签 android ruby-on-rails ruby in-app-purchase google-play

我一直在谷歌搜索和搜索,这让我很痛苦。我只是想设置我们的 RoR 服务器,以便能够查询 google play purchases API 以验证订阅是否已续订,但我似乎找不到实际的解决方案。我浏览了所有的谷歌文档。看来我需要一个服务帐户,如此处所述

https://developers.google.com/accounts/docs/OAuth2ServiceAccount

但后来我发现了这篇关于他们实际上希望我们如何使用 Web 服务器应用程序流程的 Python 文章

http://milancermak.wordpress.com/2012/08/24/server-side-verification-of-google-play-subsc/

此时我真的不在乎,我只需要让服务器成功与 Google API 对话以验证/续订订阅。我找到了 0 篇关于此流程如何工作的文章。有人得到这个工作吗??

最佳答案

对于偶然发现此问题的任何人来说,这是我曾经不得不处理的最疯狂的事情,但我设法弄清楚并正确地做到了。我要感谢以下文章帮助我缓慢但肯定地弄清了这个问题。我将添加其中最令人沮丧的部分是我将在下面列出的步骤,我不是 100% 相信它们都是必要的,因为在此期间我遇到的唯一错误是“403 权限不足”,所以我只能做这里告诉你我所做的一切,希望它对你有用。

这篇文章使一切正常,但使用的是网络访问方法并声明服务帐户无法正常工作

http://milancermak.wordpress.com/2012/08/24/server-side-verification-of-google-play-subsc/

这是对上述 Ruby 文章的一个很好的实现

https://gist.github.com/cornflakesuperstar/5632813

这是一篇关于如何获取 key 以便您可以将其存储为 Heroku 的环境变量的好文章

http://ar.zu.my/how-to-store-private-key-files-in-heroku/

这是一篇关于如何设置服务帐户的好文章(请注意,我不是我们服务的帐户所有者,所以我在告诉他如何执行此操作时是盲目的。这篇文章有效)

https://developers.google.com/console/help/#activatingapis

这篇文章是关于如何授权上面自动生成的电子邮件

How can I authorize with OAuth 2.0 for google's predictive API in Ruby?

API 的官方 Google 实现在这里

https://developers.google.com/android-publisher/api-ref/purchases/subscriptions/get

自从我发布这篇文章以来,经过无数个深夜,我能够通过以下步骤使用服务帐户进行此操作

  1. 我让我们的开发者控制台管理员为我生成了上述开发者文章中所述的服务帐户信息
  2. 我让我们的管理员添加电子邮件帐户并按照上面的堆栈溢出文章中的描述授予它权限
  3. 我关注了关于如何使 key 成为可以存储为字符串的字符串的文章

经过大量的反复试验,奇迹般地从 API 中获得了“200”。这是我的代码

ISSUER = '45366745684568-afdasfasdfasdfasdfasdf@developer.gserviceaccount.com'  # From service account
APP_NAME = '<appname>'  # This value didn't seem to matter.  I think it is for logging
APP_VERSION = '1.0'     # This value didn't seem to matter.  I think it is for logging

  class SomeClass < ActiveRecord::Base
   def self.google_api_client
    @@google_client ||= Google::APIClient.new(
      application_name:    APP_NAME,
      application_version: APP_VERSION
    ).tap do |client|

      # Load the key downloaded from the Google Developer Console
      if ENV['GOOGLE_API_KEY'].nil?
        puts "Be sure that you have ENV['GOOGLE_API_KEY'] defined in your environment."
        return
      end
      key = OpenSSL::PKey::RSA.new ENV['GOOGLE_API_KEY'], 'notasecret'

      # Initialize the connection
      client.authorization = Signet::OAuth2::Client.new(
        :token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
        :audience => 'https://accounts.google.com/o/oauth2/token',
        :scope => 'https://www.googleapis.com/auth/androidpublisher',
        :issuer => ISSUER,
        :signing_key => key)
      client.authorization.fetch_access_token!
    end
  end

  def self.test_server(package_name, subscription_id, token)
    # Get the client
    client = self.google_api_client

    # Discover the subscriptions API
    publisher = client.discovered_api('androidpublisher', 'v2')

    # Make the API call
    result = client.execute(
      :api_method => publisher.purchases.subscriptions.get,
      :parameters => {'packageName' => package_name, 'subscriptionId' => subscription_id, 'token' => token}
    )
  end
end

完成上述所有步骤后,我仍在苦苦挣扎(同样的 403 错误)。我意识到困扰我的是“范围”没有正确设置为“https://www.googleapis.com/auth/androidpublisher”。我希望这真的对某人有帮助。这让我心碎,现在它完美无缺。

感谢所有提到的文章对您的帮助。

关于Android 使用 Ruby 验证 IAP 订阅服务器端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25828491/

相关文章:

ruby - 将 shell 脚本源代码放入 ruby​​ 脚本中的环境中

ruby-on-rails - Rails 中的精确词搜索

android - android 中的 getResources() 遇到错误?

mysql - 在 Rails 应用程序的 MySQL 数据库中使用 TimeZone 正确存储日期

ruby-on-rails - 使用 FFaker::Internet.user_name 时指定最小长度

ruby-on-rails - rails : undefined method error when move codes from view to helper file

arrays - 从字符串中删除字母并仅保留数字

iphone - 如何使用 UDID 或设备 token 进行安全登录?

android - 如何将特定的、现有的、联系人的 ACCOUNT_TYPE 字段更改为 Google 之一,并使其同步?

android - 如何在安卓上使用ffmpeg