簡體   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