forked from minchao/go-apple-music
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
72 lines (61 loc) · 1.54 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
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"github.com/minchao/go-apple-music/token"
)
func usage() {
fmt.Fprintln(os.Stderr, `Usage: token-generator [options]`)
flag.PrintDefaults()
}
func main() {
var (
keyId string
teamId string
ttl int64
pk string
pkFile string
secret []byte
)
flag.StringVar(&keyId, "k", "", "MusicKit key")
flag.StringVar(&teamId, "t", "", "Team ID")
flag.Int64Var(&ttl, "l", 3600, "TTL (time-to-live), must not be greater than 15777000 (6 months in seconds)")
flag.StringVar(&pk, "pk", "", "MusicKit private key, enter string without BEGIN and END annotations")
flag.StringVar(&pkFile, "pf", "", "MusicKit private key, the path of private key file (.p8)")
flag.Usage = usage
flag.Parse()
if len(os.Args) < 4 {
flag.Usage()
os.Exit(1)
}
if pk == "" && pkFile == "" {
fmt.Fprintln(os.Stderr, "The -pk or -pf is required")
os.Exit(1)
}
if pk != "" {
pk = fmt.Sprintf("-----BEGIN PRIVATE KEY-----\n%s\n-----END PRIVATE KEY-----", pk)
secret = []byte(pk)
} else {
var err error
secret, err = ioutil.ReadFile(pkFile)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
gen := token.Generator{
KeyId: keyId,
TeamId: teamId,
TTL: ttl,
Secret: secret,
}
t, err := gen.Generate()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
fmt.Fprintf(os.Stdout, "-----APPLE MUSIC API TOKEN-----\n%s\n", t)
fmt.Fprintf(os.Stdout, "-----CURL EXAMPLE-----\ncurl -v -H 'Authorization: Bearer %s' https://api.music.apple.com/v1/storefronts/tw\n", t)
}