การเริ่มต้น Python อย่างรวดเร็ว

การเริ่มต้นอย่างรวดเร็วจะอธิบายถึงการตั้งค่าและเรียกใช้แอปที่เรียก Google Workspace API

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

สร้างแอปพลิเคชันบรรทัดคําสั่ง Python ที่ส่งคําขอไปยัง Google Apps Script API

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

  • ตั้งค่าสภาพแวดล้อมของคุณ
  • ติดตั้งไลบรารีของไคลเอ็นต์
  • ตั้งค่าตัวอย่าง
  • เรียกใช้ตัวอย่าง

สิ่งที่ต้องดำเนินการก่อน

หากต้องการเรียกใช้การเริ่มต้นอย่างรวดเร็วนี้ คุณต้องมีข้อกําหนดเบื้องต้นต่อไปนี้

  • บัญชี Google ที่เปิดใช้ Google ไดรฟ์

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

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

เปิดใช้ API

ก่อนใช้ Google APIs คุณต้องเปิดใช้งาน API ดังกล่าวในโปรเจ็กต์ Google Cloud คุณจะเปิดใช้ API อย่างน้อย 1 รายการในโปรเจ็กต์ Google Cloud เดียวได้

ให้สิทธิ์ข้อมูลรับรองสําหรับแอปพลิเคชันเดสก์ท็อป

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

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

  2. คลิกสร้างข้อมูลเข้าสู่ระบบ > รหัสไคลเอ็นต์ OAuth
  3. คลิกประเภทแอปพลิเคชัน > แอปบนเดสก์ท็อป
  4. ในช่องชื่อ ให้พิมพ์ชื่อของข้อมูลเข้าสู่ระบบ ชื่อนี้จะปรากฏเฉพาะในคอนโซล Google Cloud
  5. คลิกสร้าง หน้าจอไคลเอ็นต์ OAuth จะปรากฏขึ้นเพื่อแสดงรหัสไคลเอ็นต์และรหัสลับไคลเอ็นต์ใหม่
  6. คลิกตกลง ข้อมูลรับรองที่สร้างขึ้นใหม่จะปรากฏในรหัสไคลเอ็นต์ OAuth 2.0
  7. บันทึกไฟล์ JSON ที่ดาวน์โหลดไว้เป็น credentials.json และย้ายไฟล์ไปยังไดเรกทอรีที่ทํางานอยู่

ติดตั้งไลบรารีของไคลเอ็นต์ Google

  • ติดตั้งไลบรารีของไคลเอ็นต์ Google สําหรับ Python

    pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib
    

กําหนดค่าตัวอย่าง

  1. ในไดเรกทอรีชื่อ ให้สร้างไฟล์ชื่อ quickstart.py
  2. รวมรหัสต่อไปนี้ใน quickstart.py:

    apps_script/quickstart/quickstart.py
    """
    Shows basic usage of the Apps Script API.
    Call the Apps Script API to create a new script project, upload a file to the
    project, and log the script's URL to the user.
    """
    from __future__ import print_function
    
    import os.path
    
    from google.auth.transport.requests import Request
    from google.oauth2.credentials import Credentials
    from google_auth_oauthlib.flow import InstalledAppFlow
    from googleapiclient import errors
    from googleapiclient.discovery import build
    
    # If modifying these scopes, delete the file token.json.
    SCOPES = ['https://www.googleapis.com/auth/script.projects']
    
    SAMPLE_CODE = '''
    function helloWorld() {
      console.log("Hello, world!");
    }
    '''.strip()
    
    SAMPLE_MANIFEST = '''
    {
      "timeZone": "America/New_York",
      "exceptionLogging": "CLOUD"
    }
    '''.strip()
    
    
    def main():
        """Calls the Apps Script API.
        """
        creds = None
        # The file token.json stores the user's access and refresh tokens, and is
        # created automatically when the authorization flow completes for the first
        # time.
        if os.path.exists('token.json'):
            creds = Credentials.from_authorized_user_file('token.json', SCOPES)
        # If there are no (valid) credentials available, let the user log in.
        if not creds or not creds.valid:
            if creds and creds.expired and creds.refresh_token:
                creds.refresh(Request())
            else:
                flow = InstalledAppFlow.from_client_secrets_file(
                    'credentials.json', SCOPES)
                creds = flow.run_local_server(port=0)
            # Save the credentials for the next run
            with open('token.json', 'w') as token:
                token.write(creds.to_json())
    
        try:
            service = build('script', 'v1', credentials=creds)
    
            # Call the Apps Script API
            # Create a new project
            request = {'title': 'My Script'}
            response = service.projects().create(body=request).execute()
    
            # Upload two files to the project
            request = {
                'files': [{
                    'name': 'hello',
                    'type': 'SERVER_JS',
                    'source': SAMPLE_CODE
                }, {
                    'name': 'appsscript',
                    'type': 'JSON',
                    'source': SAMPLE_MANIFEST
                }]
            }
            response = service.projects().updateContent(
                body=request,
                scriptId=response['scriptId']).execute()
            print('https://script.google.com/d/' + response['scriptId'] + '/edit')
        except errors.HttpError as error:
            # The API encountered a problem.
            print(error.content)
    
    
    if __name__ == '__main__':
        main()

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

  1. สร้างและเรียกใช้ตัวอย่างในไดเรกทอรีการทํางาน

    python3 quickstart.py
    
  2. ครั้งแรกที่เรียกใช้ตัวอย่าง ระบบจะแจ้งให้คุณให้สิทธิ์เข้าถึง

    1. หากคุณยังไม่ได้ลงชื่อเข้าใช้บัญชี Google ของคุณ ระบบจะแจ้งให้คุณลงชื่อเข้าใช้ หากลงชื่อเข้าใช้หลายบัญชี ให้เลือกบัญชีที่จะใช้เพื่อให้สิทธิ์
    2. คลิกยอมรับ

    ข้อมูลการให้สิทธิ์จะจัดเก็บไว้ในระบบไฟล์ ดังนั้น เมื่อคุณเรียกใช้โค้ดตัวอย่างครั้งต่อไป จึงไม่ได้รับแจ้งให้ให้สิทธิ์

คุณสร้างแอปพลิเคชัน Python แรกที่ส่งคําขอไปยัง Google Apps Script API สําเร็จแล้ว

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