-
Notifications
You must be signed in to change notification settings - Fork 1
Home
montsamu edited this page Sep 13, 2010
·
15 revisions
Welcome to the go-twitter-oauth wiki! Everything is terrible. Hopefully part of the hmac/hash/base64 code is useful for learning, but you can authenticate vs. Twitter OAuth, get replies JSON, etc. “Persistence” is a simple, terrible file-based storage. Just ensure the directory exists and the user can write to it.
Ex:
import ( "oauth"; "session"; "persist"; "json"; // stdlib )
var oauth_persist_service = persist.NewPersistService("tokens"); var twitter_client = oauth.NewTwitterClient(oauth_persist_service, "myconsumerkey", "myconsumersecret", "authenticate");
var session_persist_service = persist.NewPersistService("sessions"); var session_service = session.NewSessionService(session_persist_service, "Example-Id");
func MyTwitterLoginHandler(c *http.Conn, req *http.Request) { http.Redirect(c, twitter_client.Get_authorization_url("http://my.callback.url/callback/twitter"), http.StatusFound); }
func MyTwitterCallbackHandler(c *http.conn, req *http.Request) { var auth_token = req.FormValue("oauth_token"); var auth_verifier = req.FormValue("oauth_verifier"); user_info := twitter_client.Get_user_info(auth_token, auth_verifier); session_service.StartSession(c,req,user_info); http.Redirect(c, "/", http.StatusFound); }
func MyRepliesHandler(c *http.Conn, req *http.Request) { s := session_service.GetSession(c,req); auth_token, atx := s.Data["oauth_token"]; if atx { // they're logged in and authenticated via Twitter this session auth_token_secret := s.Data["oauth_token_secret"]; r, _, _ := twitter_client.MakeRequest("http://twitter.com/statuses/mentions.json", map[string]string{"oauth_token":auth_token}, auth_token_secret, false); // also in additional params map: "since":"someId" if you like b, _ := io.ReadAll(r.Body); str := bytes.NewBuffer(b).String(); j, _, _ := json.StringToJson(str); // poke around the j (Json) data, I dunno, send it through a template or something instead of raw ugly: c.Write(strings.Bytes(j.String())); } else { // not authenticated via Twitter this session http.Redirect(c, "/login/twitter", http.StatusFound); } }
What you do with user_info (store the access token, for example, in a session, as above) is up to you, but user_info has oauth_token, oauth_token_secret, user_id, and screen_name coming back from Twitter. Above is a simple request for Twitter replies; you can store the last retrieved reply in the session if you like and only retrieve newer, color new differently, etc.
SUBJECT TO RAMPANT CHANGE WITH WILD DISREGARD.