Samuele Catuzzi
Samuele Catuzzi

Reputation: 710

"Service Accounts" androidpublisher verify subscription purchase -> 401 "This developer account does not own the application"

api version: google-api-php-client-0.6.7

I need to verify on my server, through my php script, an android subscription purchase made by the user from my android app.

So, no user interaction, the user will send to my server only the token of his purchase and my server will verify if is a valid purchase.
The only way that I found that doesn't need user interaction seem to be the "Service Accounts", so I followed the documentation and made CLIENT_ID,SERVICE_ACCOUNT_NAME,KEY_FILE also, for testing, I used a real purchase token from my application $ANDROIDUsertoken

this is my server script (for security reasons I changed some code parts):

<?php
include_once('../lib/google-api-php-client/src/Google_Client.php');
include_once('../lib/google-api-php-client/src/contrib/Google_AndroidpublisherService.php');

//user token, in json format
$ANDROIDUsertoken = '{"orderId":"....","packageName":"....","productId":"....","purchaseTime":....,"purchaseState":0,"purchaseToken":"...."}';
$user_token= json_decode($ANDROIDUsertoken,true);

// https://developers.google.com/console/help/#service_accounts
const CLIENT_ID = 'something-private.apps.googleusercontent.com';
const SERVICE_ACCOUNT_NAME = '[email protected]';
const KEY_FILE = 'secret-dir/privatekey.p12';

$client = new Google_Client();
$client->setApplicationName($user_token['packageName']);
$client->setClientId(CLIENT_ID);
$key = file_get_contents(KEY_FILE);

echo "start auth:<br>";

$auth = new Google_AssertionCredentials(
    SERVICE_ACCOUNT_NAME,
    array('https://www.googleapis.com/auth/androidpublisher'),
    $key);

$client->setAssertionCredentials($auth);

//$client->getAuth()->refreshTokenWithAssertion();
//$accessToken=$client->getAccessToken();
//$client->setAccessToken($accessToken);

echo "start requests:<br>";

$AndroidPublisherService = new Google_AndroidPublisherService($client);
$res = $AndroidPublisherService->purchases->get($user_token['packageName'], $user_token['productId'], $user_token['purchaseToken']);

var_dump($res);
?>

errors:

start auth:
start requests:
Fatal error: Uncaught exception 'Google_ServiceException' with message 'Error calling GET https://www.googleapis.com/androidpublisher/v1.1/applications/..APPLICATION../subscriptions/..PRODUCT../purchases/..TOKEN..: 
(401) This developer account does not own the application.' in /..PATH TO MY WEBSERVER../lib/google-api-php-client/src/io/Google_REST.php on line 66

I also previously successfully work with OAuth+androidpublisher on "web applications" but that require user interaction that is not my objective (I need a server side check)

note that $AndroidPublisherService->purchases->get stand for subscription

I saw this post but is slightly different and was not working for me

Upvotes: 1

Views: 3224

Answers (1)

frieser
frieser

Reputation: 745

Ok I found the solution!

You must to connect your API with your app. You must go to your Google Play publish page (https://play.google.com/apps/publish) and invite a user with the service account email in Settings->User account & rights->Invite new user and give it the privileges of "View financial reports".

Thanks works for me, good luck!

Upvotes: 2

Related Questions