.NET quickstart for customers

Follow the steps in this quickstart guide, and in about 10 minutes you have a simple .NET C# console app that makes requests to the zero-touch enrollment customer API using a service account.

Prerequisites

To run this quickstart, you need:

  • A service account, that's linked to you zero-touch enrollment customer account. See Get started.
  • Visual Studio 2013 or later.
  • Access to the internet and a web browser.

Step 1: Turn on the zero-touch enrollment API

  1. Use this wizard to create or select a project in the Google Developers Console and automatically turn on the API. Click Continue, then Go to credentials .
  2. Set What data will you be accessing? to Application data.
  3. Click Next. You should be prompted to create a service account.
  4. Give a descriptive name for Service account name.
  5. Note the Service account ID (it looks like an email address) because you'll use it later.
  6. Set Role to Service Accounts > Service Account User.
  7. Click Done to finish creating the service account.
  8. Click the email address for the service account that you created.
  9. Click **Keys**.
  10. Click **Add key**, then click **Create new key**.
  11. For **Key type**, select **JSON**.
  12. Click Create and the private key downloads to your computer.
  13. Click **Close**.
  14. Move the file to your working directory and rename it service_account_key.json.

Step 2: Prepare the project

  1. Create a new .NET Core C# Console Application project in Visual Studio.
  2. Open the Package Manager, select the package source nuget.org, and add the following packages:
    • Google.Apis.AndroidProvisioningPartner.v1
    • Google.Apis.Auth

To learn more, read the Microsoft document Install and use a package.

Step 3: Set up the sample

  1. Drag the service_account_key.json you downloaded when you created your service account into your Visual Studio Solution Explorer.
  2. Select service_account_key.json, and then go to the Properties window and set Copy to output directory field to Always copy.
  3. Replace the contents of Program.cs with the following code:
using Google.Apis.AndroidProvisioningPartner.v1;
using Google.Apis.AndroidProvisioningPartner.v1.Data;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;

namespace ZeroTouchCustomerQuickstart
{
    class Program
    {
        // A single scope is used for the zero-touch enrollment customer API.
        static readonly string[] Scopes =
            { "https://www.googleapis.com/auth/androidworkzerotouchemm" };
        static string ApplicationName = "Zero-touch Enrollment .NET Quickstart";

        static void Main(string[] args)
        {
            GoogleCredential credential;

            // Authenticate using the service account key
            credential = GoogleCredential.FromFile("service_account_key.json")
                .CreateScoped(Scopes);

            // Create a zero-touch enrollment API service endpoint.
            var service = new AndroidProvisioningPartnerService(new BaseClientService.Initializer
            {
                HttpClientInitializer = credential,
                ApplicationName = ApplicationName
            });

            // Get the customer's account. Because a customer might have more
            // than one, limit the results to the first account found.
            CustomersResource.ListRequest accountRequest = service.Customers.List();
            accountRequest.PageSize = 1;
            CustomerListCustomersResponse accountResponse = accountRequest.Execute();
            if (accountResponse.Customers.Count == 0)
            {
                // No accounts found for the user. Confirm the Google Account
                // that authorizes the request can access the zero-touch portal.
                Console.WriteLine("No zero-touch enrollment account found.");
                Environment.Exit(-1);
            }
            Company customer = accountResponse.Customers[0];
            var customerAccount = String.Format("customers/{0}", customer.CompanyId);


            // Send an API request to list all the DPCs available.
            CustomersResource.DpcsResource.ListRequest request = service.Customers.Dpcs.
                List(customerAccount);
            CustomerListDpcsResponse response = request.Execute();

            // Print out the details of each DPC.
            IList<Dpc> dpcs = response.Dpcs;
            foreach (Dpc dpcApp in dpcs)
            {
                Console.WriteLine("Name:{0}  APK:{1}",
                                  dpcApp.DpcName,
                                  dpcApp.PackageName);
            }

        }
    }
}

Step 4: Run the sample

To build and run the sample, click Start in the Visual Studio toolbar.

Notes

  • Avoid sharing your service_account_key.json file with anyone. Be careful not to include it in source code repositories. You can read more advice on handling service account secrets.

Learn more