ผสานรวมกับ UI ของไดรฟ์"เปิดด้วย"

เมื่อผู้ใช้เลือกไฟล์และคลิกรายการเมนู "เปิดด้วย" ของ UI ของไดรฟ์ ไดรฟ์จะเปลี่ยนเส้นทาง ผู้ใช้ไปยัง URL เปิดของแอปนั้นที่กำหนดไว้ในกำหนดค่าการผสานรวม UI ของไดรฟ์

หากเลือกช่อง "นำเข้า" เมื่อกำหนดค่าการผสานรวม UI ของไดรฟ์ ผู้ใช้จะเลือกชุดค่าผสมของไฟล์เฉพาะแอปและไฟล์ Google Workspace เพื่อเปิดได้ เมื่อกำหนดค่าการผสานรวม UI ของไดรฟ์ ระบบจะกำหนดไฟล์เฉพาะแอปในช่อง "ประเภท MIME เริ่มต้น" และ "นามสกุลไฟล์เริ่มต้น" ในขณะที่ไฟล์ Google Workspace จะกำหนดในช่อง "ประเภท MIME รอง" และ "นามสกุลไฟล์รอง"

สำหรับไฟล์แต่ละไฟล์ที่ผู้ใช้ต้องการเปิด ไดรฟ์จะตรวจสอบประเภท MIME เทียบกับประเภท MIME เริ่มต้นและรองที่คุณกำหนดไว้ ดังนี้

  • สำหรับประเภท MIME ที่กำหนดไว้ในช่อง "ประเภท MIME เริ่มต้น" ระบบจะส่งรหัสไฟล์ไปยังแอปของคุณ ดูข้อมูลเกี่ยวกับวิธีจัดการไฟล์เฉพาะแอปได้ที่จัดการ URL ที่เปิดสำหรับเอกสารเฉพาะแอป

  • สำหรับประเภท MIME ที่กำหนดไว้ในช่อง "ประเภท MIME รอง" UI ของไดรฟ์จะแสดงกล่องโต้ตอบที่ถามผู้ใช้ว่าต้องการแปลงไฟล์ Google Workspace เป็นไฟล์ประเภทใด เช่น หากคุณเลือกไฟล์ Google เอกสารใน UI ของไดรฟ์และช่อง "MIME ประเภทรอง" แนะนำว่าแอปของคุณรองรับ text/plain หรือ application/pdf UI ของไดรฟ์จะถามผู้ใช้ว่าต้องการแปลงเป็นข้อความธรรมดาหรือ PDF หรือไม่

    ดูข้อมูลเกี่ยวกับวิธีจัดการไฟล์ Google Workspace ได้ที่จัดการ URL ที่เปิดสำหรับเอกสาร Google Workspace ดูรายการเอกสาร Google Workspace และรูปแบบการแปลงประเภท MIME ได้ที่ส่งออกประเภท MIME สำหรับเอกสาร Google Workspace

จัดการ URL ที่เปิดสำหรับเอกสารเฉพาะแอป

ดังที่กล่าวไว้ในกําหนดค่าการผสานรวม UI ของไดรฟ์ แอปจะได้รับตัวแปรเทมเพลตพร้อมข้อมูลเพื่อให้แอปเปิด ไฟล์ได้ แอปของคุณจะได้รับชุดตัวแปรเทมเพลตเริ่มต้น ภายในพารามิเตอร์ state state ข้อมูลเริ่มต้นสำหรับ URL เปิดที่เฉพาะเจาะจงแอปมีดังนี้

{   "ids": ["ID"],   "resourceKeys":{"RESOURCE_KEYS":"RESOURCE_KEYS"},   "action":"open",   "userId":"USER_ID" } 

เอาต์พุตนี้มีค่าต่อไปนี้

  • ID: รหัสของโฟลเดอร์หลัก
  • RESOURCE_KEYS: พจนานุกรม JSON ของรหัสไฟล์ที่แมปกับ คีย์ทรัพยากรที่เกี่ยวข้อง
  • open: การดำเนินการที่กำลังทำ ค่าจะเป็น open เมื่อใช้ Open URL
  • USER_ID: รหัสโปรไฟล์ที่ระบุผู้ใช้ที่ไม่ซ้ำกัน

