3

I need to validate my google play purchases using the PurchaseStatus API.. i want to use the AccountService option... [I did get this to work using the WebServer workflow, but that requires manual interaction, i can't use that on production ]. For using Service Account, I tried two approaches that i could think of:

1) Token using Google Libraries + REST APIs..

credential = new ServiceAccountCredential( new ServiceAccountCredential.Initializer(serviceAccountEmail)
{   Scopes = new[] { "https://www.googleapis.com/auth/androidpublisher"  }
}.FromCertificate(certificate));

var task = credential.RequestAccessTokenAsync(cancellationtoken); 
// wait for it ...  with error handling.. 
var accessToken  = credential.Token.AccessToken ;  
string url = string.Format("https://www.googleapis.com/androidpublisher/v1.1/applications/{0}/inapp/{1}/purchases/{2}?access_token={3}",  packageName, sku, purchaseToken , accessToken ); 
// UseWebRequest agains that   URL ...

RESULT: the above fails with 401: Unauthorized
That same code, works OK if instead of using the token coming from ServiceAccountCredential, I use a token from Web Server workflow..

2) Next I tried using ServiceAccountCredential + AndroidPublisherService.. so both google libraries:

// Same ServiceAccountCredential as above.. but now I initialize my AndroidPublisherService with the credential in this manner.. 

AndroidPublisherService svc = new AndroidPublisherService( new Google.Apis.Services.BaseClientService.Initializer()
{ HttpClientInitializer = credential   });                               
var purchaseRequest = svc.Inapppurchases.Get(packageName, sku, token);
var purchase = purchaseRequest.Execute();

RESULT: I get a

[Google.GoogleApiException]
{"Google.Apis.Requests.RequestError\r\nThis developer account does not own the application. [401]\r\nErrors [\r\n\tMessage[This developer account does not own the application.] Location[ - ] Reason[developerDoesNotOwnApplication] Domain[androidpublisher]\r\n]\r\n"} Google.GoogleApiException

3) I tried a few other things such as trying to pass a User to the initializer... but they did not work..

What am I doing wrong?
How can I validate an Android Purchase using the ServiceAccount from ASP .NET ?

joaquin
  • 82,968
  • 29
  • 138
  • 152

1 Answers1

1

Please follow the steps in this SO article- Am I getting the steps right for verifying a user's Android in-app subscription?

On a shorter note you need to create Application on the Google APIs Console and use the Client Id and Client Secret.

Then you need to get a "refresh token" which is never expiring and can be saved in the application for all future requests.

After refresh token you need to make a post request which will provide an "access token" using that token you can send a get request and validate the purchase.

Community
  • 1
  • 1
Manik Arora
  • 4,702
  • 1
  • 25
  • 48