-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauth.go
59 lines (49 loc) · 1.12 KB
/
oauth.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
package main
import (
"log"
"code.google.com/p/goauth2/oauth"
"github.com/codegangsta/cli"
"github.com/gin-gonic/gin"
)
type OAuthClient struct {
clientId string
clientSecret string
redirectURL string
config *oauth.Config
}
func NewOAuthClient(c *cli.Context) *OAuthClient {
return &OAuthClient{
config: &oauth.Config{
ClientId: c.String("app-id"),
ClientSecret: c.String("app-secret"),
RedirectURL: c.String("redirect-url"),
AuthURL: "https://graph.facebook.com/oauth/authorize",
TokenURL: "https://graph.facebook.com/oauth/access_token",
},
}
}
func (o *OAuthClient) Authorize(c *gin.Context) {
form := struct {
Code string `form:"code"`
}{}
if !c.Bind(&form) {
c.JSON(500, map[string]string{"status": "unable to parse form data"})
return
}
t := &oauth.Transport{Config: o.config}
tok, err := t.Exchange(form.Code)
if err != nil {
c.Fail(500, err)
return
}
go func() {
err := Export(tok.AccessToken)
if err != nil {
log.Println(err)
}
}()
c.JSON(200, tok)
}
func (o *OAuthClient) AuthCodeURL(state string) string {
return o.config.AuthCodeURL(state)
}