Пример #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, errors.New("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)
	u, err := tl.Account.VerifyCredentials(nil)
	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)

}
Пример #2
0
// 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
	var user User
	user, err := loadUserCookie(r)
	if err == nil {
		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)
				opts := tweetlib.NewOptionals()
				opts.Add("user_id", user.TwitterId)
				u, err := tl.User.Show(user.TwitterScreenName, opts)
				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["adnid"] = user.ADNId
		params["adnname"] = user.ADNScreenName
		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()
			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.ExecuteTemplate(w, "home", params); err != nil {
		serveError(c, w, err)
		return
	}

}
Пример #3
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)

	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 error
	switch kind {
	case "status":
		// post a status update
		_, err = tl.Tweets.Update(shorten(c, "status", content, act.Url, tl), nil)
	case "status_share":
		_, err = tl.Tweets.Update(shorten(c, "link", content, act.Url, tl), nil)
	case "article":
		// post a link
		c.Debugf("Article (%s):\n\tcontent: %s\n\turl: %s\n", user.TwitterId, content, attachment.Url)

		if content == attachment.Url || content == "" {
			if attachment.DisplayName != "" {
				content = attachment.DisplayName
			} else {
				content = "Shared a link."
			}
		}
		_, err = tl.Tweets.Update(shorten(c, "link", content, attachment.Url, tl), nil)
	case "photo":
		// download photo
		mediaUrl := attachment.FullImage.Url
		fileName := path.Base(mediaUrl)

		var media []byte
		item, err := memcache.Get(c, "picture"+mediaUrl)
		if err != nil {
			client := urlfetch.Client(c)
			resp, err := client.Get(attachment.FullImage.Url)
			c.Debugf("Downloading %s (%v)\n", mediaUrl, err)
			if err != nil {
				break
			}
			media, err = ioutil.ReadAll(resp.Body)
			c.Debugf("Reading contents of %s (%v)\n", mediaUrl, err)
			if err != nil {
				break
			}
			memcache.Add(c, &memcache.Item{Key: "picture" + mediaUrl, Value: media})
		} else {
			media = item.Value
		}
		// now we post it
		tweetMedia := &tweetlib.TweetMedia{
			Filename: fileName,
			Data:     media}
		_, err = tl.Tweets.UpdateWithMedia(shorten(c, "media", content, act.Url, tl), tweetMedia, nil)
		c.Debugf("Tweeting %s (%v)\n", mediaUrl, err)
	default:
		if obj != nil {
			_, err = tl.Tweets.Update(shorten(c, "link", content, obj.Url, tl), nil)
		}
	}

	c.Debugf("publishActivityToTwitter(%s): err=%v\n", kind, err)
}