繁体   English   中英

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

[英]Android verify IAP subscription server side with Ruby

我一直在谷歌搜索和搜索这是杀了我。 我只是想设置我们的RoR服务器,以便能够查询谷歌游戏购买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 Insufficient Permissions”所以我真的可以做到这里告诉你我所做的一切,并希望它对你有用。

本文使一切正常,但使用Web访问方法并声明服务帐户不起作用

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

对于上面的Ruby文章,这是一个很好的实现

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

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

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

这是一篇关于如何设置服务帐户的好文章(请注意,我不是我们服务的帐户所有者,所以我不知道如何做到这一点。这篇文章有效)

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

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

如何使用OAuth 2.0为Ruby中的google预测API进行授权?

API的官方Google实施就在这里

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

自从我发布这篇文章以来无数深夜之后,我能够通过以下步骤使用服务帐户

  1. 我有我们的开发人员控制台管理员生成服务帐户信息,如上面的开发人员文章中所述
  2. 我让我们的管理员添加电子邮件帐户并允许其权限,如上面的堆栈溢出文章中所述
  3. 我按照文章介绍了如何使密钥成为可以存储为字符串的字符串

经过大量的试验和错误,奇迹般地从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 '。 我希望这真的能帮助别人。 这让我分崩离析,现在它完美无缺。

感谢所有上述文章的帮助。

以下是使用最新的google-api-client gem验证Google Play订阅当前是否有效的rails gist: https//gist.github.com/jkotchoff/e60fdf048ec443272045

该要点还包括有关如何创建可脱机使用的OAuth令牌的文档。

即。

  1. 访问http://console.developers.google.com
  2. API管理器
  3. 证书
  4. 创建凭据(OAuth客户端ID)
  5. 应用类型:Web应用程序
  6. 授权重定向URI: https//developers.google.com/oauthplayground
    • 生成的客户端ID /客户端密钥用于访问令牌
  7. 访问: https//developers.google.com/oauthplayground/
  8. 单击设置图标以显示OAuth 2.0配置
  9. 勾选'使用您自己的OAuth凭据'
  10. 输入您刚刚创建的OAuth客户端ID和OAuth客户端密钥
  11. 检查范围字段中的“Calendar API v3”条目,然后单击“授权API”
  12. 点击“允许”
  13. 点击“兑换令牌的授权码”
    • 现在,您的客户端ID /机密有一个刷新令牌和访问令牌

请注意,这个要点最近已升级为使用google-api-client gem的0.9.x版本。 对于使用该gem的弃用v0.8.x的实现,请参阅: https ://gist.github.com/jkotchoff/e60fdf048ec443272045/e3e2c867633900d9d6f53de2de13aa0a0a16bb03

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM