Get android subscription status, failed with 403

I spent some time on this but at the end the error message says all:
“[…] has not been linked in the Google Play Developer Console”.

I spent hours looking in the Google Developer Console, but the place where to link the project is in the Google Play Developer console.

Just go to Settings -> Api Access and you’ll be able to link the project you created in the Google Developer Console.

Here is a working PHP solution to get the subscription status. You need to create a service account in the Google Developer Console -> ‘your project’ -> API & Auth -> Credential and download the Google API PHP Client from https://github.com/google/google-api-php-client

    set_include_path("../src/" . PATH_SEPARATOR . get_include_path());
    require_once 'Google/Client.php';
    require_once 'Google/Service/AndroidPublisher.php';

    $client_id = '';    //Your client id
    $service_account_name="";  //Your service account email
    $key_file_location = ''; //Your p12 file (key.p12)

    $client = new Google_Client();
    $client->setApplicationName(""); //This is the name of the linked application
    $service = new Google_Service_AndroidPublisher($client);

    $key = file_get_contents($key_file_location);
    $cred = new Google_Auth_AssertionCredentials(
        $service_account_name,
        array('https://www.googleapis.com/auth/androidpublisher'),
        $key
    );
    $client->setAssertionCredentials($cred);
    if($client->getAuth()->isAccessTokenExpired()) {
      $client->getAuth()->refreshTokenWithAssertion($cred);
    }        
    $apiKey = ""; //Your API key
    $client->setDeveloperKey($apiKey);

    $package_name = ""; //Your package name (com.example...)
    $subscriptionId = "";   //SKU of your subscription item

    //Token returned to the app after the purchase
    $token = "";

    $service = new Google_Service_AndroidPublisher($client);
    $results = $service->purchases_subscriptions->get($package_name,$subscriptionId,$token,array());

    print_r ($results); //This object has all the data about the subscription
    echo "expiration: " . $results->expiryTimeMillis;
    exit;

Leave a Comment