จะสตรีมเพลงในแอปของฉันจากลิงก์ Google Drive และทำให้ลิงก์เข้าถึงได้อย่างไร

Aug 16 2020

บนเว็บไซต์ผู้ใช้สามารถอัพโหลดเพลง เพลงนี้กำลังอัปโหลดในบัญชี Google ไดรฟ์ของฉัน หลังจากนั้นรูปแบบลิงก์ต่อไปนี้จะถูกเขียนลงในฐานข้อมูล:

https://drive.google.com/uc?id=ID_GOES_HERE

ตอนนี้เมื่อคุณเข้าถึงลิงค์มันจะเปลี่ยนไปในลักษณะนี้:

https://doc-14-8s-docs.googleusercontent.com/docs/securesc/m4p0h6h3bpkoufgbajruvng9n4f51clu/fnjf7ftnl18svtcblses77hapi91jpe5/1597565925000/10139445714759083419/10139445714759083419/1mLu_myEgts9eaoWv6S9nxu8n4qfZM0Rr?authuser=0

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

ขอขอบคุณ!

    <form id="form">
  <input name="file" id="uploadfile" type="file">
  <input name="filename" id="filename" type="text">
  <input id="submit" type="submit">
</form>
<script>
const form = document.getElementById('form');
form.addEventListener('submit', e => {
  e.preventDefault();
  const file = form.file.files[0];
  const fr = new FileReader();
  fr.readAsArrayBuffer(file);
  fr.onload = f => {
    
    const url =   "https://script.google.com/macros/s/###/exec";  // <---    Please set the URL of Web Apps.

    const qs = new URLSearchParams({filename:    form.filename.value || file.name, mimeType: file.type});
fetch(`${url}?${qs}`, {method: "POST", body: JSON.stringify([...new Int8Array(f.target.result)])})
.then(res => res.json())
.then(e => console.log("https://drive.google.com/uc?   export=download&id=" + e.fileId))
    .catch(err => console.log(err));
  }
});
</script>

นี่คือสคริปต์แอป Google

function doPost(e) {
  const folderId = "root";  // Folder ID which is used for   putting the file, if you need.

  const blob =   Utilities.newBlob(JSON.parse(e.postData.contents),   e.parameter.mimeType, e.parameter.filename);
  const file = DriveApp.getFolderById(folderId ||  "root").createFile(blob);
  const responseObj = {filename: file.getName(), fileId:  file.getId(), fileUrl: file.getUrl()};
  return   ContentService.createTextOutput(JSON.stringify(responseObj  )).setMimeType(ContentService.MimeType.JSON);
}

------- แก้ไข -------

ฉันลองวิธีแก้ปัญหาที่กำหนด:

น่าเศร้าที่มันไม่ได้ผลตามที่คาดไว้ .. ฉันได้รับลิงค์ต่อไปนี้:

https://drive.google.com/uc?export=download&id=1Tnh8UgAzFKrp6e-8QCl21v16L3yVCqso 

ตอนนี้เมื่อฉันใช้รหัสจากมันและแทรกที่นี่:

http://docs.google.com/uc?export=open&id=your_file_id 

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

https://doc-08-8s-docs.googleusercontent.com/docs/securesc/m4p0h6h3bpkoufgbajruvng9n4f51clu/cgag5dus6c18p9hc1hv0362urudm2g6t/1597730475000/10139445714759083419/10139445714759083419/1Tnh8UgAzFKrp6e-8QCl21v16L3yVCqso?e=open&authuser=0 

จะทำให้ลิงค์นี้เข้าถึงได้อย่างไร?

ดูเหมือนว่าลิงก์นี้จะมีสิทธิ์ที่คุณตั้งไว้ http://docs.google.com/uc?export=open&id=your_file_id แต่หากคุณคัดลอกลิงก์แบบยาวลิงก์นี้ไม่ได้ให้การตั้งค่าการอนุญาตก็ยังคงบล็อก

คำตอบ

2 Viney Aug 16 2020 at 12:29

หลังจากสร้างไฟล์ของคุณแล้วคุณจะต้องให้ระดับการเข้าถึงที่เหมาะสมซึ่งจะกำหนดว่าใครสามารถเข้าถึงได้และระดับสิทธิ์ที่กำหนดว่าพวกเขาสามารถทำอะไรกับไฟล์นั้นได้ (อ่าน / เขียน / ลบ)

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

const file = DriveApp.getFolderById(folderId ||  "root").createFile(blob);
file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW); // <-- add this