yongwoon
yongwoon

Reputation: 155

How to download audio file?

I want to download audio files (mp3, m4a, wma...) with Google Drive API. Trying following code, I got the error on simulator.

Error:

The operation could't be completed. (The user has not granted the app xxxx read access to the file alEnlflseiFNSEi.)

Code:

let url = "https://www.googleapis.com/drive/v3/files/\(identifier)?alt=media"

service.fetchObject(
    with: URL(string: url)!,
    delegate: self,
    didFinish: #selector(GoogleDriveFileListTableViewController.downloadWithTicket(ticket:finishedWithObject:error:))
)

// Parse results and display
func downloadWithTicket(ticket : GTLServiceTicket, finishedWithObject response : GTLDriveFileList, error : NSError?) {
    if let error = error {
        showAlert(title: "Error", message: error.localizedDescription)
        return
    }

}

Upvotes: 2

Views: 732

Answers (1)

ReyAnthonyRenacia
ReyAnthonyRenacia

Reputation: 17613

With regard to your "read access" problem here's what the Download files states:

Downloading the file requires the user to have at least read access. Additionally, your app must be authorized with a scope that allows reading of file content. For example, an app using the drive.readonly.metadata scope would not be authorized to download the file contents. Users with edit permission may restrict downloading by read-only users by setting the viewersCanCopyContent field to true.

Try obtaining the webContentLink of the audio file using Files.get. When this link is clicked or opened in a new window, the audio file will be downloaded.

It looks something like this:

https://drive.google.com/uc?id=0Bzgk4zccNwI7TzBxZTFzbHFhdUU&export=download

Upvotes: 1

Related Questions