แอปของคุณต้องดำเนินการตามคำขอนี้โดยทำตามขั้นตอนต่อไปนี้

  1. ตรวจสอบว่าช่อง action มีค่าเป็น open และมีช่อง ids อยู่
  2. ใช้ค่า userId เพื่อสร้างเซสชันใหม่สำหรับผู้ใช้ ดูข้อมูลเพิ่มเติมเกี่ยวกับผู้ใช้ที่ลงชื่อเข้าใช้ได้ที่ผู้ใช้และเหตุการณ์ใหม่
  3. ใช้วิธี files.get เพื่อตรวจสอบสิทธิ์ ดึงข้อมูลเมตาของไฟล์ และดาวน์โหลดเนื้อหาของไฟล์โดยใช้ค่า ID
  4. หากตั้งค่า resourceKeys ในคำขอ ให้ตั้งค่าส่วนหัวของคำขอ X-Goog-Drive-Resource-Keys ดูข้อมูลเพิ่มเติมเกี่ยวกับ คีย์ทรัพยากรได้ที่หัวข้อเข้าถึงไฟล์ที่แชร์ลิงก์โดยใช้คีย์ ทรัพยากร

พารามิเตอร์ state มีการเข้ารหัส URL ดังนั้นแอปของคุณต้องจัดการอักขระหลีก และแยกวิเคราะห์เป็น JSON

จัดการ URL ที่เปิดสำหรับเอกสาร Google Workspace

ดังที่กล่าวไว้ในกำหนดค่าการผสานรวม UI ของไดรฟ์ แอปจะได้รับชุดตัวแปรเทมเพลตเริ่มต้นภายในพารามิเตอร์ state stateข้อมูลเริ่มต้น สำหรับ URL แบบเปิดของ Google Workspace มีดังนี้

{   "exportIds": ["ID"],   "resourceKeys":{"RESOURCE_KEYS":"RESOURCE_KEYS"},   "action":"open",   "userId":"USER_ID" } 

เอาต์พุตนี้มีค่าต่อไปนี้

  • EXPORT_ID: รายการรหัสไฟล์ที่คั่นด้วยคอมมาซึ่งจะ ส่งออก ใช้เมื่อเปิดไฟล์ Google Workspace เท่านั้น
  • RESOURCE_KEYS: พจนานุกรม JSON ของรหัสไฟล์ที่แมปกับ คีย์ทรัพยากรที่เกี่ยวข้อง
  • open: การดำเนินการที่กำลังทำ ค่าจะเป็น open เมื่อใช้ Open URL
  • USER_ID: รหัสโปรไฟล์ที่ระบุผู้ใช้

