Not able to Download google drive file through golang api

270 Views Asked by At
// Download a file from Google Drive.
func downloadFile(service *drive.Service, fileID, filePath string) error {
    resp, err := service.Files.Get(fileID).Download()
    if err != nil {
        return err
    }
    defer resp.Body.Close()

    out, err := os.Create(filePath)
    if err != nil {
        return err
    }
    defer out.Close()

    _, err = io.Copy(out, resp.Body)
    if err != nil {
        return err
    }

    return nil
}

// Recursively download a folder from Google Drive.
func downloadFolder(service *drive.Service, folderID, localPath string) error {
    files, err := service.Files.List().Q(fmt.Sprintf("'%s' in parents", folderID)).Do()
    if err != nil {
        return err
    }

    for _, file := range files.Files {
        filePath := filepath.Join(localPath, file.Name)
        if file.MimeType == "application/vnd.google-apps.folder" {
            err := os.MkdirAll(filePath, os.ModePerm)
            if err != nil {
                return err
            }
            err = downloadFolder(service, file.Id, filePath)
            if err != nil {
                return err
            }
        } else {
            err := downloadFile(service, file.Id, filePath)
            if err != nil {
                return err
            }
        }
    }

    return nil
}

func main() {
    
    // Download a folder
    folderID := "1JEQe7LOoFCHXJw2I5IPkVd79W0ggI1zX"                    // Replace with the ID of the folder you want to download
    folderPath := "C:/Users/vinamra/Desktop/google-drive-api/download" // Replace with the desired local folder path
    err = os.MkdirAll(folderPath, os.ModePerm)
    if err != nil {
        log.Fatalf("Unable to create local folder: %v", err)
    }
    err = downloadFolder(srv, folderID, folderPath)
    if err != nil {
        log.Fatalf("Unable to download folder: %v", err)
    }
}

// [END drive_quickstart]

i am able to connect to google drive api and i am able to list my file. But when i want to download the file it is giving me error of
Error 403: The user has not granted the app read access to the file

i was expecting for the file to download. please can someone help me what is wrong

0

There are 0 best solutions below