somia
somia

Reputation: 193

Direct link to a file located on Google Drive

What I want to do is to create a direct link URL to a mp3 file which is located on my Google Drive and use it on Audio object of HTML5, but I get 403 error.

I know that when one tries to create a link of a file located on Google Drive, it creates not a direct URL to the file but a URL for viewing the file through a viewer such as

https://drive.google.com/file/d/<file ID>/view?usp=sharing

I googled to find it is possible to rewrite it into a direct link URL like this:

https://drive.google.com/uc?id=<file ID>

I set this URL in the src property of my audio element. However, when I try play() method, the following error is thrown:

GET https://drive.google.com/uc?id=<file ID> 403

myProject.html:1 Uncaught (in promise) DOMException: Failed to load because no supported source was found.

So I tried to access to the URL https://drive.google.com/uc?id=<file ID> from my browser.

Then, I got this:

403. That’s an error.

We're sorry, but you do not have access to this page. That’s all we know.

I tried many times so it is not likely that I'm mis-pasting the <file ID>.

What should I do to create a valid direct link URL of the file? I would appreciate for any information.

Progress

I got what was wrong. The problem was that a file on Google Drive can be accessed only from the user who is authorized, which means only the owner of the Google account can access the file URL.

I tried to access it from Chrome Browser which is associated with the Google account, then, the error didn't occur.

However, I want to serve this file to all the people. What should I do to give permission for access to other people?

Upvotes: 1

Views: 2726

Answers (2)

somia
somia

Reputation: 193

I solved this on my own. I right clicked the file, clicked Get link, and changed the authorization selection from Restricted into Anyone with the link. Then, the 403 error vanished for the access from anyone.

Upvotes: 1

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 117301

You understand that Google drive is not a file hosting service right? This solution is not going to be very stable even if you do get it to work.

For it to work your going to need to set the file public so that everyone can access it. Then i would be willing to bet you will need an API key to do this in the long run.

Also remember that file id is not stable it can change in the future if for example you upload the file.

Upvotes: 2

Related Questions