เริ่มต้นใช้งาน JavaScript

สร้างเว็บแอปพลิเคชัน JavaScript ที่ส่งคำขอไปยัง Reseller API

คู่มือเริ่มต้นใช้งานอธิบายวิธีตั้งค่าและเรียกใช้แอปที่เรียกใช้ Google Workspace API คู่มือเริ่มต้นใช้งานนี้ใช้วิธีการตรวจสอบสิทธิ์แบบง่ายซึ่งเหมาะกับสภาพแวดล้อมการทดสอบ สำหรับสภาพแวดล้อมการใช้งานจริง เราขอแนะนำให้คุณศึกษาเกี่ยวกับ การตรวจสอบสิทธิ์และการให้สิทธิ์ ก่อน เลือกข้อมูลเข้าสู่ระบบ ที่เหมาะสมกับแอป

คู่มือเริ่มต้นใช้งานนี้ใช้ไลบรารีไคลเอ็นต์ API ที่แนะนำของ Google Workspace เพื่อจัดการรายละเอียดบางอย่างของขั้นตอนการตรวจสอบสิทธิ์และการให้สิทธิ์

วัตถุประสงค์

  • ตั้งค่าสภาพแวดล้อม
  • ตั้งค่าตัวอย่าง
  • เรียกใช้ตัวอย่าง

ข้อกำหนดเบื้องต้น

  • อินสแตนซ์โดเมนตัวแทนจำหน่าย Google
  • ข้อตกลงการเป็นพาร์ทเนอร์ Google Workspace ที่ดำเนินการอย่างสมบูรณ์

ตั้งค่าสภาพแวดล้อม

ตั้งค่าสภาพแวดล้อมเพื่อทำคู่มือเริ่มต้นใช้งานนี้ให้เสร็จสมบูรณ์

เปิดใช้ API

ก่อนใช้ Google API คุณต้องเปิด API เหล่านั้นในโปรเจ็กต์ที่อยู่ในระบบคลาวด์ของ Google คุณสามารถเปิด API อย่างน้อย 1 รายการในโปรเจ็กต์ Google Cloud เดียว

กำหนดค่าหน้าจอขอคำยินยอม OAuth

หากใช้โปรเจ็กต์ Google Cloud ใหม่เพื่อทำคู่มือเริ่มต้นใช้งานนี้ให้เสร็จสมบูรณ์ ให้กำหนดค่าหน้าจอขอคำยินยอม OAuth หากทำขั้นตอนนี้สำหรับโปรเจ็กต์ Cloud เสร็จแล้ว ให้ข้ามไปยังส่วนถัดไป

  1. ในคอนโซล Google API ให้ไปที่เมนู > แพลตฟอร์มการตรวจสอบสิทธิ์ของ Google > การสร้างแบรนด์

    ไปที่การสร้างแบรนด์

  2. หากกำหนดค่าแพลตฟอร์มการตรวจสอบสิทธิ์ของ Google ไว้แล้ว คุณสามารถกำหนดค่าการตั้งค่าหน้าจอขอคำยินยอม OAuth ต่อไปนี้ใน การสร้างแบรนด์, กลุ่มเป้าหมาย และ การเข้าถึงข้อมูล หากเห็นข้อความที่ระบุว่า Google Auth platform not configured yet ให้คลิก Get Started ดังนี้
    1. ในส่วนข้อมูลแอป ในชื่อแอป ให้ป้อนชื่อแอป
    2. ในส่วนอีเมลสนับสนุนสำหรับผู้ใช้ ให้เลือกอีเมลสนับสนุนที่ผู้ใช้ติดต่อคุณได้หากมีคำถามเกี่ยวกับความยินยอม
    3. คลิกถัดไป
    4. ในส่วนกลุ่มเป้าหมาย ให้เลือกภายใน
    5. คลิกถัดไป
    6. ในส่วนข้อมูลติดต่อ ให้ป้อนอีเมล ที่คุณจะได้รับการแจ้งเตือนเกี่ยวกับการเปลี่ยนแปลงในโปรเจ็กต์
    7. คลิกถัดไป
    8. ในส่วนเสร็จสิ้น ให้อ่านนโยบายข้อมูลผู้ใช้สำหรับบริการ Google API และหากยอมรับ ให้เลือกฉันยอมรับบริการ Google API: นโยบายข้อมูลผู้ใช้
    9. คลิกต่อไป
    10. คลิกสร้าง
  3. ตอนนี้คุณสามารถข้ามการเพิ่มขอบเขตได้ ในอนาคต เมื่อสร้างแอปเพื่อใช้ภายนอกองค์กร Google Workspace คุณต้องเปลี่ยนประเภทผู้ใช้ เป็นภายนอก จากนั้น เพิ่มขอบเขตการให้สิทธิ์ที่แอปต้องใช้ ดูข้อมูลเพิ่มเติมได้ในคู่มือฉบับเต็มเกี่ยวกับ การกำหนดค่าหน้าจอขอคำยินยอม OAuth

