Post

RubyでGoogleplayのレシートを照合する

# アンドロイドのレシートチェック def check_receipt_for_android def fetch_receipt_for_android(product_id, purchase_token, token) require 'net/http' require 'net/https' require 'uri' require 'json' # path = "https://www.googleapis.com/androidpublisher/v2/applications/#{$ANDROID_PACKAGE_NAME}/purchases/products/#{product_id}/tokens/#{token}" path = "https://www.googleapis.com/androidpublisher/v1.1/applications/#{$ANDROID_PACKAGE_NAME}/inapp/#{product_id}/purchases/#{purchase_token}?access_token=#{token}" uri = URI.parse path https = Net::HTTP.new(uri.host,uri.port) https.use_ssl = true https.verify_mode = OpenSSL::SSL::VERIFY_NONE headers = {} headers["accept"] = "application/json" headers["content-type"] = "application/json" # 参考: https://stackoverflow.com/questions/7346398/nethttp-failure-to-access-google-docs-list-data-api https.start do |http| return http.get(uri.request_uri, headers).body end end # main # gemfile => gem 'google-api-client', '~> 0.8.6' require 'google/api_client' client = Google::APIClient.new( application_name: '適宜', application_version: '1.2.3', ) 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.eisbahn.jp/yoichiro/2011/10/oauth2-0_google-api.html client_id: $ANDROID_CLIENT_ID, client_secret: $ANDROID_CLIENT_SECRET, refresh_token: $ANDROID_REFRESH_KEY, ) p client.authorization.access_token #=> nil client.authorization.refresh! product_id = "product_id" purchase_token = "xxxxxxxxxxxx.xx-xxxx-kRhDk04x2ahVCKPxCDYAnxuxxxW1FX5kn8rEZf66d7BpItG411lyUr-xxxxxxxxxxxxxnBaDPZakoUBhUliHrbzwOT1ZoLvP-5uy7Fxii3DsxxxxxxMHf-n_N3c-ozSgxxxJ-LUXxx-bxxxf1Tw" receipt = get_android_refresh_token(product_id, purchase_token, client.authorization.access_token) pp JSON.parse(receipt, {symbolize_names: true}) end
APIに必要なもの($ANDROID_CLIENT_IDなどに代入する)を揃えたら、実行できる。
This post is licensed under CC BY 4.0 by the author.