-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
main.go
145 lines (131 loc) · 3.48 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"context"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"time"
"github.com/yaegashi/msgraph.go/jsonx"
"github.com/yaegashi/msgraph.go/msauth"
msgraph "github.com/yaegashi/msgraph.go/v1.0"
"github.com/yaegashi/wtz.go"
"golang.org/x/oauth2"
)
const (
defaultTenantID = "common"
defaultClientID = "45c7f99c-0a94-42ff-a6d8-a8d657229e8c"
defaultTokenCachePath = "token_cache.json"
)
var defaultScopes = []string{"offline_access", "User.Read", "Calendars.Read", "Files.Read"}
func dump(o interface{}) {
enc := jsonx.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
enc.Encode(o)
}
func main() {
var tenantID, clientID, tokenCachePath string
flag.StringVar(&tenantID, "tenant-id", defaultTenantID, "Tenant ID")
flag.StringVar(&clientID, "client-id", defaultClientID, "Client ID")
flag.StringVar(&tokenCachePath, "token-cache-path", defaultTokenCachePath, "Token cache path")
flag.Parse()
ctx := context.Background()
m := msauth.NewManager()
m.LoadFile(tokenCachePath)
ts, err := m.DeviceAuthorizationGrant(ctx, tenantID, clientID, defaultScopes, nil)
if err != nil {
log.Fatal(err)
}
m.SaveFile(tokenCachePath)
httpClient := oauth2.NewClient(ctx, ts)
graphClient := msgraph.NewClient(httpClient)
{
log.Printf("Get current logged in user information")
req := graphClient.Me().Request()
log.Printf("GET %s", req.URL())
user, err := req.Get(ctx)
if err == nil {
dump(user)
} else {
log.Println(err)
}
}
{
log.Println("Get current logged in user's first 10 events")
req := graphClient.Me().Events().Request()
req.Top(10)
tzname, err := wtz.LocationToName(time.Local)
if err != nil {
log.Println(err)
tzname = "Tokyo Standard Time"
}
req.Header().Add("Prefer", fmt.Sprintf(`outlook.timezone="%s"`, tzname))
log.Printf("GET %s", req.URL())
events, err := req.GetN(ctx, 1)
if err == nil {
dump(events)
} else {
log.Println(err)
}
}
var items []msgraph.DriveItem
{
log.Printf("Get files in the root folder of user's drive")
req := graphClient.Me().Drive().Root().Children().Request()
// This filter is not supported by OneDrive for Business or SharePoint Online
// https://docs.microsoft.com/en-us/onedrive/developer/rest-api/concepts/filtering-results?#filterable-properties
// req.Filter("file ne null")
log.Printf("GET %s", req.URL())
items, err = req.Get(ctx)
if err != nil {
log.Println(err)
}
}
for _, item := range items {
timestamp := item.LastModifiedDateTime.Format(time.RFC3339)
itemType := "FILE"
if item.File == nil {
itemType = "DIR "
}
log.Printf(" %s %s %10d %s", itemType, timestamp, *item.Size, *item.Name)
}
fmt.Print("Press ENTER to download files or Ctrl-C to abort: ")
fmt.Scanln()
for _, item := range items {
if item.File == nil {
continue
}
err := func() error {
log.Printf("Download %q (%d bytes)", *item.Name, *item.Size)
if url, ok := item.GetAdditionalData("@microsoft.graph.downloadUrl"); ok {
res, err := http.Get(url.(string))
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
b, _ := ioutil.ReadAll(res.Body)
return fmt.Errorf("%s: %s", res.Status, string(b))
}
f, err := os.Create(*item.Name)
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(f, res.Body)
if err != nil {
return err
}
} else {
return fmt.Errorf("unknown download URL")
}
return nil
}()
if err != nil {
log.Println(err)
}
}
}