let me share the code directly :)

package main

import (
	"context"
	"fmt"
	"github.com/goh-chunlin/go-onedrive/onedrive"
	"golang.org/x/oauth2"
	"io"
	"net/http"
	"os"
)

func main() {

	ctx := context.Background()
	ts := oauth2.StaticTokenSource(
		&oauth2.Token{AccessToken: ""},
	)
	tc := oauth2.NewClient(ctx, ts)

	client := onedrive.NewClient(tc)

	// Get Default Drive
	defaultDrive, err := client.Drives.Get(ctx, "")
	if err != nil {
		_ = fmt.Errorf("Error: %v\n", err)
		return
	}
	fmt.Printf("Default Drive ID: %v\n", defaultDrive.Id)
	fmt.Printf("Default Drive Owner: %v\n", defaultDrive.Owner.User.DisplayName)

	// Get All Drives
	drives, err := client.Drives.List(ctx)
	if err != nil {
		_ = fmt.Errorf("Error: %v\n", err)
		return
	}
	for _, drive := range drives.Drives {
		fmt.Printf("Results: %v\n", drive.Owner.User.DisplayName)
	}

	// Get Items In Drive
	driveItems, err := client.DriveItems.List(ctx, "")
	if err != nil {
		_ = fmt.Errorf("Error: %v\n", err)
		return
	}


	// List Drive Items
	for _, driveItem := range driveItems.DriveItems {
		fmt.Printf("Results: %v\n", driveItem.Name)
		fmt.Printf("Results: %v\n", driveItem.DownloadURL)

		//fmt.Printf(driveItem.File.)

		// Download Files To Local
		//if driveItem.DownloadURL != "" {
		//	err := DownloadFile(driveItem.Name, driveItem.DownloadURL)
		//	if err != nil {
		//		panic(err)
		//	}
		//	fmt.Println("Downloaded: " + driveItem.Name)
		//}

	}

	// list all OneDrive drives for the current logged in user
	//drives, err := onedrive.Drives.List(ctx)

}

// DownloadFile will download a url to a local file. It's efficient because it will
// write as it downloads and not load the whole file into memory.
func DownloadFile(filepath string, url string) error {

	// Get the data
	resp, err := http.Get(url)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	// Create the file
	out, err := os.Create(filepath)
	if err != nil {
		return err
	}
	defer out.Close()

	// Write the body to file
	_, err = io.Copy(out, resp.Body)
	return err
}
Share this article: Link copied to clipboard!