Created
August 14, 2013 06:40
-
-
Save shazow/6228559 to your computer and use it in GitHub Desktop.
OAuth2 for GA in Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func AccountConnectHandler(w http.ResponseWriter, r *http.Request) { | |
// TODO: Check state? r.FormValue("state") | |
transport := &oauth.Transport{ | |
Config: &config, | |
Transport: &urlfetch.Transport{ | |
Context: appengine.NewContext(r), | |
}, | |
} | |
token, err := transport.Exchange(r.FormValue("code")) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
log.Println("Exchanged token:", token) | |
client := transport.Client() | |
oauthApi, err := oauth2.New(client) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
tokenInfo, err := oauthApi.Userinfo.Get().Do() | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
log.Println("Email detected:", tokenInfo.Email) | |
analyticsApi, err := analytics.New(client) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
result, err := analyticsApi.Management.Accounts.List().Do() | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
return | |
} | |
fmt.Fprint(w, result.Kind) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment