Authenticate requests to the Google Wallet API

  • Google Wallet API requests must be authenticated using a Google Cloud service account key.

  • The Google API Client Libraries handle authentication and API requests, and must be installed before use.

  • For REST API requests, Google provides client libraries for Java, PHP, Python, C#, Node.js, and Go.

  • Google Wallet Android SDK requests are automatically authenticated using your app signing certificate, for which the SHA-1 fingerprint will be included in requests to the Google Wallet API.

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

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.

importcom.auth0.jwt.JWT;
importcom.auth0.jwt.algorithms.Algorithm;
importcom.google.api.client.googleapis.batch.BatchRequest;
importcom.google.api.client.googleapis.batch.json.JsonBatchCallback;
importcom.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
importcom.google.api.client.googleapis.json.GoogleJsonError;
importcom.google.api.client.googleapis.json.GoogleJsonResponseException;
importcom.google.api.client.http.*;
importcom.google.api.client.json.gson.GsonFactory;
importcom.google.api.services.walletobjects.*;
importcom.google.api.services.walletobjects.model.*;
importcom.google.auth.http.HttpCredentialsAdapter;
importcom.google.auth.oauth2.GoogleCredentials;
importcom.google.auth.oauth2.ServiceAccountCredentials;
importjava.io.*;
importjava.security.interfaces.RSAPrivateKey;
importjava.util.*;
/** Demo class for creating and managing Offers in Google Wallet. */
publicclass DemoOffer{
/**
 * Path to service account key file from Google Cloud Console. Environment variable:
 * GOOGLE_APPLICATION_CREDENTIALS.
 */
publicstaticStringkeyFilePath;
/** Service account credentials for Google Wallet APIs. */
publicstaticGoogleCredentialscredentials;
/** Google Wallet service client. */
publicstaticWalletobjectsservice;
publicDemoOffer()throwsException{
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\DateTime;
use Google\Service\Walletobjects\TimeInterval;
use Google\Service\Walletobjects\OfferObject;
use Google\Service\Walletobjects\OfferClass;
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\Message;
use Google\Service\Walletobjects\AddMessageRequest;
use Google\Service\Walletobjects\Uri;
/** Demo class for creating and managing Offers in Google Wallet. */
class DemoOffer
{
 /**
 * 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.

importjson
importos
importuuid
fromgoogleapiclient.discoveryimport build
fromgoogleapiclient.errorsimport HttpError
fromgoogle.oauth2.service_accountimport Credentials
fromgoogle.authimport jwt, crypt
classDemoOffer:
"""Demo class for creating and managing Offers 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.

usingSystem.IdentityModel.Tokens.Jwt;
usingSystem.Net.Http.Headers;
usingSystem.Text.RegularExpressions;
usingGoogle.Apis.Auth.OAuth2;
usingGoogle.Apis.Services;
usingGoogle.Apis.Walletobjects.v1;
usingGoogle.Apis.Walletobjects.v1.Data;
usingMicrosoft.IdentityModel.Tokens;
usingNewtonsoft.Json;
usingNewtonsoft.Json.Linq;
/// <summary>
/// Demo class for creating and managing Offers in Google Wallet.
/// </summary>
classDemoOffer
{
/// <summary>
/// Path to service account key file from Google Cloud Console. Environment
/// variable: GOOGLE_APPLICATION_CREDENTIALS.
/// </summary>
publicstaticstringkeyFilePath;
/// <summary>
/// Service account credentials for Google Wallet APIs
/// </summary>
publicstaticServiceAccountCredentialcredentials;
/// <summary>
/// Google Wallet service client
/// </summary>
publicstaticWalletobjectsServiceservice;
publicDemoOffer()
{
keyFilePath=Environment.GetEnvironmentVariable(
"GOOGLE_APPLICATION_CREDENTIALS")??"/path/to/key.json";
Auth();
}

Node.js

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

const{google}=require('googleapis');
constjwt=require('jsonwebtoken');
const{v4:uuidv4}=require('uuid');
/**
 * Demo class for creating and managing Offers in Google Wallet.
 */
classDemoOffer{
constructor(){
/**
 * Path to service account key file from Google Cloud Console. Environment
 * variable: GOOGLE_APPLICATION_CREDENTIALS.
 */
this.keyFilePath=process.env.GOOGLE_APPLICATION_CREDENTIALS||'/path/to/key.json';
this.auth();
}

Go

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

packagemain
import(
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/golang-jwt/jwt"
"github.com/google/uuid"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
oauthJwt"golang.org/x/oauth2/jwt"
"google.golang.org/api/option"
"google.golang.org/api/walletobjects/v1"
"io"
"log"
"os"
"strings"
)

Next, use one of the framework libraries to retrieve the necessary credentials to call the Google Wallet API.

Java

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

/**
 * Create authenticated HTTP client using a service account file.
 *
 */
publicvoidauth()throwsException{
credentials=
GoogleCredentials.fromStream(newFileInputStream(keyFilePath))
.createScoped(List.of(WalletobjectsScopes.WALLET_OBJECT_ISSUER));
credentials.refresh();
HttpTransporthttpTransport=GoogleNetHttpTransport.newTrustedTransport();
// Initialize Google Wallet API service
service=
newWalletobjects.Builder(
httpTransport,
GsonFactory.getDefaultInstance(),
newHttpCredentialsAdapter(credentials))
.setApplicationName("APPLICATION_NAME")
.build();
}

PHP

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

/**
 * Create authenticated HTTP client using a service account file.
 */
public function auth()
{
 $this->credentials = new ServiceAccountCredentials(
 Walletobjects::WALLET_OBJECT_ISSUER,
 $this->keyFilePath
 );
 // Initialize Google Wallet API service
 $this->client = new GoogleClient();
 $this->client->setApplicationName('APPLICATION_NAME');
 $this->client->setScopes(Walletobjects::WALLET_OBJECT_ISSUER);
 $this->client->setAuthConfig($this->keyFilePath);
 $this->service = new Walletobjects($this->client);
}

Python

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

defauth(self):
"""Create authenticated HTTP client using a service account file."""
 self.credentials = Credentials.from_service_account_file(
 self.key_file_path,
 scopes=['https://www.googleapis.com/auth/wallet_object.issuer'])
 self.client = build('walletobjects', 'v1', credentials=self.credentials)

C#

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

/// <summary>
/// Create authenticated service client using a service account file.
/// </summary>
publicvoidAuth()
{
credentials=(ServiceAccountCredential)GoogleCredential
.FromFile(keyFilePath)
.CreateScoped(newList<string>
{
WalletobjectsService.ScopeConstants.WalletObjectIssuer
})
.UnderlyingCredential;
service=newWalletobjectsService(
newBaseClientService.Initializer()
{
HttpClientInitializer=credentials
});
}

Node.js

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

/**
 * Create authenticated HTTP client using a service account file.
 */
auth(){
constauth=newgoogle.auth.GoogleAuth({
keyFile:this.keyFilePath,
scopes:['https://www.googleapis.com/auth/wallet_object.issuer'],
});
this.credentials=require(this.keyFilePath);
this.client=google.walletobjects({
version:'v1',
auth:auth,
});
}

Go

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

// Create authenticated HTTP client using a service account file.
func(d*demoOffer)auth(){
credentialsFile:=os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")
b,_:=os.ReadFile(credentialsFile)
credentials,err:=google.JWTConfigFromJSON(b,walletobjects.WalletObjectIssuerScope)
iferr!=nil{
fmt.Println(err)
log.Fatalf("Unable to load credentials: %v",err)
}
d.credentials=credentials
d.service,_=walletobjects.NewService(context.Background(),option.WithCredentialsFile(credentialsFile))
}

Google Wallet Android SDK requests

Requests using the Google Wallet Android SDK are automatically autheticated using you app signing certificate. The Android SDK will automatically create the SHA-1 fingerprint of your signing certificate and include it with requests to the Google Wallet API.

For more information on generating and registering the SHA-1 fingerprint of your app signing certificate, see Authorizing your app for the Google Wallet Android SDK.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2026年09月01日 UTC.