面向转销商的 Java 快速入门

按照此快速入门指南中的步骤操作,您将在大约 10 分钟后获得一个简单的 Java 命令行应用,该应用会向零触摸注册转销商 API 发出请求。

前提条件

如需运行本快速入门,您需要:

  • Google 帐号,即您的零触摸注册转销商帐号的成员。如果您尚未进行初始配置,请按照转销商门户指南使用入门中的步骤操作。
  • Java 1.7 或更高版本。
  • Gradle 2.3 或更高版本
  • 能够访问互联网和网络浏览器。

第 1 步:启用零触摸注册 API

  1. 使用此向导在 Google Developers Console 中创建或选择项目,并自动启用 API。点击继续,然后点击转到凭据
  2. 您要访问哪些数据?设为应用数据
  3. 点击下一步。系统应该会提示您创建服务帐号。
  4. 服务帐号名称指定一个描述性名称。
  5. 记下服务帐号 ID(看起来像是电子邮件地址),因为您稍后会用到它。
  6. 角色设置为 Service Accounts > Service Account User
  7. 点击完成以完成服务帐号的创建过程。
  8. 点击您创建的服务帐号的电子邮件地址。
  9. 点击 **Keys**。
  10. 点击 **添加密钥**,然后点击 **创建新密钥**。
  11. 对于 **密钥类型**,选择 **JSON**。
  12. 点击创建,然后将私钥下载到您的计算机。
  13. 点击 **Close**。
  14. 将该文件移动到工作目录,并将其重命名为 service_account_key.json
  1. 打开零触摸注册门户。您可能需要登录。
  2. 点击 服务帐号
  3. 点击 关联服务帐号
  4. 电子邮件地址设置为您创建的服务帐号的地址。
  5. 点击关联服务帐号,将该服务帐号与您的零触摸注册帐号结合使用。

第 3 步:准备项目

请按照以下步骤设置 Gradle 项目:

  1. 运行以下命令以在工作目录中创建新项目:

    gradle init --type basic
    mkdir -p src/main/java src/main/resources
    
  2. 将您在第 1 步中下载的 service_account_key.json 文件复制到上面创建的 src/main/resources/ 目录中。

  3. 打开默认的 build.gradle 文件,并将其内容替换为以下代码:

    apply plugin: 'java'
    apply plugin: 'application'
    
    mainClassName = 'ResellerQuickstart'
    sourceCompatibility = 1.7
    targetCompatibility = 1.7
    version = '1.0'
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        compile 'com.google.api-client:google-api-client:1.30.11'
        compile 'com.google.apis:google-api-services-androiddeviceprovisioning:+'
        compile 'com.google.oauth-client:google-oauth-client-jetty:+'
    }
    

第 4 步:设置示例

创建一个名为 src/main/java/ResellerQuickstart.java 的文件,然后复制以下代码并保存该文件。插入您自己的转销商合作伙伴 ID 作为 PARTNER_ID(应用的第一行)的值。

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.androiddeviceprovisioning.v1.AndroidProvisioningPartner;
import com.google.api.services.androiddeviceprovisioning.v1.model.Company;
import com.google.api.services.androiddeviceprovisioning.v1.model.ListCustomersResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;

/**
 * This class forms the quickstart introduction to the zero-touch enrollemnt
 * reseller API.
 */
public class ResellerQuickstart {

  // TODO: replace this with your partner reseller ID.
  private static long PARTNER_ID = 11036885;

  // Use a single scope for the all methods in the reseller API.
  private static final List<String> SCOPES =
      Arrays.asList("https://www.googleapis.com/auth/androidworkprovisioning");
  private static final String APP_NAME = "Zero-touch Reseller Java Quickstart";

  // Global shared instances.
  private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
  private static HttpTransport HTTP_TRANSPORT;

  static {
    try {
      HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
    } catch (Throwable t) {
      t.printStackTrace();
      System.exit(1);
    }
  }

  /**
   * Creates a Credential object with the correct OAuth2 authorization
   * for the service account that calls the reseller API. The service
   * endpoint invokes this method when setting up a new service instance.
   * @return an authorized Credential object.
   * @throws IOException
   */
  public static Credential authorize() throws IOException {
      // Load the service account key from the JSON file.
      InputStream in =
          ResellerQuickstart.class.getResourceAsStream("/service_account_key.json");

      // Create the credential scoped to the zero-touch enrollemnt
      // reseller APIs.
      GoogleCredential credential = GoogleCredential
         .fromStream(in)
         .createScoped(SCOPES);
      return credential;
  }

  /**
   * Builds and returns an authorized zero-touch enrollment API client service.
   * Use the service endpoint to call the API methods.
   * @return an authorized client service endpoint
   * @throws IOException
   */
  public static AndroidProvisioningPartner getService() throws IOException {
    Credential credential = authorize();
    return new AndroidProvisioningPartner.Builder(
        HTTP_TRANSPORT, JSON_FACTORY, credential)
        .setApplicationName(APP_NAME)
        .build();
  }

  /**
   * Runs the zero-touch enrollment quickstart app.
   * @throws IOException
   */
  public static void main(String[] args) throws IOException {

    // Create a zero-touch enrollment API service endpoint.
    AndroidProvisioningPartner service = getService();

    // Send an API request to list all our customers.
    AndroidProvisioningPartner.Partners.Customers.List request =
          service.partners().customers().list(PARTNER_ID);
    ListCustomersResponse response = request.execute();

    // Print out the details of each customer.
    if (response.getCustomers() != null) {
      java.util.List<Company> customers = response.getCustomers();
      for (Company customer : customers) {
          System.out.format("Name:%s  ID:%d\n",
                customer.getCompanyName(),
                customer.getCompanyId());
      }
    } else {
      System.out.println("No customers found");
    }
  }
}

合作伙伴 ID

API 调用通常需要您的转销商合作伙伴 ID 作为参数。如需从零触摸注册门户查找合作伙伴 ID,请按以下步骤操作:

  1. 打开门户网站。您可能需要登录。
  2. 点击 服务帐号
  3. 复制您的转销商 ID 行中的合作伙伴 ID 编号。

第 5 步:运行示例

在操作系统的帮助下,运行文件中的脚本。在 UNIX 和 Mac 计算机上,在终端中运行以下命令:

gradle -q run

问题排查

告诉我们快速入门中的问题,我们将努力予以解决。如需了解零触摸如何使用服务帐号对 API 调用进行授权,请参阅授权

了解详情