Skip to content

Instantly share code, notes, and snippets.

@aki-lua87
Created July 7, 2017 09:15
Show Gist options
  • Save aki-lua87/cd862f1d4a2e1e633424964b01675929 to your computer and use it in GitHub Desktop.
Save aki-lua87/cd862f1d4a2e1e633424964b01675929 to your computer and use it in GitHub Desktop.
GAE/Goでanacondaを利用した認証 ref: http://qiita.com/aki_lua87/items/718e999fc37b252d1c53
go get "github.com/ChimeraCoder/anaconda"
go get "github.com/garyburd/go-oauth/oauth"
go get "google.golang.org/appengine"
var credential *oauth.Credentials
func GetRequestTokenHandler(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)
http.DefaultClient.Transport = &urlfetch.Transport{Context: ctx}
anaconda.SetConsumerKey("consumerKey")
anaconda.SetConsumerSecret("consumerSecret")
url, tmpCred, err := anaconda.AuthorizationURL("GetAccessTokenHandlerが呼ばれるURL")
if err != nil {
return
}
credential = tmpCred
http.Redirect(w, r, url, http.StatusFound)
}
func GetAccessTokenHandler(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)
http.DefaultClient.Transport = &urlfetch.Transport{Context: ctx}
c, _, err := anaconda.GetCredentials(credential, r.URL.Query().Get("oauth_verifier"))
if err != nil {
return
}
api := anaconda.NewTwitterApi(c.Token, c.Secret)
// TEST POST
api.PostTweet("test tweet", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment