Могу ли я публиковать сообщения в Твиттере от зарегистрированного пользователя Oauth? - PullRequest
0 голосов
/ 15 апреля 2019

В настоящее время есть веб-приложение, которое регистрирует пользователя (и сохраняет его в БД) через Oauthing with twitter.

Вы также можете твитнуть из веб-приложения, однако твит в настоящее время закодирован для отправки из жестко закодированного AccessToken (полученного из панели инструментов разработчиков), а не от ранее зарегистрированного пользователя.

Как получить доступ к токену доступа, сгенерированному при входе в систему, чтобы они могли затем отправить твит с этой учетной записи? Или есть другой метод.

Каждый фрагмент кода, который я нахожу, включает в себя твиты из аккаунта разработчиков, а не вход в систему через Oauth, а затем твиты.

В настоящее время я использую Golang Buffalo Framework с библиотеками Goth (для аутентификации) и go-twitter dghubble (для твитов).

Вот код для аутентификации:

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 ... Если есть лучший способ, пожалуйста, укажите его.

...