-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathgithub_auth_token.go
111 lines (104 loc) · 2.43 KB
/
github_auth_token.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
// Copyright (c) 2012-2013 Jason McVetta. This is Free Software, released
// under the terms of the GPL v3. See http://www.gnu.org/copyleft/gpl.html for
// details. Resist intellectual serfdom - the ownership of ideas is akin to
// slavery.
// Example demonstrating use of package napping, with HTTP Basic
// authentictation over HTTPS, to retrieve a Github auth token.
package main
/*
NOTE: This example may only work on *nix systems due to gopass requirements.
*/
import (
"fmt"
"github.com/howeyc/gopass"
"github.com/kr/pretty"
"gopkg.in/jmcvetta/napping.v3"
"log"
"net/url"
"time"
)
func init() {
log.SetFlags(log.Ltime | log.Lshortfile)
}
func main() {
//
// Prompt user for Github username/password
//
var username string
fmt.Printf("Github username: ")
_, err := fmt.Scanf("%s", &username)
if err != nil {
log.Fatal(err)
}
fmt.Printf("github.com/howeyc/gopass")
passwd, err := gopass.GetPasswd()
if err != nil {
log.Fatal(err)
}
//
// Compose request
//
// http://developer.github.com/v3/oauth/#create-a-new-authorization
//
payload := struct {
Scopes []string `json:"scopes"`
Note string `json:"note"`
}{
Scopes: []string{"public_repo"},
Note: "testing Go napping" + time.Now().String(),
}
//
// Struct to hold response data
//
res := struct {
Id int
Url string
Scopes []string
Token string
App map[string]string
Note string
NoteUrl string `json:"note_url"`
UpdatedAt string `json:"updated_at"`
CreatedAt string `json:"created_at"`
}{}
//
// Struct to hold error response
//
e := struct {
Message string
Errors []struct {
Resource string
Field string
Code string
}
}{}
//
// Setup HTTP Basic auth for this session (ONLY use this with SSL). Auth
// can also be configured on a per-request basis when using Send().
//
s := napping.Session{
Userinfo: url.UserPassword(username, string(passwd)),
}
url := "https://api.github.com/authorizations"
//
// Send request to server
//
resp, err := s.Post(url, &payload, &res, &e)
if err != nil {
log.Fatal(err)
}
//
// Process response
//
println("")
if resp.Status() == 201 {
fmt.Printf("Github auth token: %s\n\n", res.Token)
} else {
fmt.Println("Bad response status from Github server")
fmt.Printf("\t Status: %v\n", resp.Status())
fmt.Printf("\t Message: %v\n", e.Message)
fmt.Printf("\t Errors: %v\n", e.Message)
pretty.Println(e.Errors)
}
println("")
}