-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgithub_auth_token.go
105 lines (98 loc) · 2.17 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
// 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 restclient, 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 (
"code.google.com/p/gopass"
"fmt"
"github.com/jmcvetta/restclient"
"log"
"net/url"
)
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)
}
passwd, err := gopass.GetPass("Github password: ")
if err != nil {
log.Fatal(err)
}
//
// Compose request
//
// http://developer.github.com/v3/oauth/#create-a-new-authorization
//
d := struct {
Scopes []string `json:"scopes"`
Note string `json:"note"`
}{
Scopes: []string{"public_repo"},
Note: "testing Go restclient",
}
//
// 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
}{}
//
// Setup HTTP Basic auth (ONLY use this with SSL)
//
u := url.UserPassword(username, passwd)
rr := restclient.RequestResponse{
Url: "https://api.github.com/authorizations",
Userinfo: u,
Method: "POST",
Data: &d,
Result: &res,
Error: &e,
}
//
// Send request to server
//
status, err := restclient.Do(&rr)
if err != nil {
log.Fatal(err)
}
//
// Process response
//
println("")
if 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", status)
fmt.Printf("\t Message: %v\n", e.Message)
}
println("")
}