private static async Task Run()

in secure-app-model/keyvault/CPVApplication/Program.cs [35:93]


        private static async Task Run()
        {
            // The following properties indicate which partner and customer context the calls are going to be made.
            string PartnerId = "<Partner tenant id>";
            string CustomerId = "<Customer tenant id>";

            Console.WriteLine(" ===================== Partner center  API calls ============================", DateTime.Now);
            IAggregatePartner ops = await GetUserPartnerOperationsAsync(PartnerId);
            SeekBasedResourceCollection<CustomerUser> customerUsers = ops.Customers.ById(CustomerId).Users.Get();
            Console.WriteLine(JsonConvert.SerializeObject(customerUsers));

            Console.WriteLine(" ===================== Partner graph  API calls ============================", DateTime.Now);
            Tuple<string, DateTimeOffset> tokenResult = await LoginToGraph(PartnerId);
            JObject mydetails = await ApiCalls.GetAsync(tokenResult.Item1, "https://graph.microsoft.com/v1.0/me");
            Console.WriteLine(JsonConvert.SerializeObject(mydetails));

            // The customer graph calls require customer admin to consent the application
            Console.WriteLine(" ===================== Customer consent for graph  API calls ============================", DateTime.Now);

            // Enable consent
            Tuple<string, DateTimeOffset> tokenPartnerResult = await LoginToPartnerCenter(PartnerId);
            JObject contents = new JObject
            {
                // Provide your application display name
                ["displayName"] = "CPV Marketplace",

                // Provide your application id
                ["applicationId"] = CPVApplicationId,

                // Provide your application grants
                ["applicationGrants"] = new JArray(
                    JObject.Parse("{\"enterpriseApplicationId\": \"00000002-0000-0000-c000-000000000000\", \"scope\":\"Domain.ReadWrite.All,User.ReadWrite.All,Directory.Read.All\"}"), // for graph api access,  Directory.Read.All
                    JObject.Parse("{\"enterpriseApplicationId\": \"797f4846-ba00-4fd7-ba43-dac1f8f63013\", \"scope\":\"user_impersonation\"}")) // for ARM api access
            };

            /** The following steps have to performed once in per customer tenant if your application is Control panel vendor application and requires customer tenant graph access  **/

            // delete the previous grant into customer tenant
            JObject consentDeletion = await ApiCalls.DeleteAsync(
                tokenPartnerResult.Item1,
               string.Format("https://api.partnercenter.microsoft.com/v1/customers/{0}/applicationconsents/{1}", CustomerId, CPVApplicationId));
            Console.WriteLine(JsonConvert.SerializeObject(consentDeletion));

            // create new grants for the application given the setting in application grants payload.
            JObject consentCreation = await ApiCalls.PostAsync(
                tokenPartnerResult.Item1,
                string.Format("https://api.partnercenter.microsoft.com/v1/customers/{0}/applicationconsents", CustomerId),
                contents.ToString());
            Console.WriteLine(JsonConvert.SerializeObject(consentCreation));


            Console.WriteLine(" ===================== Customer graph  API calls ============================", DateTime.Now);

            Tuple<string, DateTimeOffset> tokenCustomerResult = await LoginToCustomerGraph(PartnerId, CustomerId);
            JObject customerDomainsUsingGraph = await ApiCalls.GetAsync(tokenCustomerResult.Item1, "https://graph.windows.net/" + CustomerId + "/domains?api-version=1.6");
            Console.WriteLine(JsonConvert.SerializeObject(customerDomainsUsingGraph));

            Console.ReadLine();
        }