Skip to content

Instantly share code, notes, and snippets.

@jakejscott
Created February 12, 2014 07:11
Show Gist options
  • Save jakejscott/8951215 to your computer and use it in GitHub Desktop.
Save jakejscott/8951215 to your computer and use it in GitHub Desktop.
oauth2 martini google
package main
import (
"github.com/codegangsta/martini"
"github.com/martini-contrib/oauth2"
"github.com/martini-contrib/sessions"
)
func main() {
m := martini.Classic()
m.Use(sessions.Sessions("my_session", sessions.NewCookieStore([]byte("secret123"))))
m.Use(oauth2.Google(&oauth2.Options{
ClientId: "1018452126237-b7skrr384p5bgbspr1ol5lmu696o58g3.apps.googleusercontent.com",
ClientSecret: "pJ1Q3K0QuG7BbEGtmvQBpSxZ",
RedirectURL: "http://localhost:3000/oauth2callback",
Scopes: []string{"https://www.googleapis.com/auth/drive"},
}))
oauth2.PathLogin = "/login"
oauth2.PathLogout = "/logout"
m.Get("/", func() string {
return `
<p>Home</p>
<ul>
<li><a href="/login?next=/admin">admin</a></li>
<li><a href="/logout">logout</a></li>
</ul>
`
})
m.Get("/admin", oauth2.LoginRequired, func() string {
return `
<p>Admin</p>
<ul>
<li><a href="/logout">logout</a></li>
</ul>
`
})
// tokens are injected to the handlers
m.Get("/access_token", func(tokens oauth2.Tokens) (int, string) {
if tokens != nil {
return 200, tokens.AccessToken()
}
return 403, "not authenticated"
})
m.Run()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment