当前有一个Web应用程序,可通过Twitter的Oauth登录用户(并将其保存到DB)。
您也可以从Web应用程序进行鸣叫,但是该鸣叫当前已编码为从硬编码的AccessToken(开发人员仪表板中的Gotten)发送,而不是以前登录的用户发送。
我如何访问登录时生成的访问令牌,以便他们随后可以从该帐户发送推文?还是有另一种方法。
我发现的每一段代码都涉及从开发人员帐户发布推文,而不是通过Oauth登录然后发布推文。
我目前正在使用Golang buffalo框架,并使用Goth(用于auth)和dghubble的go-twitter(用于tweet)库。
这是验证的代码:
func init() {
gothic.Store = App().SessionStore
goth.UseProviders(
twitter.New(os.Getenv("API_KEY"), os.Getenv("API_SECRET"), fmt.Sprintf("%s%s", App().Host, "/auth/twitter/callback")),
)
}
func AuthCallback(c buffalo.Context) error {
gu, err := gothic.CompleteUserAuth(c.Response(), c.Request())
if err != nil {
return c.Error(401, err)
}
tx := c.Value("tx").(*pop.Connection)
q := tx.Where("provider = ? and provider_id = ?", gu.Provider, gu.UserID)
exists, err := q.Exists("users")
if err != nil {
return errors.WithStack(err)
}
u := &models.User{}
if exists {
if err = q.First(u); err != nil {
return errors.WithStack(err)
}
}
u.Name = defaults.String(gu.Name, gu.NickName)
u.Provider = gu.Provider
u.ProviderID = gu.UserID
u.Email = nulls.NewString(gu.Email)
if err = tx.Save(u); err != nil {
return errors.WithStack(err)
}
c.Session().Set("current_user_id", u.ID)
if err = c.Session().Save(); err != nil {
return errors.WithStack(err)
}
c.Flash().Add("success", "You have been logged in")
return c.Redirect(302, "/")
}
func AuthDestroy(c buffalo.Context) error {
c.Session().Clear()
c.Flash().Add("success", "You have been logged out")
return c.Redirect(302, "/")
}
func SetCurrentUser(next buffalo.Handler) buffalo.Handler {
return func(c buffalo.Context) error {
if uid := c.Session().Get("current_user_id"); uid != nil {
u := &models.User{}
tx := c.Value("tx").(*pop.Connection)
if err := tx.Find(u, uid); err != nil {
return errors.WithStack(err)
}
c.Set("current_user", u)
}
return next(c)
}
}
func Authorize(next buffalo.Handler) buffalo.Handler {
return func(c buffalo.Context) error {
if uid := c.Session().Get("current_user_id"); uid == nil {
c.Flash().Add("danger", "You must be authorized to see that page")
return c.Redirect(302, "/")
}
return next(c)
}
}
这是处理推文的代码:
GetClient设置客户端准备发送推文。
type Credentials struct {
APIKey string
APISecret string
AccessToken string
AccessTokenSecret string
}
// getClient is a helper function that will return a twitter client
// that we can subsequently use to send tweets, or to stream new tweets
// this will take in a pointer to a Credential struct which will contain
// everything needed to authenticate and return a pointer to a twitter Client
// or an error
func GetClient(creds *Credentials) (*twitter.Client, error) {
// Pass in your consumer key (API Key) and your Consumer Secret (API Secret)
config := oauth1.NewConfig(creds.APIKey, creds.APISecret)
// Pass in your Access Token and your Access Token Secret
token := oauth1.NewToken(creds.AccessToken, creds.AccessTokenSecret)
httpClient := config.Client(oauth1.NoContext, token)
client := twitter.NewClient(httpClient)
// Verify Credentials
verifyParams := &twitter.AccountVerifyParams{
SkipStatus: twitter.Bool(true),
IncludeEmail: twitter.Bool(true),
}
// we can retrieve the user and verify if the credentials
// we have used successfully allow us to log in!
user, _, err := client.Accounts.VerifyCredentials(verifyParams)
if err != nil {
return nil, err
}
log.Printf("User's ACCOUNT:\n%+v\n", user)
return client, nil
}
这将发送推文:
func SendHandler(c buffalo.Context) error {
//Calls http.Request package to get the form values. Assigns it to Req
Req := c.Request()
//ParseForm decodes the form
Req.ParseForm()
//Gets the value from the form, assigns it to body.
body := Req.FormValue("tweetbody")
fmt.Printf("%+v\n", creds)
//Gets the client from the twitter package
client, err := twitter.GetClient(&creds)
if err != nil {
log.Println("Error getting Twitter Client")
log.Println(err)
}
//Sending the actual tweet. Body from the form
tweet, resp, err := client.Statuses.Update(body, nil)
if err != nil {
log.Println(err)
}
log.Printf("%+v\n", resp)
log.Printf("%+v\n", tweet)
return c.Redirect(302, "/tweet/confirm")
}
我只是假设我必须从oauth获取访问令牌...如果有更好的方法,请声明它。