/*
* Copyright (c) 2022 Winsider Seminars & Solutions, Inc. All rights reserved.
*
* This file is part of System Informer.
*
* Authors:
*
* dmex
*
*/
namespace CustomBuildTool
{
///
/// Configuration required to call the Microsoft Store submission API for MSI or EXE apps.
///
/// This type is never serialized; it only carries credentials and endpoints.
public class StoreClientConfiguration
{
///
/// Client Id of your Microsoft Entra ID Directory app.
/// Example: 00001111-aaaa-2222-bbbb-3333cccc4444
///
public string ClientId { get; set; }
///
/// Client secret of your Microsoft Entra ID Directory app.
///
public string ClientSecret { get; set; }
///
/// Service root endpoint.
/// Example: "https://api.store.microsoft.com"
///
public string ServiceUrl { get; set; }
///
/// Token endpoint to which the request is to be made. Specific to your Microsoft Entra ID Directory app.
/// Example: https://login.microsoftonline.com/d454d300-128e-2d81-334a-27d9b2baf002/oauth2/v2.0/token
///
public string TokenEndpoint { get; set; }
///
/// Resource scope. If not provided (set to null), the default one is used for the production API
/// endpoint ("https://api.store.microsoft.com/.default").
///
public string Scope { get; set; }
///
/// Partner Center Application ID.
/// Example: 3e31a9f9-84e8-4d2d-9eba-487878d02ebf
///
public string ApplicationId { get; set; }
///
/// The Partner Center Seller Id.
/// Example: 123456892
///
public int SellerId { get; set; }
}
///
/// This class is a proxy that abstracts the functionality of the Microsoft Store submission API service.
///
/// Ported from the Microsoft Store submission API documentation sample. The original sample used
/// Newtonsoft.Json together with dynamic payloads; both are incompatible with NativeAOT and trimming, so
/// every request and response is bound to a concrete type and serialized through .
public class SubmissionClient : IDisposable
{
public static readonly string Version = "1";
private HttpClient HttpClient;
private HttpClient ImageUploadClient;
private readonly string AccessToken;
public const string PackagesUrlTemplate = "/submission/v{0}/product/{1}/packages";
public const string PackageByIdUrlTemplate = "/submission/v{0}/product/{1}/packages/{2}";
public const string PackagesCommitUrlTemplate = "/submission/v{0}/product/{1}/packages/commit";
public const string AppMetadataUrlTemplate = "/submission/v{0}/product/{1}/metadata";
public const string AppListingsFetchMetadataUrlTemplate = "/submission/v{0}/product/{1}/metadata/listings";
public const string ListingAssetsUrlTemplate = "/submission/v{0}/product/{1}/listings/assets";
public const string ListingAssetsCreateUrlTemplate = "/submission/v{0}/product/{1}/listings/assets/create";
public const string ListingAssetsCommitUrlTemplate = "/submission/v{0}/product/{1}/listings/assets/commit";
public const string ProductDraftStatusPollingUrlTemplate = "/submission/v{0}/product/{1}/status";
public const string CreateSubmissionUrlTemplate = "/submission/v{0}/product/{1}/submit";
public const string SubmissionStatusPollingUrlTemplate = "/submission/v{0}/product/{1}/submission/{2}/status";
private const string DefaultScope = "https://api.store.microsoft.com/.default";
private const string JsonContentType = "application/json";
private const string PngContentType = "image/png";
private const string BinaryStreamContentType = "application/octet-stream";
///
/// Initializes a new instance of the class.
///
/// The access token. This is a JWT token obtained from Microsoft Entra ID Directory
/// allowing the caller to invoke the API on behalf of a user.
/// The service URL.
public SubmissionClient(string AccessToken, string ServiceUrl)
{
ArgumentException.ThrowIfNullOrEmpty(AccessToken);
ArgumentException.ThrowIfNullOrEmpty(ServiceUrl);
this.AccessToken = AccessToken;
this.HttpClient = BuildHttpClient.CreateHttpClient();
this.HttpClient.BaseAddress = new Uri(ServiceUrl);
this.ImageUploadClient = BuildHttpClient.CreateHttpClient();
this.DefaultHeaders = new Dictionary(StringComparer.OrdinalIgnoreCase);
}
///
/// Gets or sets the default headers sent with every service request.
///
public Dictionary DefaultHeaders { get; set; }
///
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
///
public void Dispose()
{
if (this.HttpClient != null)
{
this.HttpClient.Dispose();
this.HttpClient = null;
}
if (this.ImageUploadClient != null)
{
this.ImageUploadClient.Dispose();
this.ImageUploadClient = null;
}
GC.SuppressFinalize(this);
}
///
/// Gets the authorization token for the provided client id, client secret, and the scope.
/// This token is usually valid for 1 hour, so if your submission takes longer than that to complete,
/// make sure to get a new one periodically.
///
/// Token endpoint to which the request is to be made. Specific to your
/// Microsoft Entra ID Directory app. Example: https://login.microsoftonline.com/d454d300-128e-2d81-334a-27d9b2baf002/oauth2/v2.0/token
/// Client Id of your Microsoft Entra ID Directory app. Example: 00001111-aaaa-2222-bbbb-3333cccc4444
/// Client secret of your Microsoft Entra ID Directory app.
/// Scope. If not provided, the default one is used for the production API endpoint.
/// A cancellation token that can be used to cancel the operation. Optional.
/// Authorization token. Prepend it with "Bearer " and pass it in the request header as the
/// value for the "Authorization" header.
public static async ValueTask GetClientCredentialAccessToken(
string TokenEndpoint,
string ClientId,
string ClientSecret,
string Scope = null,
CancellationToken CancellationToken = default
)
{
ArgumentException.ThrowIfNullOrEmpty(TokenEndpoint);
ArgumentException.ThrowIfNullOrEmpty(ClientId);
ArgumentException.ThrowIfNullOrEmpty(ClientSecret);
Scope ??= DefaultScope;
using (HttpClient client = BuildHttpClient.CreateHttpClient())
using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, TokenEndpoint))
{
request.Content = new FormUrlEncodedContent([
new("grant_type", "client_credentials"),
new("client_id", ClientId),
new("client_secret", ClientSecret),
new("scope", Scope)
]);
using (HttpResponseMessage response = await BuildHttpClient.SendWithRetry(client, request, CancellationToken))
{
var content = await response.Content.ReadAsStreamAsync(CancellationToken);
if (!response.IsSuccessStatusCode)
{
throw new HttpRequestException("GetClientCredentialAccessToken", null, response.StatusCode);
}
var result = await JsonSerializer.DeserializeAsync(content, StoreSubmissionJsonContext.Default.StoreTokenResponse, CancellationToken);
if (result == null || string.IsNullOrWhiteSpace(result.AccessToken))
throw new InvalidOperationException("The token endpoint did not return an access token.");
return result.AccessToken;
}
}
}
///
/// Invokes the specified HTTP method without a request body.
///
/// The type of the deserialized response.
/// The HTTP method.
/// The relative URL.
/// The source generated metadata for .
/// A cancellation token that can be used to cancel the operation. Optional.
/// An instance of the type .
public ValueTask Invoke(
HttpMethod HttpMethod,
string RelativeUrl,
JsonTypeInfo ResponseTypeInfo,
CancellationToken CancellationToken = default
)
{
return this.InvokeCore(HttpMethod, RelativeUrl, null, ResponseTypeInfo, CancellationToken);
}
///
/// Invokes the specified HTTP method with a JSON request body.
///
/// The type of the request body.
/// The type of the deserialized response.
/// The HTTP method.
/// The relative URL.
/// Content of the request.
/// The source generated metadata for .
/// The source generated metadata for .
/// A cancellation token that can be used to cancel the operation. Optional.
/// An instance of the type .
public ValueTask Invoke(
HttpMethod HttpMethod,
string RelativeUrl,
TRequest RequestContent,
JsonTypeInfo RequestTypeInfo,
JsonTypeInfo ResponseTypeInfo,
CancellationToken CancellationToken = default
)
{
HttpContent content = null;
if (RequestContent != null)
{
content = JsonContent.Create(RequestContent, RequestTypeInfo, new MediaTypeHeaderValue(JsonContentType, "utf-8"));
}
return this.InvokeCore(HttpMethod, RelativeUrl, content, ResponseTypeInfo, CancellationToken);
}
private async ValueTask InvokeCore(
HttpMethod HttpMethod,
string RelativeUrl,
HttpContent RequestContent,
JsonTypeInfo ResponseTypeInfo,
CancellationToken CancellationToken
)
{
using (var request = new HttpRequestMessage(HttpMethod, RelativeUrl))
{
this.SetRequest(request, RequestContent);
using (HttpResponseMessage response = await BuildHttpClient.SendWithRetry(this.HttpClient, request, CancellationToken))
{
if (this.TryHandleResponse(response, out TResponse handled))
{
return handled;
}
if (!response.IsSuccessStatusCode)
{
string content = await response.Content.ReadAsStringAsync(CancellationToken);
throw new HttpRequestException(content, null, response.StatusCode);
}
await using var stream = await response.Content.ReadAsStreamAsync(CancellationToken);
return await JsonSerializer.DeserializeAsync(stream, ResponseTypeInfo, CancellationToken);
}
}
}
///
/// Uploads a given image asset file to asset storage.
///
/// Asset storage URL.
/// The stream instance of the file to be uploaded.
/// A cancellation token that can be used to cancel the operation. Optional.
public async ValueTask UploadAsset(string AssetUploadUrl, Stream FileStream, CancellationToken CancellationToken = default)
{
ArgumentException.ThrowIfNullOrEmpty(AssetUploadUrl);
ArgumentNullException.ThrowIfNull(FileStream);
using (var request = new HttpRequestMessage(HttpMethod.Put, AssetUploadUrl))
{
request.Headers.Add("x-ms-blob-type", "BlockBlob");
request.Content = new StreamContent(FileStream);
request.Content.Headers.ContentType = new MediaTypeHeaderValue(PngContentType);
using (HttpResponseMessage response = await BuildHttpClient.SendWithRetry(this.ImageUploadClient, request, CancellationToken))
{
if (response.IsSuccessStatusCode)
return;
throw new HttpRequestException(await response.Content.ReadAsStringAsync(CancellationToken), null, response.StatusCode);
}
}
}
///
/// Sets the authorization header, the default headers and the request body.
///
/// The request.
/// Content of the request, or null when the request has no body.
protected virtual void SetRequest(HttpRequestMessage Request, HttpContent RequestContent)
{
Request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", this.AccessToken);
foreach (var header in this.DefaultHeaders)
{
Request.Headers.Add(header.Key, header.Value);
}
if (RequestContent != null)
{
Request.Content = RequestContent;
}
}
///
/// Tries to handle the response.
///
/// The type of the deserialized response.
/// The response.
/// The result.
/// true if the response was handled.
protected virtual bool TryHandleResponse(HttpResponseMessage Response, out TResponse Result)
{
Result = default;
return false;
}
}
///
/// Demonstrates updating an app submission using several methods of the Microsoft Store submission API.
///
/// This is a step-for-step port of the documentation sample, including its demonstration values, so
/// that it can be compared against the upstream sample. It is not wired into any build command.
public class AppSubmissionUpdateSample
{
private readonly StoreClientConfiguration ClientConfig;
///
/// Initializes a new instance of the class.
///
/// An instance of with all parameters populated.
public AppSubmissionUpdateSample(StoreClientConfiguration Configuration)
{
ArgumentNullException.ThrowIfNull(Configuration);
this.ClientConfig = Configuration;
}
///
/// Main method to run the sample application.
///
///
public async Task RunAppSubmissionUpdateSample()
{
// **********************
// SETTINGS
// **********************
var appId = this.ClientConfig.ApplicationId;
var clientId = this.ClientConfig.ClientId;
var clientSecret = this.ClientConfig.ClientSecret;
var serviceEndpoint = this.ClientConfig.ServiceUrl;
var tokenEndpoint = this.ClientConfig.TokenEndpoint;
var scope = this.ClientConfig.Scope;
// Get authorization token.
Program.PrintColorMessage("Getting authorization token", ConsoleColor.Cyan);
var accessToken = await SubmissionClient.GetClientCredentialAccessToken(
tokenEndpoint,
clientId,
clientSecret,
scope);
using var client = new SubmissionClient(accessToken, serviceEndpoint);
client.DefaultHeaders = new Dictionary(StringComparer.OrdinalIgnoreCase)
{
{ "X-Seller-Account-Id", this.ClientConfig.SellerId.ToString(CultureInfo.InvariantCulture) }
};
Program.PrintColorMessage("Getting Current Application Draft Status", ConsoleColor.Cyan);
var appDraftStatus = await client.Invoke(
HttpMethod.Get,
string.Format(CultureInfo.InvariantCulture, SubmissionClient.ProductDraftStatusPollingUrlTemplate, SubmissionClient.Version, appId),
StoreSubmissionJsonContext.Default.StoreApiResponseStoreProductDraftStatus);
PrintResponse(appDraftStatus, StoreSubmissionJsonContext.Default.StoreApiResponseStoreProductDraftStatus);
Program.PrintColorMessage("Getting Application Packages", ConsoleColor.Cyan);
var packagesResponse = await client.Invoke(
HttpMethod.Get,
string.Format(CultureInfo.InvariantCulture, SubmissionClient.PackagesUrlTemplate, SubmissionClient.Version, appId),
StoreSubmissionJsonContext.Default.StoreApiResponseStorePackagesData);
PrintResponse(packagesResponse, StoreSubmissionJsonContext.Default.StoreApiResponseStorePackagesData);
if (packagesResponse?.ResponseData?.Packages == null || packagesResponse.ResponseData.Packages.Count == 0)
throw new InvalidOperationException("The current draft submission does not contain any packages.");
Program.PrintColorMessage("Getting Single Package", ConsoleColor.Cyan);
var singlePackageResponse = await client.Invoke(
HttpMethod.Get,
string.Format(CultureInfo.InvariantCulture, SubmissionClient.PackageByIdUrlTemplate, SubmissionClient.Version, appId, packagesResponse.ResponseData.Packages[0].PackageId),
StoreSubmissionJsonContext.Default.StoreApiResponseStorePackagesData);
PrintResponse(singlePackageResponse, StoreSubmissionJsonContext.Default.StoreApiResponseStorePackagesData);
Program.PrintColorMessage("Updating Entire Package Set", ConsoleColor.Cyan);
// Update data in Packages list to have final set of updated Packages
// Example - Updating Installer Parameters
packagesResponse.ResponseData.Packages[0].InstallerParameters = "/s /r new-args";
var packagesUpdateRequest = new StorePackagesUpdateRequest
{
Packages = packagesResponse.ResponseData.Packages
};
var packagesUpdateResponse = await client.Invoke(
HttpMethod.Put,
string.Format(CultureInfo.InvariantCulture, SubmissionClient.PackagesUrlTemplate, SubmissionClient.Version, appId),
packagesUpdateRequest,
StoreSubmissionJsonContext.Default.StorePackagesUpdateRequest,
StoreSubmissionJsonContext.Default.StoreApiResponseStoreCommitData);
PrintResponse(packagesUpdateResponse, StoreSubmissionJsonContext.Default.StoreApiResponseStoreCommitData);
Program.PrintColorMessage("Updating Single Package's Download Url", ConsoleColor.Cyan);
// Update data in the SinglePackage object
var singlePackageUpdateRequest = singlePackageResponse.ResponseData.Packages[0];
// Example - Updating Installer Parameters
singlePackageUpdateRequest.InstallerParameters = "/s /r /t new-args";
var packageUpdateResponse = await client.Invoke(
HttpMethod.Patch,
string.Format(CultureInfo.InvariantCulture, SubmissionClient.PackageByIdUrlTemplate, SubmissionClient.Version, appId, singlePackageUpdateRequest.PackageId),
singlePackageUpdateRequest,
StoreSubmissionJsonContext.Default.StorePackage,
StoreSubmissionJsonContext.Default.StoreApiResponseStoreCommitData);
PrintResponse(packageUpdateResponse, StoreSubmissionJsonContext.Default.StoreApiResponseStoreCommitData);
Program.PrintColorMessage("Committing Packages", ConsoleColor.Cyan);
var packageCommitResponse = await client.Invoke(
HttpMethod.Post,
string.Format(CultureInfo.InvariantCulture, SubmissionClient.PackagesCommitUrlTemplate, SubmissionClient.Version, appId),
StoreSubmissionJsonContext.Default.StoreApiResponseStoreCommitData);
PrintResponse(packageCommitResponse, StoreSubmissionJsonContext.Default.StoreApiResponseStoreCommitData);
Program.PrintColorMessage("Polling Package Upload Status", ConsoleColor.Cyan);
appDraftStatus = await client.Invoke(
HttpMethod.Get,
string.Format(CultureInfo.InvariantCulture, SubmissionClient.ProductDraftStatusPollingUrlTemplate, SubmissionClient.Version, appId),
StoreSubmissionJsonContext.Default.StoreApiResponseStoreProductDraftStatus);
while (appDraftStatus?.ResponseData == null || !appDraftStatus.ResponseData.IsReady)
{
appDraftStatus = await client.Invoke(
HttpMethod.Get,
string.Format(CultureInfo.InvariantCulture, SubmissionClient.ProductDraftStatusPollingUrlTemplate, SubmissionClient.Version, appId),
StoreSubmissionJsonContext.Default.StoreApiResponseStoreProductDraftStatus);
Program.PrintColorMessage("Waiting for Upload to finish", ConsoleColor.Yellow);
await Task.Delay(TimeSpan.FromSeconds(2));
if (appDraftStatus?.Errors != null && appDraftStatus.Errors.Count > 0)
{
for (var index = 0; index < appDraftStatus.Errors.Count; index++)
{
if (string.Equals(appDraftStatus.Errors[index].Code, "packageuploaderror", StringComparison.OrdinalIgnoreCase))
{
throw new InvalidOperationException("Package Upload Failed. Please try committing packages again.");
}
}
}
}
Program.PrintColorMessage("Getting Application Metadata - All Modules", ConsoleColor.Cyan);
var appMetadata = await client.Invoke(
HttpMethod.Get,
string.Format(CultureInfo.InvariantCulture, SubmissionClient.AppMetadataUrlTemplate, SubmissionClient.Version, appId),
StoreSubmissionJsonContext.Default.StoreApiResponseStoreMetadataData);
PrintResponse(appMetadata, StoreSubmissionJsonContext.Default.StoreApiResponseStoreMetadataData);
Program.PrintColorMessage("Getting Application Metadata - Listings", ConsoleColor.Cyan);
var appListingsMetadata = await client.Invoke(
HttpMethod.Get,
string.Format(CultureInfo.InvariantCulture, SubmissionClient.AppListingsFetchMetadataUrlTemplate, SubmissionClient.Version, appId),
StoreSubmissionJsonContext.Default.StoreApiResponseStoreMetadataData);
PrintResponse(appListingsMetadata, StoreSubmissionJsonContext.Default.StoreApiResponseStoreMetadataData);
if (appListingsMetadata?.ResponseData?.Listings == null || appListingsMetadata.ResponseData.Listings.Count == 0)
throw new InvalidOperationException("The current draft submission does not contain any listings.");
Program.PrintColorMessage("Updating Listings Metadata - Description", ConsoleColor.Cyan);
// Update Required Fields in Listings Metadata Object - Per Language. For eg. AppListingsMetadata.ResponseData.Listings[0]
// Example - Updating Description
appListingsMetadata.ResponseData.Listings[0].Description = "New Description Updated By C# Sample Code";
// Note: the fetch returns 'listings' as an array while the update sends a single object.
var listingsUpdateRequest = new StoreListingsUpdateRequest
{
Listings = appListingsMetadata.ResponseData.Listings[0]
};
var updateListingsMetadataResponse = await client.Invoke(
HttpMethod.Put,
string.Format(CultureInfo.InvariantCulture, SubmissionClient.AppMetadataUrlTemplate, SubmissionClient.Version, appId),
listingsUpdateRequest,
StoreSubmissionJsonContext.Default.StoreListingsUpdateRequest,
StoreSubmissionJsonContext.Default.StoreApiResponseStoreCommitData);
PrintResponse(updateListingsMetadataResponse, StoreSubmissionJsonContext.Default.StoreApiResponseStoreCommitData);
Program.PrintColorMessage("Getting All Listings Assets", ConsoleColor.Cyan);
var listingAssets = await client.Invoke(
HttpMethod.Get,
string.Format(CultureInfo.InvariantCulture, SubmissionClient.ListingAssetsUrlTemplate, SubmissionClient.Version, appId),
StoreSubmissionJsonContext.Default.StoreApiResponseStoreListingAssetsData);
PrintResponse(listingAssets, StoreSubmissionJsonContext.Default.StoreApiResponseStoreListingAssetsData);
if (listingAssets?.ResponseData?.ListingAssets == null || listingAssets.ResponseData.ListingAssets.Count == 0)
throw new InvalidOperationException("The current draft submission does not contain any listing assets.");
Program.PrintColorMessage("Creating Listing Assets for 1 Screenshot", ConsoleColor.Cyan);
var assetCreateRequest = new StoreCreateAssetsRequest
{
Language = listingAssets.ResponseData.ListingAssets[0].Language,
CreateAssetRequest = new Dictionary(StringComparer.OrdinalIgnoreCase)
{
{ "Screenshot", 1 },
{ "Logo", 0 }
}
};
var assetCreateResponse = await client.Invoke(
HttpMethod.Post,
string.Format(CultureInfo.InvariantCulture, SubmissionClient.ListingAssetsCreateUrlTemplate, SubmissionClient.Version, appId),
assetCreateRequest,
StoreSubmissionJsonContext.Default.StoreCreateAssetsRequest,
StoreSubmissionJsonContext.Default.StoreApiResponseStoreCreatedAssetsData);
PrintResponse(assetCreateResponse, StoreSubmissionJsonContext.Default.StoreApiResponseStoreCreatedAssetsData);
if (assetCreateResponse?.ResponseData?.ListingAssets?.Screenshots == null || assetCreateResponse.ResponseData.ListingAssets.Screenshots.Count == 0)
throw new InvalidOperationException("The create listing assets request did not return any screenshot upload urls.");
var createdScreenshot = assetCreateResponse.ResponseData.ListingAssets.Screenshots[0];
Program.PrintColorMessage("Uploading Listing Assets", ConsoleColor.Cyan);
// Path to PNG File to be Uploaded as Screenshot / Logo
var pathToFile = "./Image.png";
await using (var assetToUpload = File.OpenRead(pathToFile))
{
await client.UploadAsset(createdScreenshot.PrimaryAssetUploadUrl, assetToUpload);
}
Program.PrintColorMessage("Committing Listing Assets", ConsoleColor.Cyan);
var assetCommitRequest = new StoreAssetsCommitRequest
{
ListingAssets = new StoreAssetCommitSet
{
Language = listingAssets.ResponseData.ListingAssets[0].Language,
StoreLogos = ToAssetReferences(listingAssets.ResponseData.ListingAssets[0].StoreLogos),
Screenshots = new List
{
new StoreAssetReference
{
Id = createdScreenshot.Id,
AssetUrl = createdScreenshot.PrimaryAssetUploadUrl
}
}
}
};
var assetCommitResponse = await client.Invoke(
HttpMethod.Put,
string.Format(CultureInfo.InvariantCulture, SubmissionClient.ListingAssetsCommitUrlTemplate, SubmissionClient.Version, appId),
assetCommitRequest,
StoreSubmissionJsonContext.Default.StoreAssetsCommitRequest,
StoreSubmissionJsonContext.Default.StoreApiResponseStoreCommitData);
PrintResponse(assetCommitResponse, StoreSubmissionJsonContext.Default.StoreApiResponseStoreCommitData);
Program.PrintColorMessage("Getting Current Application Draft Status before Submission", ConsoleColor.Cyan);
appDraftStatus = await client.Invoke(
HttpMethod.Get,
string.Format(CultureInfo.InvariantCulture, SubmissionClient.ProductDraftStatusPollingUrlTemplate, SubmissionClient.Version, appId),
StoreSubmissionJsonContext.Default.StoreApiResponseStoreProductDraftStatus);
PrintResponse(appDraftStatus, StoreSubmissionJsonContext.Default.StoreApiResponseStoreProductDraftStatus);
if (appDraftStatus?.ResponseData == null || !appDraftStatus.ResponseData.IsReady)
{
throw new InvalidOperationException("Application Current Status is not in Ready Status for All Modules");
}
Program.PrintColorMessage("Creating Submission", ConsoleColor.Cyan);
var submissionCreationResponse = await client.Invoke(
HttpMethod.Post,
string.Format(CultureInfo.InvariantCulture, SubmissionClient.CreateSubmissionUrlTemplate, SubmissionClient.Version, appId),
StoreSubmissionJsonContext.Default.StoreApiResponseStoreSubmissionCreatedData);
PrintResponse(submissionCreationResponse, StoreSubmissionJsonContext.Default.StoreApiResponseStoreSubmissionCreatedData);
if (submissionCreationResponse?.ResponseData == null || string.IsNullOrWhiteSpace(submissionCreationResponse.ResponseData.SubmissionId))
throw new InvalidOperationException("The create submission request did not return a submission id.");
Program.PrintColorMessage("Current Submission Status", ConsoleColor.Cyan);
var submissionStatus = await client.Invoke(
HttpMethod.Get,
string.Format(CultureInfo.InvariantCulture, SubmissionClient.SubmissionStatusPollingUrlTemplate, SubmissionClient.Version, appId, submissionCreationResponse.ResponseData.SubmissionId),
StoreSubmissionJsonContext.Default.StoreApiResponseStoreSubmissionStatusData);
PrintResponse(submissionStatus, StoreSubmissionJsonContext.Default.StoreApiResponseStoreSubmissionStatusData);
// User can Poll on this API to know if Submission Status is INPROGRESS, PUBLISHED or FAILED.
// This Process involves File Scanning, App Certification and Publishing and can take more than a day.
}
///
/// Projects a set of existing listing assets to the identifier and url pairs expected by the commit API.
///
private static List ToAssetReferences(List Assets)
{
if (Assets == null)
return null;
var references = new List(Assets.Count);
foreach (var asset in Assets)
{
references.Add(new StoreAssetReference
{
Id = asset.Id,
AssetUrl = asset.AssetUrl
});
}
return references;
}
///
/// Writes the JSON representation of a service response to the console.
///
private static void PrintResponse(TResponse Response, JsonTypeInfo ResponseTypeInfo)
{
if (Response == null)
return;
Console.WriteLine(JsonSerializer.Serialize(Response, ResponseTypeInfo));
}
}
}
namespace CustomBuildTool
{
///
/// The envelope returned by every Microsoft Store submission API method.
///
/// The type of the method specific response payload.
public class StoreApiResponse
{
[JsonPropertyName("isSuccess")]
public bool IsSuccess { get; set; }
///
/// The list of error or warning messages if any.
///
[JsonPropertyName("errors")]
public List Errors { get; set; }
[JsonPropertyName("responseData")]
public T ResponseData { get; set; }
}
public class StoreApiError
{
///
/// The error code of the message.
///
[JsonPropertyName("code")]
public string Code { get; set; }
///
/// The description of the error.
///
[JsonPropertyName("message")]
public string Message { get; set; }
///
/// The entity from which the error originated.
///
[JsonPropertyName("target")]
public string Target { get; set; }
}
///
/// The token response returned by the Microsoft Entra ID token endpoint.
///
public class StoreTokenResponse
{
[JsonPropertyName("access_token")]
public string AccessToken { get; set; }
[JsonPropertyName("token_type")]
public string TokenType { get; set; }
[JsonPropertyName("expires_in")]
public long ExpiresIn { get; set; }
}
///
/// Product draft status polling payload.
///
public class StoreProductDraftStatus
{
[JsonPropertyName("isReady")]
public bool IsReady { get; set; }
[JsonPropertyName("ongoingSubmissionId")]
public string OngoingSubmissionId { get; set; }
}
///
/// The payload shared by the commit and module update methods.
///
public class StoreCommitData
{
[JsonPropertyName("pollingUrl")]
public string PollingUrl { get; set; }
[JsonPropertyName("ongoingSubmissionId")]
public string OngoingSubmissionId { get; set; }
}
public class StorePackagesData
{
[JsonPropertyName("packages")]
public List Packages { get; set; }
}
public class StorePackage
{
[JsonPropertyName("packageId")]
public string PackageId { get; set; }
[JsonPropertyName("packageUrl")]
public string PackageUrl { get; set; }
[JsonPropertyName("languages")]
public List Languages { get; set; }
///
/// Should contain a single architecture - Neutral, X86, X64, Arm, Arm64.
///
[JsonPropertyName("architectures")]
public List Architectures { get; set; }
///
/// This should be marked as true if the installer runs in silent mode without requiring switches.
///
[JsonPropertyName("isSilentInstall")]
public bool IsSilentInstall { get; set; }
[JsonPropertyName("installerParameters")]
public string InstallerParameters { get; set; }
[JsonPropertyName("genericDocUrl")]
public string GenericDocUrl { get; set; }
[JsonPropertyName("errorDetails")]
public List ErrorDetails { get; set; }
[JsonPropertyName("packageType")]
public string PackageType { get; set; }
}
public class StorePackageErrorDetail
{
///
/// One of [rebootRequired, installCancelled, alreadyInstalled, downgrade, packageRejected,
/// internalError, otherError, applicationNotFound, contactSupport, invalidParameter,
/// diskFull, networkFailure, packageInUse, invalidPackage, downloadFailure,
/// serverFailure, installFailure, uninstallFailure, custom, blockedByPolicy,
/// systemNotSupported, userCancelled, installationInProgress, installationSuccessful, miscellaneous].
///
[JsonPropertyName("errorScenario")]
public string ErrorScenario { get; set; }
[JsonPropertyName("errorScenarioDetails")]
public List ErrorScenarioDetails { get; set; }
}
public class StorePackageErrorScenario
{
///
/// Error code which can be present during installation.
///
[JsonPropertyName("errorValue")]
public string ErrorValue { get; set; }
[JsonPropertyName("errorUrl")]
public string ErrorUrl { get; set; }
}
///
/// The metadata payload. The listings fetch method populates only .
///
public class StoreMetadataData
{
[JsonPropertyName("availability")]
public StoreAvailability Availability { get; set; }
[JsonPropertyName("properties")]
public StoreProperties Properties { get; set; }
[JsonPropertyName("listings")]
public List Listings { get; set; }
}
public class StoreAvailability
{
[JsonPropertyName("markets")]
public List Markets { get; set; }
[JsonPropertyName("discoverability")]
public string Discoverability { get; set; }
[JsonPropertyName("enableInFutureMarkets")]
public bool EnableInFutureMarkets { get; set; }
[JsonPropertyName("pricing")]
public string Pricing { get; set; }
[JsonPropertyName("freeTrial")]
public string FreeTrial { get; set; }
}
public class StoreProperties
{
[JsonPropertyName("isPrivacyPolicyRequired")]
public bool IsPrivacyPolicyRequired { get; set; }
[JsonPropertyName("privacyPolicyUrl")]
public string PrivacyPolicyUrl { get; set; }
[JsonPropertyName("website")]
public string Website { get; set; }
[JsonPropertyName("supportContactInfo")]
public string SupportContactInfo { get; set; }
[JsonPropertyName("certificationNotes")]
public string CertificationNotes { get; set; }
[JsonPropertyName("category")]
public string Category { get; set; }
[JsonPropertyName("subcategory")]
public string Subcategory { get; set; }
[JsonPropertyName("productDeclarations")]
public StoreProductDeclarations ProductDeclarations { get; set; }
[JsonPropertyName("isSystemFeatureRequired")]
public List IsSystemFeatureRequired { get; set; }
[JsonPropertyName("systemRequirementDetails")]
public List SystemRequirementDetails { get; set; }
}
public class StoreProductDeclarations
{
[JsonPropertyName("dependsOnDriversOrNT")]
public bool DependsOnDriversOrNT { get; set; }
[JsonPropertyName("accessibilitySupport")]
public bool AccessibilitySupport { get; set; }
[JsonPropertyName("penAndInkSupport")]
public bool PenAndInkSupport { get; set; }
}
public class StoreSystemFeature
{
[JsonPropertyName("isRequired")]
public bool IsRequired { get; set; }
[JsonPropertyName("isRecommended")]
public bool IsRecommended { get; set; }
///
/// One of [Touch, Keyboard, Mouse, Camera, NFC_HCE, NFC_Proximity, Bluetooth_LE, Telephony, Microphone].
///
[JsonPropertyName("hardwareItemType")]
public string HardwareItemType { get; set; }
}
public class StoreSystemRequirement
{
[JsonPropertyName("minimumRequirement")]
public string MinimumRequirement { get; set; }
[JsonPropertyName("recommendedRequirement")]
public string RecommendedRequirement { get; set; }
///
/// One of [Memory, DirectX, Video_Memory, Processor, Graphics].
///
[JsonPropertyName("hardwareItemType")]
public string HardwareItemType { get; set; }
}
public class StoreListing
{
[JsonPropertyName("language")]
public string Language { get; set; }
[JsonPropertyName("description")]
public string Description { get; set; }
[JsonPropertyName("whatsNew")]
public string WhatsNew { get; set; }
[JsonPropertyName("productFeatures")]
public List ProductFeatures { get; set; }
[JsonPropertyName("shortDescription")]
public string ShortDescription { get; set; }
[JsonPropertyName("searchTerms")]
public List SearchTerms { get; set; }
[JsonPropertyName("additionalLicenseTerms")]
public string AdditionalLicenseTerms { get; set; }
[JsonPropertyName("copyright")]
public string Copyright { get; set; }
[JsonPropertyName("developedBy")]
public string DevelopedBy { get; set; }
[JsonPropertyName("sortTitle")]
public string SortTitle { get; set; }
[JsonPropertyName("requirements")]
public List Requirements { get; set; }
[JsonPropertyName("contactInfo")]
public string ContactInfo { get; set; }
}
public class StoreListingRequirement
{
[JsonPropertyName("minimumHardware")]
public string MinimumHardware { get; set; }
[JsonPropertyName("recommendedHardware")]
public string RecommendedHardware { get; set; }
}
public class StoreListingAssetsData
{
[JsonPropertyName("listingAssets")]
public List ListingAssets { get; set; }
}
public class StoreListingAsset
{
[JsonPropertyName("language")]
public string Language { get; set; }
[JsonPropertyName("storeLogos")]
public List StoreLogos { get; set; }
[JsonPropertyName("screenshots")]
public List Screenshots { get; set; }
}
public class StoreImageAsset
{
[JsonPropertyName("id")]
public string Id { get; set; }
[JsonPropertyName("assetUrl")]
public string AssetUrl { get; set; }
[JsonPropertyName("imageSize")]
public StoreImageSize ImageSize { get; set; }
}
public class StoreImageSize
{
[JsonPropertyName("width")]
public int Width { get; set; }
[JsonPropertyName("height")]
public int Height { get; set; }
}
public class StoreCreatedAssetsData
{
[JsonPropertyName("listingAssets")]
public StoreCreatedAssetSet ListingAssets { get; set; }
}
public class StoreCreatedAssetSet
{
[JsonPropertyName("language")]
public string Language { get; set; }
[JsonPropertyName("storeLogos")]
public List StoreLogos { get; set; }
[JsonPropertyName("screenshots")]
public List Screenshots { get; set; }
}
public class StoreCreatedAsset
{
[JsonPropertyName("id")]
public string Id { get; set; }
///
/// Short lived SAS url used to upload the asset to the blob store.
///
[JsonPropertyName("primaryAssetUploadUrl")]
public string PrimaryAssetUploadUrl { get; set; }
[JsonPropertyName("secondaryAssetUploadUrl")]
public string SecondaryAssetUploadUrl { get; set; }
}
public class StoreSubmissionCreatedData
{
[JsonPropertyName("submissionId")]
public string SubmissionId { get; set; }
[JsonPropertyName("pollingUrl")]
public string PollingUrl { get; set; }
[JsonPropertyName("ongoingSubmissionId")]
public string OngoingSubmissionId { get; set; }
}
public class StoreSubmissionStatusData
{
///
/// Publishing status of the submission - [INPROGRESS, PUBLISHED, FAILED, UNKNOWN].
///
[JsonPropertyName("publishingStatus")]
public string PublishingStatus { get; set; }
///
/// Indicates if publishing has failed and won't be retried.
///
[JsonPropertyName("hasFailed")]
public bool HasFailed { get; set; }
}
///
/// The request body of the full package module update method.
///
public class StorePackagesUpdateRequest
{
[JsonPropertyName("packages")]
public List Packages { get; set; }
}
///
/// The request body of the listings metadata update method.
///
/// The fetch method returns the listings as an array while the update method expects a single
/// object; this asymmetry matches the service contract.
public class StoreListingsUpdateRequest
{
[JsonPropertyName("listings")]
public StoreListing Listings { get; set; }
}
///
/// The request body of the create listing assets method.
///
public class StoreCreateAssetsRequest
{
[JsonPropertyName("language")]
public string Language { get; set; }
///
/// The number of assets to create per asset type. Screenshot [1 - 10], Logo [1 or 2].
///
[JsonPropertyName("createAssetRequest")]
public Dictionary CreateAssetRequest { get; set; }
}
///
/// The request body of the commit listing assets method.
///
public class StoreAssetsCommitRequest
{
[JsonPropertyName("listingAssets")]
public StoreAssetCommitSet ListingAssets { get; set; }
}
public class StoreAssetCommitSet
{
[JsonPropertyName("language")]
public string Language { get; set; }
[JsonPropertyName("storeLogos")]
public List StoreLogos { get; set; }
[JsonPropertyName("screenshots")]
public List Screenshots { get; set; }
}
public class StoreAssetReference
{
///
/// Either an existing id retained from the get listing assets method or a new id under which an
/// asset was uploaded by the create listing assets method.
///
[JsonPropertyName("id")]
public string Id { get; set; }
///
/// Either an existing asset url retained from the get listing assets method or the upload url used
/// by the create listing assets method.
///
[JsonPropertyName("assetUrl")]
public string AssetUrl { get; set; }
}
[JsonSourceGenerationOptions(DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, WriteIndented = false, GenerationMode = JsonSourceGenerationMode.Default)]
[JsonSerializable(typeof(StoreTokenResponse))]
[JsonSerializable(typeof(StoreApiResponse), TypeInfoPropertyName = "StoreApiResponseStoreProductDraftStatus")]
[JsonSerializable(typeof(StoreApiResponse), TypeInfoPropertyName = "StoreApiResponseStoreCommitData")]
[JsonSerializable(typeof(StoreApiResponse), TypeInfoPropertyName = "StoreApiResponseStorePackagesData")]
[JsonSerializable(typeof(StoreApiResponse), TypeInfoPropertyName = "StoreApiResponseStoreMetadataData")]
[JsonSerializable(typeof(StoreApiResponse), TypeInfoPropertyName = "StoreApiResponseStoreListingAssetsData")]
[JsonSerializable(typeof(StoreApiResponse), TypeInfoPropertyName = "StoreApiResponseStoreCreatedAssetsData")]
[JsonSerializable(typeof(StoreApiResponse), TypeInfoPropertyName = "StoreApiResponseStoreSubmissionCreatedData")]
[JsonSerializable(typeof(StoreApiResponse), TypeInfoPropertyName = "StoreApiResponseStoreSubmissionStatusData")]
[JsonSerializable(typeof(StorePackage))]
[JsonSerializable(typeof(StorePackagesUpdateRequest))]
[JsonSerializable(typeof(StoreListingsUpdateRequest))]
[JsonSerializable(typeof(StoreCreateAssetsRequest))]
[JsonSerializable(typeof(StoreAssetsCommitRequest))]
internal partial class StoreSubmissionJsonContext : JsonSerializerContext;
}