简体   繁体   中英

Google Play Custom App Publishing API to publish a private app in C#

I'm trying to figure out how to use the Google Play Custom App Publishing API to publish a private app using C# and I'm not able to find a way to upload or pass the APK.

The documentation here gives an example of how to do it :

Path apkPath = Paths.get("PATH_TO_APK");
ByteArrayContent apk =
    new ByteArrayContent("application/octet-stream", Files.readAllBytes(apkPath));

CustomApp appMetadata =
    new CustomApp()
      .setTitle("APPLICATION TITLE").setLanguageCode("en_US");

CustomApps.Create request =
    apiClient.accounts() // Playcustomapp apiClient
      .customApps().create(DEV_ACCOUNT_ID, appMetadata, apk);

CustomApp response = request.execute();

I've imported the NuGet package Google.Apis.Playcustomapp.v1 and created appMetaData

CustomApp appMetadata = new CustomApp() {
    Title = "Whatsapp",
    LanguageCode = "en-US"
};

There are 2 API requests available and they are :

var createRequest = new AccountsResource.CustomAppsResource.CreateRequest(
    service, appMetadata, 9197907806840XXXXX);
var createMediaUpload = new AccountsResource.CustomAppsResource.CreateMediaUpload(
    service, appMetadata, 9197907806840XXXXX, ms, "application /octet-stream");

Both of which give me error 404. This is because I have not passed the APK which needs to be uploaded. Where can I send this APK from? There's a stream parameter in the CreateMediaUpload but it doesn't help as well.

Errors :

CreateRequest : Unexpected character encountered while parsing value: <. Path ''. How do I pass the path of the APK here?

CreateMediaUpload : The requested URL /upload/9197907806840917992/customApps?uploadType=resumable was not found on this server.

Can someone help me with this?

The documentation you have found is a Java Sample. After some random guessing on how to put the pieces together, i finally got it.

Keep in mind, there is some magic undocumented restriction about how often you can create private apps per day. Something about 15 apps per day. After that you hit that limit you will get this message error:

apkNotificationMessageKeyReachedUploadLimit

static void Main(string[] args)

    {
        MainAsync(args).GetAwaiter().GetResult();
    }

    static async Task MainAsync(string[] args)
    {
        var email = "yourserviceaccountmail";
        ServiceAccountCredential credential = new ServiceAccountCredential(
            new ServiceAccountCredential.Initializer(email)
            {
                Scopes = new[] { PlaycustomappService.Scope.Androidpublisher }
            }.FromPrivateKey("client_secrets.json"));

        CustomApp appMetadata = new CustomApp();
        appMetadata.Title = "YOUR APP";
        appMetadata.LanguageCode = "en_US";

        // Create the service.
        var service = new PlaycustomappService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "Custom Apps API Sample",
        });

        var acc = new AccountsResource(service);
        using (var stream = new FileStream("app-release.apk", FileMode.Open))
        {
            long devIAccountd = 12345678L;
            var app = acc.CustomApps.Create(appMetadata, devIAccountd, stream, "application/octet-stream");
            var response = await app.UploadAsync();
            if (response.Exception != null)
            {
                Console.WriteLine("Someting went wrong:");
                Console.WriteLine(response.Exception.Message);
            }
            if (response.Status == Google.Apis.Upload.UploadStatus.Completed)
            {
                Console.WriteLine("Succeeded!");
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM