-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
90 lines (72 loc) · 1.96 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
// The authorize command acquires an initial access token for Google API usage.
// See the README.md file for more information.
package main
import (
"context"
"fmt"
"log"
"os"
"strings"
"google.golang.org/api/drive/v2"
"google.golang.org/api/option"
"github.com/madkins23/go-google/oauth2"
"golang.org/x/xerrors"
)
const (
usage = `
usage: authorize <applicationName> <accessScope>*
<applicatonName> name of application to be authorized
<accessScope> access scopes for application (defaults to "drive")
`
)
//////////////////////////////////////////////////////////////////////////////
// Main routine.
func main() {
fmt.Println("authorize starting")
// Parse arguments.
if len(os.Args) < 2 {
log.Panic(usage)
}
applicationName := os.Args[1]
var accessScopes []string
if len(os.Args) < 3 {
accessScopes = []string{"drive"}
} else {
accessScopes = os.Args[2:]
}
fmt.Printf("Application: %s\n", applicationName)
fmt.Printf("Access Scopes: %s\n", strings.Join(accessScopes, ", "))
// Create Google authorizer.
authorizer, err := oauth2.NewAuthorizer(applicationName, accessScopes)
if err != nil {
log.Fatalf("Unable to create authorizer: %v", err)
}
client, err := authorizer.GetClient()
if err == nil && client == nil {
err = xerrors.New("no client returned")
}
if err != nil {
log.Fatalf("Unable to get client: %v", err)
}
for _, scope := range accessScopes {
if scope == "drive" {
service, err := drive.NewService(context.Background(), option.WithHTTPClient(client))
if err != nil {
log.Fatalf("Unable to get drive service object from client: %v", err)
}
rows, err := service.Files.List(). /*Fields("id, description").*/ Do()
if err != nil {
log.Fatalf("Unable to list files: %v", err)
}
fmt.Println("First few drive items:")
for index, item := range rows.Items {
if index > 4 {
break
}
fmt.Printf(" %s\n", item.Title)
}
break
}
}
fmt.Println("authorize finished")
}