แอปของคุณต้องดำเนินการตามคำขอนี้โดยทำตามขั้นตอนต่อไปนี้

  1. ยืนยันว่านี่คือคำขอเปิดไฟล์โดยตรวจหาทั้งopen ค่าในช่อง state และการมีอยู่ของช่อง exportIds

  2. ใช้วิธี files.get เพื่อตรวจสอบสิทธิ์ ดึงข้อมูลเมตาของไฟล์ และกำหนดประเภท MIME โดยใช้ค่า EXPORT_ID

  3. แปลงเนื้อหาไฟล์โดยใช้เมธอด files.export ตัวอย่างโค้ดต่อไปนี้แสดงวิธีส่งออกเอกสาร Google Workspace ไปยัง MIME ประเภทที่ขอ

  4. หากตั้งค่า resourceKey ในคำขอ ให้ตั้งค่าส่วนหัวของคำขอ X-Goog-Drive-Resource-Keys ดูข้อมูลเพิ่มเติมเกี่ยวกับ คีย์ทรัพยากรได้ที่หัวข้อเข้าถึงไฟล์ที่แชร์ลิงก์โดยใช้คีย์ ทรัพยากร

    Java

    drive/snippets/drive_v3/src/main/java/ExportPdf.java
    import com.google.api.client.googleapis.json.GoogleJsonResponseException; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.gson.GsonFactory; import com.google.api.services.drive.Drive; import com.google.api.services.drive.DriveScopes; import com.google.auth.http.HttpCredentialsAdapter; import com.google.auth.oauth2.GoogleCredentials; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.Arrays;  /* Class to demonstrate use-case of drive's export pdf. */ public class ExportPdf {    /**    * Download a Document file in PDF format.    *    * @param realFileId file ID of any workspace document format file.    * @return byte array stream if successful, {@code null} otherwise.    * @throws IOException if service account credentials file not found.    */   public static ByteArrayOutputStream exportPdf(String realFileId) throws IOException {     // Load pre-authorized user credentials from the environment.     // TODO(developer) - See https://developers.google.com/identity for     // guides on implementing OAuth2 for your application.     GoogleCredentials credentials = GoogleCredentials.getApplicationDefault()         .createScoped(Arrays.asList(DriveScopes.DRIVE_FILE));     HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(         credentials);      // Build a new authorized API client service.     Drive service = new Drive.Builder(new NetHttpTransport(),         GsonFactory.getDefaultInstance(),         requestInitializer)         .setApplicationName("Drive samples")         .build();      OutputStream outputStream = new ByteArrayOutputStream();     try {       service.files().export(realFileId, "application/pdf")           .executeMediaAndDownloadTo(outputStream);        return (ByteArrayOutputStream) outputStream;     } catch (GoogleJsonResponseException e) {       // TODO(developer) - handle error appropriately       System.err.println("Unable to export file: " + e.getDetails());       throw e;     }   } }

    Python

    drive/snippets/drive-v3/file_snippet/export_pdf.py
    import io  import google.auth from googleapiclient.discovery import build from googleapiclient.errors import HttpError from googleapiclient.http import MediaIoBaseDownload   def export_pdf(real_file_id):   """Download a Document file in PDF format.   Args:       real_file_id : file ID of any workspace document format file   Returns : IO object with location    Load pre-authorized user credentials from the environment.   TODO(developer) - See https://developers.google.com/identity   for guides on implementing OAuth2 for the application.   """   creds, _ = google.auth.default()    try:     # create drive api client     service = build("drive", "v3", credentials=creds)      file_id = real_file_id      # pylint: disable=maybe-no-member     request = service.files().export_media(         fileId=file_id, mimeType="application/pdf"     )     file = io.BytesIO()     downloader = MediaIoBaseDownload(file, request)     done = False     while done is False:       status, done = downloader.next_chunk()       print(f"Download {int(status.progress() * 100)}.")    except HttpError as error:     print(f"An error occurred: {error}")     file = None    return file.getvalue()   if __name__ == "__main__":   export_pdf(real_file_id="1zbp8wAyuImX91Jt9mI-CAX_1TqkBLDEDcr2WeXBbKUY")

    Node.js

    drive/snippets/drive_v3/file_snippets/export_pdf.js
    /**  * Download a Document file in PDF format  * @param{string} fileId file ID  * @return{obj} file status  * */ async function exportPdf(fileId) {   const {GoogleAuth} = require('google-auth-library');   const {google} = require('googleapis');    // Get credentials and build service   // TODO (developer) - Use appropriate auth mechanism for your app   const auth = new GoogleAuth({     scopes: 'https://www.googleapis.com/auth/drive',   });   const service = google.drive({version: 'v3', auth});    try {     const result = await service.files.export({       fileId: fileId,       mimeType: 'application/pdf',     });     console.log(result.status);     return result;   } catch (err) {     // TODO(developer) - Handle error     throw err;   } }

    PHP

    drive/snippets/drive_v3/src/DriveExportPdf.php
    <?php use Google\Client; use Google\Service\Drive; function exportPdf() {     try {         $client = new Client();         $client->useApplicationDefaultCredentials();         $client->addScope(Drive::DRIVE);         $driveService = new Drive($client);         $realFileId = readline("Enter File Id: ");         $fileId = '1ZdR3L3qP4Bkq8noWLJHSr_iBau0DNT4Kli4SxNc2YEo';         $fileId = $realFileId;         $response = $driveService->files->export($fileId, 'application/pdf', array(             'alt' => 'media'));         $content = $response->getBody()->getContents();         return $content;      }  catch(Exception $e) {          echo "Error Message: ".$e;     }  }

แสดงไฟล์ที่แปลงแล้วเป็นแบบอ่านอย่างเดียวหรือแสดงกล่องโต้ตอบที่ให้ผู้ใช้ บันทึกไฟล์เป็นประเภทไฟล์ใหม่

พารามิเตอร์ state มีการเข้ารหัส URL ดังนั้นแอปของคุณต้องจัดการอักขระหลีก และแยกวิเคราะห์เป็น JSON

ผู้ใช้และเหตุการณ์ใหม่

แอปไดรฟ์ควรถือว่าเหตุการณ์ "เปิดด้วย" ทั้งหมดเป็นการลงชื่อเข้าใช้ที่อาจเกิดขึ้น ผู้ใช้บางรายอาจมีหลายบัญชี ดังนั้นรหัสผู้ใช้ในstate พารามิเตอร์อาจไม่ตรงกับเซสชันปัจจุบัน หากรหัสผู้ใช้ในพารามิเตอร์ state ไม่ตรงกับเซสชันปัจจุบัน ให้สิ้นสุดเซสชันปัจจุบันสำหรับแอป ของคุณ แล้วลงชื่อเข้าใช้ในฐานะผู้ใช้ที่ขอ

นอกเหนือจากการเปิดแอปพลิเคชันจาก UI ของ Google ไดรฟ์แล้ว แอปพลิเคชันยังสามารถ แสดงเครื่องมือเลือกไฟล์เพื่อเลือกเนื้อหาจากภายในแอปได้ด้วย ดูข้อมูลเพิ่มเติมได้ใน Google Picker