.NET quickstart for customers

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

Prerequisites

To run this quickstart, you'll need:

  • A Google account, that's a member of your zero-touch enrollment customer account. See Customer accounts.
  • 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. Click Cancel on the Add credentials to your project page.
  3. At the top of the page, select the OAuth consent screen tab. Select an Email address, enter a Product name if not already set, and click the Save button.
  4. Select the Credentials tab, click the Create credentials button and select OAuth client ID.
  5. Select the application type Other, enter the name "Quickstart", and click the Create button.
  6. Click OK to dismiss the OAuth client panel.
  7. Click file_download Download JSON.
  8. Move the file to your working directory and rename it client_secret.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 client_secret.json (downloaded in Step 1) into your Visual Studio Solution Explorer.
  2. Select client_secret.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 Google.Apis.Util.Store;
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)
        {
            UserCredential credential;

            // Ask the user to authorize the request using their Google Account
            // in their browser.
            using (var stream =
                new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
            {
                string credPath = System.Environment.GetFolderPath(
                    System.Environment.SpecialFolder.Personal);
                credPath = Path.Combine(credPath, ".credentials/zero-touch.quickstart.json");

                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    Scopes,
                    "user",
                    CancellationToken.None,
                    new FileDataStore(credPath, true)).Result;
                Console.WriteLine("Credential file saved to: " + credPath);
            }

            // 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 play_arrow Start in the Visual Studio toolbar.

The first time you run the app, you need to authorize access:

  1. The app tries to open a new tab in your default browser. If this fails, copy the URL from the console and open it in your browser. If you're not already logged into your Google Account, you're prompted to log in. If you're logged into multiple Google accounts, the page prompts you to select an account for the authorization.
  2. Click Accept.
  3. Close the browser tab—the app continues running.

Notes

  • Because Google API client library stores authorization data on the file system, subsequent launches don't prompt you for authorization.
  • To reset the app's authorization data, delete the ~/.credentials/zero-touch.quickstart.json file and run the app again.
  • The authorization flow in this quickstart is ideal for a command-line app. To learn how to add authorization to a web app, see Using OAuth 2.0, Web applications (ASP.NET MVC).

Troubleshooting

Here are some common things you'll want to check. Tell us what went wrong with the quickstart and we'll work to fix it.

  • Check that you're authorizing API calls with the same Google Account that's a member of your zero-touch enrollment customer account. Try signing in to the zero-touch enrollment portal using the same Google Account to test your access.
  • Confirm that the account has accepted the latest Terms of Service in the portal. See Customer accounts.

Learn more