Authenticate requests to the Google Wallet API

  • Google Wallet API requests require authentication to verify your application's identity.

  • Before starting, generate and register credentials for the Google Wallet REST API and Android SDK.

  • REST API requests use a Google Cloud service account key, while the Android SDK uses your app signing certificate for authentication.

  • Refer to provided code samples for various programming languages to implement authentication.

Requests to the Google Wallet API must be authenticated so that the Wallet API can identify that the request is being made by your application.

Before you begin, ensure you have properly generated and registered your credentials for the Google Wallet REST API and Google Wallet Android SDK.

Google Wallet REST API requests

Request to the Google Wallet REST API are authenticated using a Google Wallet REST API Google Cloud service account key to obtain an access token.

The Google API Client Libraries handle authentication and making API requests. Make sure you install the library for your preferred programming language, which the following examples use.

First, perform the necessary library imports and define some variables for the service account JSON, and IDs for the issuer, class, unique user and object that will be saved.

Java

To start your integration in Java, refer to our complete code samples on GitHub.

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.google.api.client.googleapis.batch.BatchRequest;
import com.google.api.client.googleapis.batch.json.JsonBatchCallback;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.googleapis.json.GoogleJsonError;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.http.*;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.services.walletobjects.*;
import com.google.api.services.walletobjects.model.*;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.auth.oauth2.ServiceAccountCredentials;
import java.io.*;
import java.security.interfaces.RSAPrivateKey;
import java.util.*;

/** Demo class for creating and managing Flights in Google Wallet. */
public class DemoFlight {
  /**
   * Path to service account key file from Google Cloud Console. Environment variable:
   * GOOGLE_APPLICATION_CREDENTIALS.
   */
  public static String keyFilePath;

  /** Service account credentials for Google Wallet APIs. */
  public static GoogleCredentials credentials;

  /** Google Wallet service client. */
  public static Walletobjects service;

  public DemoFlight() throws Exception {
    keyFilePath =
        System.getenv().getOrDefault("GOOGLE_APPLICATION_CREDENTIALS", "/path/to/key.json");

    auth();
  }

PHP

To start your integration in PHP, refer to our complete code samples on GitHub.

use Firebase\JWT\JWT;
use Google\Auth\Credentials\ServiceAccountCredentials;
use Google\Client as GoogleClient;
use Google\Service\Walletobjects;
use Google\Service\Walletobjects\ReservationInfo;
use Google\Service\Walletobjects\BoardingAndSeatingInfo;
use Google\Service\Walletobjects\LatLongPoint;
use Google\Service\Walletobjects\Barcode;
use Google\Service\Walletobjects\ImageModuleData;
use Google\Service\Walletobjects\LinksModuleData;
use Google\Service\Walletobjects\TextModuleData;
use Google\Service\Walletobjects\TranslatedString;
use Google\Service\Walletobjects\LocalizedString;
use Google\Service\Walletobjects\ImageUri;
use Google\Service\Walletobjects\Image;
use Google\Service\Walletobjects\FlightObject;
use Google\Service\Walletobjects\Message;
use Google\Service\Walletobjects\AddMessageRequest;
use Google\Service\Walletobjects\Uri;
use Google\Service\Walletobjects\AirportInfo;
use Google\Service\Walletobjects\FlightCarrier;
use Google\Service\Walletobjects\FlightClass;
use Google\Service\Walletobjects\FlightHeader;

/** Demo class for creating and managing Flights in Google Wallet. */
class DemoFlight
{
  /**
   * The Google API Client
   * https://github.com/google/google-api-php-client
   */
  public GoogleClient $client;

  /**
   * Path to service account key file from Google Cloud Console. Environment
   * variable: GOOGLE_APPLICATION_CREDENTIALS.
   */
  public string $keyFilePath;

  /**
   * Service account credentials for Google Wallet APIs.
   */
  public ServiceAccountCredentials $credentials;

  /**
   * Google Wallet service client.
   */
  public Walletobjects $service;

  public function __construct()
  {
    $this->keyFilePath = getenv('GOOGLE_APPLICATION_CREDENTIALS') ?: '/path/to/key.json';

    $this->auth();
  }

Python

To start your integration in Python, refer to our complete code samples on GitHub.

import json
import os
import uuid

from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from google.oauth2.service_account import Credentials
from google.auth import jwt, crypt


class DemoFlight:
    """Demo class for creating and managing Flights in Google Wallet.

    Attributes:
        key_file_path: Path to service account key file from Google Cloud
            Console. Environment variable: GOOGLE_APPLICATION_CREDENTIALS.
        base_url: Base URL for Google Wallet API requests.
    """

    def __init__(self):
        self.key_file_path = os.environ.get('GOOGLE_APPLICATION_CREDENTIALS',
                                            '/path/to/key.json')
        # Set up authenticated client
        self.auth()

C#

To start your integration in C#, refer to our complete code samples on GitHub.