from google.oauth2 import service_account from googleapiclient.discovery import build import os # The service account JSON file you downloaded from Google Cloud Console SERVICE_ACCOUNT_FILE = "service_account.json" # The required scope for the Play Integrity API SCOPES = ["https://www.googleapis.com/auth/playintegrity"] def get_play_integrity_service(): """ Initializes the Play Integrity API client using a service account. """ credentials = service_account.Credentials.from_service_account_file( SERVICE_ACCOUNT_FILE, scopes=SCOPES ) # Build the Play Integrity API client service = build("playintegrity", "v1", credentials=credentials) return service def verify_integrity(token): """ Verifies the integrity token using the Play Integrity API. """ service = get_play_integrity_service() # Construct the request body for the verification API call request_body = {"integrityToken": token} # Call the Play Integrity API to verify the integrity token try: request = service.v1().decodeIntegrityToken( packageName=os.environ.get("PACKAGE_NAME"), body=request_body ) response = request.execute() # Print the response from the API print("Integrity Verification Response:", response) return response except Exception as e: print(f"Error verifying integrity token: {e}") return None if __name__ == "__main__": # Example token to verify (you would typically get this token from your Android app) example_token = os.environ.get("INTEGRITY_TOKEN") # Call the function to verify the token response = verify_integrity(example_token) if response: print("Verification successful!") else: print("Verification failed!")