Example #1
0
func twitterVerify(w http.ResponseWriter, r *http.Request) {
	token := r.FormValue("oauth_token")
	id := r.FormValue("id")
	c := appengine.NewContext(r)

	if id == "" {
		serveError(c, w, os.NewError("Missing ID parameter"))
		return
	}

	item, _ := memcache.Get(c, token)

	secret := string(item.Value)
	verifier := r.FormValue("oauth_verifier")

	conf := &tweetlib.Config{
		ConsumerKey:    appConfig.TwitterConsumerKey,
		ConsumerSecret: appConfig.TwitterConsumerSecret}
	tok := &tweetlib.Token{}
	tr := &tweetlib.Transport{Config: conf,
		Token:     tok,
		Transport: &urlfetch.Transport{Context: c}}

	tt := &tweetlib.TempToken{Token: token, Secret: secret}
	tok, err := tr.AccessToken(tt, verifier)
	if err != nil {
		c := appengine.NewContext(r)
		serveError(c, w, err)
		c.Errorf("%v", err)
		return
	}
	tr.Token = tok
	tl, _ := tweetlib.New(tr.Client())
	u, err := tl.Account.VerifyCredentials().Do()
	fmt.Printf("err=%v\n", err)
	user := loadUser(r, id)
	user.TwitterOAuthToken = tok.OAuthToken
	user.TwitterOAuthSecret = tok.OAuthSecret
	user.TwitterId = u.IdStr
	user.TwitterScreenName = u.ScreenName
	if err := saveUser(r, &user); err != nil {
		serveError(c, w, err)
		return
	}

	http.Redirect(w, r, "/", http.StatusFound)

}
Example #2
0
func publishActivityToTwitter(w http.ResponseWriter, r *http.Request, act *plus.Activity, user *User) {
	c := appengine.NewContext(r)

	conf := &tweetlib.Config{
		ConsumerKey:    appConfig.TwitterConsumerKey,
		ConsumerSecret: appConfig.TwitterConsumerSecret}
	tok := &tweetlib.Token{OAuthToken: user.TwitterOAuthToken, OAuthSecret: user.TwitterOAuthSecret}
	tr := &tweetlib.Transport{Config: conf,
		Token:     tok,
		Transport: &urlfetch.Transport{Context: c}}

	tl, _ := tweetlib.New(tr.Client())

	var attachment *plus.ActivityObjectAttachments
	obj := act.Object
	kind := ""
	content := ""

	if act.Verb == "share" {
		content = act.Annotation
		if content == "" {
			content = "Resharing " + obj.Actor.DisplayName
		}
		kind = "status_share"
	} else {
		kind = "status"
		if obj != nil {
			if len(obj.Attachments) > 0 {
				attachment = obj.Attachments[0]
				kind = attachment.ObjectType
			}
			content = obj.Content
		} else {
			content = act.Title
		}

	}
	content = removeTags(content)

	c.Debugf("Post (%s):\n\tkind: %s\n\tcontent: %s\n", user.TwitterId, kind, content)
	var err os.Error
	switch kind {
	case "status":
		// post a status update
		_, err = tl.Tweets.Update(shorten(140, content, act.Url)).Do()
	case "status_share":
		_, err = tl.Tweets.Update(shortenLink(140, content, act.Url)).Do()
	case "article":
		// post a link
		_, err = tl.Tweets.Update(shortenLink(140, content, attachment.Url)).Do()
	default:
		if obj != nil {
			_, err = tl.Tweets.Update(shortenLink(140, content, obj.Url)).Do()
		}
	}

	if err == tweetlib.ErrOAuth {
		user.DisableTwitter()
		saveUser(r, user)
	}
	c.Debugf("publishActivityToTwitter(%s): err=%v\n", kind, err)
}
Example #3
0
File: unico.go Project: licio/unico
// Displays the home page.
func homeHandler(w http.ResponseWriter, r *http.Request) {
	if appConfig.AppHost == "" {
		appConfig.AppHost = r.Host
	}
	c := appengine.NewContext(r)
	if r.Method != "GET" || r.URL.Path != "/" {
		serve404(w)
		return
	}

	w.Header().Set("Content-Type", "text/html; charset=utf-8")

	params := make(map[string]string)

	// Look for a browser cookie containing the user id
	// We can use this to load the user information
	userCookie, err := r.Cookie("userId")
	var user User
	if err == nil {
		user = loadUser(r, userCookie.Value)
	}
	c.Debugf("loadUser: %v\n", user)
	if user.Id != "" {
		if session, err := sessions.Session(r, "", "datastore"); err == nil {
			session["userID"] = user.Id
			f := sessions.Save(r, w)
			c.Debugf("saveSession: %v\n", f)
		}

		if user.TwitterId != "" {

			item := new(memcache.Item)
			item, err := memcache.Get(c, "pic"+user.Id)

			if err != nil {
				// get the user profile pic
				conf := &tweetlib.Config{
					ConsumerKey:    appConfig.TwitterConsumerKey,
					ConsumerSecret: appConfig.TwitterConsumerSecret}
				tok := &tweetlib.Token{
					OAuthSecret: user.TwitterOAuthSecret,
					OAuthToken:  user.TwitterOAuthToken}
				tr := &tweetlib.Transport{Config: conf,
					Token:     tok,
					Transport: &urlfetch.Transport{Context: c}}

				tl, _ := tweetlib.New(tr.Client())
				u, err := tl.Users.Show().UserId(user.TwitterId).Do()
				if err == nil {
					params["pic"] = u.ProfileImageUrl
					memcache.Add(c, &memcache.Item{Key: "pic" + user.Id, Value: []byte(u.ProfileImageUrl)})
				}

			} else {
				params["pic"] = string(item.Value)
			}

		}
		params["twitterid"] = user.TwitterId
		params["twittername"] = user.TwitterScreenName
		params["googleid"] = user.Id
		params["fbid"] = user.FBId
		params["fbname"] = user.FBName

		mu := memUser(c, user.Id)
		if mu.Name == "" {
			tr := transport(user)
			tr.Transport = &urlfetch.Transport{Context: c}
			p, _ := plus.New(tr.Client())
			person, err := p.People.Get(user.Id).Do()
			c.Debugf("Home people get: %v,(%v)\n", person, err)
			if err == nil {
				mu.Image = person.Image.Url
				mu.Name = person.DisplayName
				memUserSave(c, user.Id, mu)
			}

		}
		params["googleimg"] = mu.Image
		params["googlename"] = mu.Name

	}

	if err := templates.Execute(w, "home", params); err != nil {
		serveError(c, w, err)
		c.Errorf("%v", err)
		return
	}

}