.NET quickstart for resellers

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 reseller API.

Prerequisites

To run this quickstart, you'll need:

  • A Google account, that's a member of your zero-touch enrollment reseller account. If you haven't onboarded yet, follow the steps in Get started in the Reseller portal guide.
  • 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.
  1. Open the zero-touch enrollment portal. You might need to sign in.
  2. Click Service accounts.
  3. Click Link service account.
  4. Set Email address to the address of the service account you created.
  5. Click Link service account to use the service account with your zero-touch enrollment account.

Step 3: 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 4: Set up the sample

  1. Drag service_account_key.json (downloaded in Step 1) 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.
  4. Insert your own reseller partner ID as the value for PartnerId (the app's first line).
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;

namespace ZeroTouchResellerQuickstart
{
    class Program
    {
        // TODO: replace this with your partner reseller ID.
        static long PartnerId = 11036885;

        // Use a single scope for the all methods in the reseller API.
        static readonly string[] Scopes =
        { "https://www.googleapis.com/auth/androidworkprovisioning" };
        static string ApplicationName = "Zero-touch Reseller .NET Quickstart";

        static void Main(string[] args)
        {
            // Create a credential to authorize API requests using a service account key.
            // The service account must be linked using the zero-touch portal.
            ServiceAccountCredential credential;
            using (var stream =
                new FileStream("service_account_key.json", FileMode.Open, FileAccess.Read))
            {
                credential = GoogleCredential.FromStream(stream)
                                     .CreateScoped(Scopes)
                                     .UnderlyingCredential as ServiceAccountCredential;
            }

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

            // Send an API request to list all our customers.
            PartnersResource.CustomersResource.ListRequest request =
                service.Partners.Customers.List(PartnerId);
            ListCustomersResponse response = request.Execute();

            // Print out the details of each customer.
            IList<Company> customers = response.Customers;
            if (customers != null)
            {
                foreach (Company customer in customers)
                {
                    Console.WriteLine("Name:{0}  ID:{1}",
                                      customer.CompanyName,
                                      customer.CompanyId);
                }
            }
            else
            {
                Console.WriteLine("No customers found");
            }
        }
    }
}

Partner ID

API calls typically need your reseller partner ID as an argument. To find your partner ID from the zero-touch enrollment portal, follow the steps below:

  1. Open the portal. You might need to sign in.
  2. Click Service accounts.
  3. Copy your partner ID number from the Your reseller ID line.

Step 5: Run the sample

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

Troubleshooting

Tell us what went wrong with the quickstart and we'll work to fix it. To learn how zero-touch uses service accounts to authorize API calls, read Authorization.

Learn more