ให้สิทธิ์ข้อมูลเข้าสู่ระบบสำหรับเว็บแอปพลิเคชัน

หากต้องการตรวจสอบสิทธิ์ผู้ใช้ปลายทางและเข้าถึงข้อมูลผู้ใช้ในแอป คุณต้องสร้างรหัสไคลเอ็นต์ OAuth 2.0 อย่างน้อย 1 รายการ รหัสไคลเอ็นต์ใช้เพื่อระบุแอปเดี่ยวไปยังเซิร์ฟเวอร์ OAuth ของ Google หากแอปทำงานบนหลายแพลตฟอร์ม คุณต้องสร้างรหัสไคลเอ็นต์แยกต่างหากสำหรับแต่ละแพลตฟอร์ม
  1. ในคอนโซล Google Cloud ให้ไปที่เมนู > แพลตฟอร์มการตรวจสอบสิทธิ์ของ Google > ไคลเอ็นต์

    ไปที่ไคลเอ็นต์

  2. คลิกสร้างไคลเอ็นต์
  3. คลิกประเภทแอปพลิเคชัน > เว็บแอปพลิเคชัน
  4. พิมพ์ชื่อของข้อมูลเข้าสู่ระบบในช่องชื่อ ชื่อนี้จะแสดงในคอนโซล Google Cloud เท่านั้น
  5. เพิ่ม URI ที่ได้รับอนุญาตซึ่งเกี่ยวข้องกับแอป โดยทำดังนี้
    • แอปฝั่งไคลเอ็นต์ (JavaScript)–ในส่วนต้นทาง JavaScript ที่ได้รับอนุญาต ให้คลิกเพิ่ม URI จากนั้นป้อน URI ที่จะใช้สำหรับคำขอของเบราว์เซอร์ ซึ่งจะระบุโดเมนที่แอปพลิเคชันของคุณสามารถส่งคำขอ API ไปยังเซิร์ฟเวอร์ OAuth 2.0 ได้
    • แอปฝั่งเซิร์ฟเวอร์ (Java, Python และอื่นๆ)–ในส่วนURI การเปลี่ยนเส้นทางที่ได้รับอนุญาต ให้คลิกเพิ่ม URI จากนั้นป้อน URI ปลายทางที่เซิร์ฟเวอร์ OAuth 2.0 สามารถส่งการตอบกลับได้
  6. คลิกสร้าง

    ข้อมูลเข้าสู่ระบบที่สร้างขึ้นใหม่จะปรากฏในส่วนรหัสไคลเอ็นต์ OAuth 2.0

    โปรดทราบว่าระบบจะไม่ใช้รหัสลับไคลเอ็นต์สำหรับเว็บแอปพลิเคชัน

  7. บันทึกรหัสไคลเอ็นต์ไว้เพื่อใช้ในภายหลังในคู่มือเริ่มต้นใช้งานนี้

สร้างคีย์ API

  1. ในคอนโซล Google Cloud ให้ไปที่เมนู > API และบริการ > ข้อมูลเข้าสู่ระบบ

    ไปที่ข้อมูลเข้าสู่ระบบ

  2. คลิกสร้างข้อมูลเข้าสู่ระบบ > คีย์ API
  3. คีย์ API ใหม่จะแสดงขึ้น
    • คลิกคัดลอก เพื่อคัดลอกคีย์ API ไปใช้ในโค้ดของแอป นอกจากนี้ คุณยังดูคีย์ API ได้ในส่วน "คีย์ API" ของ ข้อมูลเข้าสู่ระบบของโปรเจ็กต์
    • เราขอแนะนำให้จำกัดตำแหน่งและ API ที่สามารถใช้คีย์ API ได้ เพื่อป้องกันการใช้งานที่ไม่ได้รับอนุญาต ดูรายละเอียดเพิ่มเติมได้ที่ เพิ่มข้อจำกัด API

