Get android subscription status, failed with 403

13,615

Solution 1

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;

Solution 2

I'm having the same issue. Seems the authentication has changed in the v2 api.

You can still use the old api endpoint (v1.1). it works fine for me.

https://www.googleapis.com/androidpublisher/v1.1/applications/{packageName}/inapp/{productId}/purchases/{token}

Solution 3

It's 2019 and this problem still exists. However, I found on another answer somewhere that someone said you had to create a new product in your account after setting this app up... and that actually worked for me.

Solution 4

I used Fabrizio Farenga's code and the Google API code with good results. In addition, it's worth noting that the permission your service account needs to access this is just View financial reports.

Share:
13,615
neobie
Author by

neobie

Updated on June 05, 2022

Comments

  • neobie
    neobie about 2 years

    While trying to get android in-app subscription status (with expiry date), I get the following error message:

    {
     "error": {
      "errors": [
       {
        "domain": "androidpublisher",
        "reason": "projectNotLinked",
        "message": "The project id used to call the Google Play Developer API has not been linked in the Google Play Developer Console."
       }
      ],
      "code": 403,
      "message": "The project id used to call the Google Play Developer API has not been linked in the Google Play Developer Console."
     }
    }
    

    The URL is: https://www.googleapis.com/androidpublisher/v2/applications/[packageName]/inapp/[productId]/purchases/[purchase_token]?access_token=[access_token]

    It says the project id is not linked. I have done the following:

    1. Create the project in Google Developer Console.
    2. Turn on the Google Android Publisher API.
    3. Link the project ID as mentioned in API access page (https://developers.google.com/android-publisher/getting_started)
    4. Fill in the packageName, productId (from in app-purchase), purchase_token (from Android app)
    

    Not sure why the error message appear as above. I have tried with the OAuth2 playground with no luck (https://developers.google.com/oauthplayground/)

  • Daniel Lubarov
    Daniel Lubarov over 9 years
    Thanks, you saved me a lot of trouble. Can't believe how difficult this is to set up!
  • Krtko
    Krtko about 9 years
    Hey are you sure your authenticating a user with the correct permissions?
  • temuri
    temuri about 9 years
    Have you been able to confirm that with v2 api you can only fetch purchases list using service account?
  • Abhijit Chakra
    Abhijit Chakra about 7 years
    'package name" ,productID, but what is here the {token} from where i will get it
  • Bevor
    Bevor over 6 years
    Additional info: The Developer Console API project (and the oauth2 credentials) has to be created with the same Google account as used in Google Play Developer Console. Otherwise you cannot link a specific project from the Developer Console API.
  • K.E.
    K.E. about 5 years
    thanks for your hint to create a new product. In my case it was not necessary to create a new product, but to only update the product in the developer console.
  • user1228739
    user1228739 almost 3 years
    How to obtain my p12 file (key.p12)?