Search code examples
ruby-on-railsrubygoogle-apigoogle-play-servicesgoogle-play-developer-api

I don't know why I get a 400 error with decode integrity token


I am using the play integrity of ruby's library google-api-ruby-client which decrypts and google-apis-playintegrity_v1 results for Android but it is returning a 400 error.

I tried to find the cause of the problem by referring to this article and others. I would like to get your advice.

The source code being implemented is as follows

# Gemfile
gem "google-apis-playintegrity_v1"
...



# API executable file
require 'google/apis/playintegrity_v1'

class AndroidRightfulnessCheck
  class << self
    def device_rightfulness_check( integrity_token: token)
      play_integrity = Google::Apis::PlayintegrityV1
      decode_integrity_token_request = play_integrity::DecodeIntegrityTokenRequest.new
      play_integrity_service = play_integrity::PlayIntegrityService.new
      cred = Google::Auth::DefaultCredentials.make_creds(json_key_io: StringIO.new(ENV["PRIVATE_KEY"]), scope: "https://www.googleapis.com/auth/playintegrity")
      play_integrity_service.authorization = cred

      integrity_token = decode_integrity_token_request.update!(integrity_token: integrity_token)
      payload = play_integrity_service.decode_integrity_token(package_name, integrity_token)
    end
  end
end

result

Error - #<Google::Apis::ClientError: Invalid request status_code: 400 header: #<HTTP::Message::Headers:0x000055d11d04b098 @http_version="1.1"
, @body_size=0
, @chunked=false
, @request_method="POST"
, @request_uri=#<Addressable::URI:0x2ae88e7fa714 URI:https://playintegrity.googleapis.com/v1/package_name:decodeIntegrityToken?>
, @request_query=nil
, @request_absolute_uri=nil
, @status_code=400
, @reason_phrase="Bad Request"
, @body_type=nil
, @body_charset=nil
, @body_date=nil
, @body_encoding=#<Encoding:UTF-8>
, @is_request=false
...
body: "{\n  \"error\": {\n    \"code\": 400
,\n    \"message\": \"Integrity token cannot be decoded due to invalid arguments.\"
,\n    \"status\": \"INVALID_ARGUMENT\"\n  }\n}\n">

Documents referenced

https://www.rubydoc.info/gems/google-apis-playintegrity_v1/0.5.0/Google/Apis/PlayintegrityV1/PlayIntegrityService https://www.rubydoc.info/gems/google-apis-playintegrity_v1/0.5.0/Google/Apis/PlayintegrityV1/DecodeIntegrityTokenRequest


Solution

  • Resolved.

    It was necessary to specify the decode_integrity_token_request after the update as an argument, not the updated token.

    After modification

    
    
    # Gemfile
    gem "google-apis-playintegrity_v1"
    ...
    
    
    
    # API executable file
    require 'google/apis/playintegrity_v1'
    
    class AndroidRightfulnessCheck
      class << self
        def device_rightfulness_check( integrity_token: token)
          play_integrity = Google::Apis::PlayintegrityV1
          decode_integrity_token_request = play_integrity::DecodeIntegrityTokenRequest.new
          play_integrity_service = play_integrity::PlayIntegrityService.new
          cred = Google::Auth::DefaultCredentials.make_creds(json_key_io: StringIO.new(ENV["PRIVATE_KEY"]), scope: "https://www.googleapis.com/auth/playintegrity")
          play_integrity_service.authorization = cred
    
          decode_integrity_token_request.update!(integrity_token: integrity_token) #changed
          payload = play_integrity_service.decode_integrity_token(package_name, decode_integrity_token_request) #changed
        end
      end
    end