android.netgoogle-apigoogle-oauthgoogle-api-dotnet-client

How to obtain android app version code using Google Api?


My desktop app used www.google.com/accounts/ClientLogin (which currently unavailable) to obtain authentification token, that i used to get android application info from unofficial market api (https://androidquery.appspot.com/api/market?app=(put the package name here))

Now Google want me to use OAuth authorization because ClientLogin deprecated and response 404 all time.

So question is - how can i get android application info by "appId" (just version code for example - "23") using OAuth 2.0 and Google Client Api Libraries for .NET?


Solution

  • I found solution for this problem.

    Google Api provides one method to obtain apllication version code.

    Firstly, you need to create a project in Google Developers Console, create credentials for Service Account with p12 key file. And enable Google Play Developers Api.

    In Google Play Developers Console you should link your app to this project.

    After, you can write this code in eour desktop .NET appliation:

    var serviceAccountEmail = "YOUR Service Account Email";
            var certificate = new X509Certificate2(@"key.p12", "YOUR_CLIENT_SECRET", X509KeyStorageFlags.Exportable);
    
            ServiceAccountCredential credential = new ServiceAccountCredential(
                new ServiceAccountCredential.Initializer(serviceAccountEmail)
                    {
                        Scopes = new[] { AndroidPublisherService.Scope.Androidpublisher }
                    }.FromCertificate(certificate));
    
            var service = new AndroidPublisherService(
                new BaseClientService.Initializer()
                    {
                        HttpClientInitializer = credential,
                        ApplicationName = "Edits Sample"
                    });
            var apiEditBody = new AppEdit();
            // packageName - your app id like com.myapp.test
            var appEdit = service.Edits.Insert(apiEditBody, packageName)
                .Execute();
    
            var list = service.Edits.Apks.List(packageName, appEdit.Id)
                .Execute()
                .Apks;
    
            var deletingEditResult = service.Edits.Delete(packageName, appEdit.Id).Execute();
            var versionCode = list.Last().VersionCode.Value;
    

    That's it. Hope, this answer will help somebody =)