دليل البدء السريع لـ Java للعملاء

اتبع الخطوات الواردة في دليل البدء السريع هذا، وفي حوالي 10 دقائق سيكون لديك هو تطبيق بسيط يستند إلى سطر أوامر Java لإرسال طلبات إلى برنامج "إعداد الأجهزة الجوّالة للمؤسسات دفعةً واحدة" تسجيل واجهة برمجة تطبيقات العميل.

المتطلبات الأساسية

لتشغيل هذه البداية السريعة، تحتاج إلى:

الخطوة 1: تفعيل واجهة برمجة التطبيقات لبرنامج "إعداد الأجهزة الجوّالة للمؤسسات دفعةً واحدة"

  1. استخدام هذه الصفحة لإنشاء أو اختيار مشروع في Google Developers Console على تفعيل واجهة برمجة التطبيقات تلقائيًا. انقر على متابعة، ثم الانتقال إلى بيانات الاعتماد. .
  2. انقر على إلغاء في إنشاء بيانات الاعتماد.
  3. في أعلى الصفحة، اختَر علامة التبويب شاشة موافقة OAuth. اختَر عنوان البريد الإلكتروني، وأدخِل اسم منتج إذا لم يسبق تحديده. انقر على الزر حفظ.
  4. اختَر علامة التبويب بيانات الاعتماد، وانقر على إنشاء بيانات اعتماد. اختَر معرِّف عميل OAuth.
  5. اختَر نوع التطبيق غير ذلك، وأدخِل الاسم. "Quickstart" (التشغيل السريع)، وانقر على Create (إنشاء) .
  6. انقر على حسنًا لإغلاق لوحة عميل OAuth.
  7. انقر على تنزيل ملف JSON.
  8. انقل الملف إلى دليل العمل وأعِد تسميته client_secret.json.

الخطوة 2: إعداد المشروع

اتبع الخطوات أدناه لإعداد مشروع Gradle الخاص بك:

  1. شغِّل الأمر التالي لإنشاء مشروع جديد في دليل العمل:

    gradle init --type basic
    mkdir -p src/main/java src/main/resources
    
  2. انسخ الملف client_secret.json الذي نزّلته في الخطوة 1 إلى ملف دليل src/main/resources/ الذي أنشأته أعلاه.

  3. افتح ملف build.gradle التلقائي واستبدِل محتواه الرمز التالي:

apply plugin: 'java'
apply plugin: 'application'

mainClassName = 'CustomerQuickstart'
sourceCompatibility = 1.7
targetCompatibility = 1.7
version = '1.0'

repositories {
    mavenCentral()
}

dependencies {
    compile 'com.google.api-client:google-api-client:2.2.0'
    compile 'com.google.apis:google-api-services-androiddeviceprovisioning:v1-rev20230509-2.0.0'
    compile 'com.google.oauth-client:google-oauth-client-jetty:1.34.1'
}

الخطوة 3: إعداد العيّنة

أنشئ ملفًا باسم "src/main/java/CustomerQuickstart.java" وانسخه في باتباع التعليمات البرمجية وحفظ الملف.

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
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.gson.GsonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
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.CustomerListCustomersResponse;
import com.google.api.services.androiddeviceprovisioning.v1.model.CustomerListDpcsResponse;
import com.google.api.services.androiddeviceprovisioning.v1.model.Dpc;
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 customer API. */
public class CustomerQuickstart {

  // A single auth scope is used for the zero-touch enrollment customer API.
  private static final List<String> SCOPES =
      Arrays.asList("https://www.googleapis.com/auth/androidworkzerotouchemm");
  private static final String APP_NAME = "Zero-touch Enrollment Java Quickstart";
  private static final java.io.File DATA_STORE_DIR =
      new java.io.File(System.getProperty("user.home"), ".credentials/zero-touch.quickstart.json");

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

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

