Add In-app Product Via Google API

Hello today I will discuss about how to add product to “Google In app product” list dynamically. In other words how to add product to the “Google in app product” list via google api from the code. Before start, lets know, what is in app products ?

When we use and android application, sometimes we got some offer from that application , what we need to by to go to the next step of the application or activate new step. It randomly happen while playing games. Like buy arms or new weapon. The stuff or product , what we can buy from inside an application is called “in app” product.

There are two steps for adding products to google in app product list

  1. Configure Google account.
  2. Add product from code via Google console API.

In this tutorial we will use Drupal CMS to add product but same concept will be applicable for other PHP application.

Configure Google Account

The minimum requirement to configure Google account is

  1. Should have a Google account to access Google developer console API. To access any Google API it is must.
  2. Should have a Google play developer console account . This is a paid account. By this account one can manage Google play application.

Now we have to follow the following steps

First, we have to create a project to the Google Developer Console (GDC). from the  “Project menu”. By clicking the new created project , there will be a side appear in the left side. To add product to “Google In-app Product” list, we have to activate “Subscriptions and In-App Purchases” api from the API list (APIs & Auth -> APIs).

To access this API we need to authenticate from the consumer application that why we need to have a client ID for this Google console project.  To create one we have to brows APIs & Auth -> Credential . There are three types of O’auth available

  • Client ID for web application, for this one individual user have to login to have the api access.
  • Service Account. by this one , no individual users have to login , but the consumer application will be authenticated by a .p12 key , given from the Google account. In this example we will use “Service Account” authentication. While creating a “Service Account”, a .p12 key file will be generated and we have keep it in save for further uses.

Second, We have to create an application in “Google play developer console”. Because , in-app product should have a container application. For each application there is a “package name”. This package-name is mandatory to add product. Add product api call will contain this package name and will identify the application to which the product will be added.

Now we have make a link between the “Google Developer Console” and “Google Play Developer Console”. There is an option in the “Google Play Developer Console” dashboard to make this link. This link is actually done via the client Id , which we have already created for API authentication. That the end of our configuration part.

Add product from code via Google console API

First of all , we have to authenticate to our google account to access the Google APIs. As we know, in the O’auth authentication process, an access key is generated after successful authentication and this is used for further api call.

To access Google API from php there is a php library called “google-api-php-client”. It’s a very good php library , which has all functionality for authentication and access different API. I can be downloaded from

For authentication with service account lets have a code example

    $serviceAccountName = // Email Address
    $keyFileLocation = // key.p12, full path

    $client = new Google_Client();

    $client->setApplicationName("Name of the Application");
    $key = file_get_contents($keyFileLocation);
    $cred = new Google_Auth_AssertionCredentials(

    if ($client->getAuth()->isAccessTokenExpired()) {

In the above code example, $serviceAccountName is the email address of the client ID and $key path is the location of .p12 file which generated from the Google while creating the service account. Next , we create a client object of “Google Client” class. This class and other classes uses in this example will be available after loading the “google-api-php-client” library. After successful authentication the $client object will be updated with a access token from the Google account. While authentication we have to mention , which Google API categories we will use , in our case we will use ”’. So out authentication is done. Now we will manage product by using this client object.

Now, API reference to manage in-app product can be found from Here you can found we parameter need to pass while add or edit or delete product from in-app product list.

As an example to add product

$client = mychapterreader_client_get();// client after authentication
$service = new Google_Service_AndroidPublisher($client);// Which service we are going to use
$product = new Google_Service_AndroidPublisher_InAppProduct();//Preparing the product object
$product->setPackageName($packageName);//Package name of the application
$product->setSku($sku);//SKU , which will act as ID of the product
/* Other product information*/    
$result = $service->inappproducts->insert($product->getPackageName(), $product);

And that’s it and we are done .