ตั้งค่าตัวอย่าง

  1. สร้างไฟล์ชื่อ index.html ในไดเรกทอรีการทำงาน
  2. วางโค้ดตัวอย่างต่อไปนี้ในไฟล์ index.html

    adminSDK/reseller/index.html
    <!DOCTYPE html>
    <html>
      <head>
        <title>Google Workspace Reseller API Quickstart</title>
        <meta charset="utf-8" />
      </head>
      <body>
        <p>Google WOrkspace Reseller API Quickstart</p>
    
        <!--Add buttons to initiate auth sequence and sign out-->
        <button id="authorize_button" onclick="handleAuthClick()">Authorize</button>
        <button id="signout_button" onclick="handleSignoutClick()">Sign Out</button>
    
        <pre id="content" style="white-space: pre-wrap;"></pre>
    
        <script type="text/javascript">
          /* exported gapiLoaded */
          /* exported gisLoaded */
          /* exported handleAuthClick */
          /* exported handleSignoutClick */
    
          // TODO(developer): Set to client ID and API key from the Developer Console
          const CLIENT_ID = '<YOUR_CLIENT_ID>';
          const API_KEY = '<YOUR_API_KEY>';
    
          // Discovery doc URL for APIs used by the quickstart
          const DISCOVERY_DOC = 'https://www.googleapis.com/discovery/v1/apis/reseller/v1/rest';
    
          // Authorization scopes required by the API; multiple scopes can be
          // included, separated by spaces.
          const SCOPES = 'https://www.googleapis.com/auth/apps.order';
    
          let tokenClient;
          let gapiInited = false;
          let gisInited = false;
    
          document.getElementById('authorize_button').style.visibility = 'hidden';
          document.getElementById('signout_button').style.visibility = 'hidden';
    
          /**
           * Callback after api.js is loaded.
           */
          function gapiLoaded() {
            gapi.load('client', initializeGapiClient);
          }
    
          /**
           * Callback after the API client is loaded. Loads the
           * discovery doc to initialize the API.
           */
          async function initializeGapiClient() {
            await gapi.client.init({
              apiKey: API_KEY,
              discoveryDocs: [DISCOVERY_DOC],
            });
            gapiInited = true;
            maybeEnableButtons();
          }
    
          /**
           * Callback after Google Identity Services are loaded.
           */
          function gisLoaded() {
            tokenClient = google.accounts.oauth2.initTokenClient({
              client_id: CLIENT_ID,
              scope: SCOPES,
              callback: '', // defined later
            });
            gisInited = true;
            maybeEnableButtons();
          }
    
          /**
           * Enables user interaction after all libraries are loaded.
           */
          function maybeEnableButtons() {
            if (gapiInited && gisInited) {
              document.getElementById('authorize_button').style.visibility = 'visible';
            }
          }
    
          /**
           *  Sign in the user upon button click.
           */
          function handleAuthClick() {
            tokenClient.callback = async (resp) => {
              if (resp.error !== undefined) {
                throw (resp);
              }
              document.getElementById('signout_button').style.visibility = 'visible';
              document.getElementById('authorize_button').innerText = 'Refresh';
              await listSubscriptions();
            };
    
            if (gapi.client.getToken() === null) {
              // Prompt the user to select a Google Account and ask for consent to share their data
              // when establishing a new session.
              tokenClient.requestAccessToken({prompt: 'consent'});
            } else {
              // Skip display of account chooser and consent dialog for an existing session.
              tokenClient.requestAccessToken({prompt: ''});
            }
          }
    
          /**
           *  Sign out the user upon button click.
           */
          function handleSignoutClick() {
            const token = gapi.client.getToken();
            if (token !== null) {
              google.accounts.oauth2.revoke(token.access_token);
              gapi.client.setToken('');
              document.getElementById('content').innerText = '';
              document.getElementById('authorize_button').innerText = 'Authorize';
              document.getElementById('signout_button').style.visibility = 'hidden';
            }
          }
    
          /**
           * Print first ten subscriptions.
           */
          async function listSubscriptions() {
            let response;
            try {
              response = await gapi.client.reseller.subscriptions.list({
                'maxResults': 10,
              });
            } catch (err) {
              document.getElementById('content').innerText = err.message;
              return;
            }
    
            const subscriptions = response.result.subscriptions;
            if (!subscriptions || subscriptions.length == 0) {
              document.getElementById('content').innerText = 'No subscriptions found.';
              return;
            }
            // Flatten to string to display
            const output = subscriptions.reduce(
                (str, subscription) => {
                  const customer = subscription.customerId;
                  const sku = subscription.skuId;
                  const plan = subscription.plan.planName;
                  return `${str}${customer} (${sku},${plan})\n`;
                },
                'Subscriptions:\n');
            document.getElementById('content').innerText = output;
          }
        </script>
        <script async defer src="https://apis.google.com/js/api.js" onload="gapiLoaded()"></script>
        <script async defer src="https://accounts.google.com/gsi/client" onload="gisLoaded()"></script>
      </body>
    </html>

    แทนที่ค่าต่อไปนี้

เรียกใช้ตัวอย่าง

  1. ติดตั้งแพ็กเกจ http-server ในไดเรกทอรีการทำงาน โดยทำดังนี้

    npm install http-server
    
  2. เริ่มเว็บเซิร์ฟเวอร์ในไดเรกทอรีการทำงาน โดยทำดังนี้

    npx http-server -p 8000
    
  1. ในเบราว์เซอร์ ให้ไปที่ http://localhost:8000
  2. คุณจะเห็นข้อความแจ้งให้ให้สิทธิ์เข้าถึง โดยทำดังนี้
    1. หากยังไม่ได้ลงชื่อเข้าใช้บัญชี Google ให้ลงชื่อเข้าใช้เมื่อได้รับข้อความแจ้ง หากลงชื่อเข้าใช้ไว้หลายบัญชี ให้เลือกบัญชีที่จะใช้ในการให้สิทธิ์
    2. คลิกยอมรับ

แอปพลิเคชัน JavaScript จะทำงานและเรียกใช้ Reseller API

ขั้นตอนถัดไป