  /**
   * Creates a Credential object with the correct OAuth2 authorization for the user calling the
   * customer 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 client secrets.
    InputStream in = CustomerQuickstart.class.getResourceAsStream("/client_secret.json");

    GoogleClientSecrets clientSecrets =
        GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in, "UTF-8"));

    // Ask the user to authorize the request using their Google Account
    // in their browser.
    GoogleAuthorizationCodeFlow flow =
        new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
            .setDataStoreFactory(DATA_STORE_FACTORY)
            .setAccessType("offline")
            .build();
    Credential credential =
        new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
    System.out.println("Credential file saved to: " + DATA_STORE_DIR.getAbsolutePath());
    return credential;
  }

  /**
   * Build and return 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();

    // Get the customer's account. Because a customer might have more
    // than one, limit the results to the first account found.
    AndroidProvisioningPartner.Customers.List accountRequest = service.customers().list();
    accountRequest.setPageSize(1);
    CustomerListCustomersResponse accountResponse = accountRequest.execute();
    if (accountResponse.getCustomers().isEmpty()) {
      // No accounts found for the user. Confirm the Google Account
      // that authorizes the request can access the zero-touch portal.
      System.out.println("No zero-touch enrollment account found.");
      System.exit(-1);
    }
    Company customer = accountResponse.getCustomers().get(0);
    String customerAccount = customer.getName();

    // Send an API request to list all the DPCs available using the customer account.
    AndroidProvisioningPartner.Customers.Dpcs.List request =
        service.customers().dpcs().list(customerAccount);
    CustomerListDpcsResponse response = request.execute();

    // Print out the details of each DPC.
    java.util.List<Dpc> dpcs = response.getDpcs();
    for (Dpc dpcApp : dpcs) {
      System.out.format("Name:%s  APK:%s\n", dpcApp.getDpcName(), dpcApp.getPackageName());
    }
  }
}

الخطوة 4: تنفيذ العيّنة

استخدِم مساعدة نظام التشغيل لتشغيل النص البرمجي في الملف. على نظامي التشغيل UNIX وMac أجهزة الكمبيوتر، قم بتشغيل الأمر أدناه في الوحدة الطرفية لديك:

gradle -q run

عند تشغيل التطبيق لأول مرة، ستحتاج إلى منح إذن الوصول:

  1. يحاول التطبيق فتح علامة تبويب جديدة في المتصفِّح التلقائي. وإذا لم يفلح ذلك، انسخ عنوان URL من وحدة التحكم وفتحها في متصفحك. إذا لم تكن قد سجّلت الدخول إلى حسابك على Google، يُطلب منك تسجيل الدخول. إذا كنت قد سجّلت الدخول إلى عدة حسابات على Google، ستطلب منك الصفحة اختيار حساب للتفويض.
  2. انقر على قبول.
  3. أغلِق علامة تبويب المتصفّح، وسيستمر التطبيق في العمل.

ملاحظات

  • ولأن مكتبة برامج واجهة Google API تخزن بيانات التفويض على نظام الملفات، فإن لا يطلب منك الإذن.
  • لإعادة ضبط بيانات تفويض التطبيق، احذف ملف ~/.credentials/zero-touch.quickstart.json وشغِّل التطبيق مرة أخرى.
  • يعد تدفق التفويض في البدء السريع هذا مثاليًا لتطبيق سطر الأوامر. للتعرّف على كيفية إضافة البطاقات، يُرجى اتّباع الخطوات التالية: التفويض إلى تطبيق ويب، راجع تطبيقات خادم الويب OAuth 2.0

تحديد المشاكل وحلّها

في ما يلي بعض الأمور الشائعة التي ستحتاج إلى التحقق منها. أخبرنا بالخطأ الذي حدث في دليل البدء السريع وسنعمل على إصلاحه.

  • يُرجى التحقّق من السماح بطلبات بيانات من واجهة برمجة التطبيقات باستخدام حساب Google نفسه المُستخدَم في حساب Google. حساب عميل مخصّص لعملية "إعداد الأجهزة الجوّالة للمؤسّسات دفعةً واحدة" جرِّب تسجيل الدخول إلى بوابة برنامج "إعداد الأجهزة الجوّالة للمؤسسات دفعةً واحدة" باستخدام حساب Google نفسه لاختبار الدخول.
  • يُرجى تأكيد قبول الحساب لأحدث بنود الخدمة في البوابة. عرض حسابات العملاء:

مزيد من المعلومات