tweetwrap

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 13, 2020 License: MIT Imports: 8 Imported by: 0

README

TweetWrap

go.dev reference Go Report Card

TweetWrap handles the 3-legged OAuth process and auth state in order to simplify the initialization of dghubble's go-twitter library.

How to use

Regiter your app

First you need a valid Twitter developer account. Create your project and get your API key and API secret key.

First run

Given the following code:

twitt, authURL, err := tweetwrap.New(tweetwrap.Config{
    APIKey:       "yours here",
    APIKeySecret: "yours here",
})
if err != nil {
    log.Fatalln(err)
}
if authURL != nil {
    fmt.Println("Authentication proceedure initiated, please logging at:", authURL.String())
    fmt.Println("Then restart with the config updated with the validation PIN.")
    os.Exit(0)
}
fmt.Println("Authenticated as:", twitt.GetAuthedUser())

As no current state has been found by the wrapper, it starts a 3-legged OAuth process with your app identity. Following the URL and validating the OAuth consent form will allow your app to act as the user you were logged as. The consent form will yield a PIN verification code which you will need on the second run.

Second run
twitt, authURL, err := tweetwrap.New(tweetwrap.Config{
    APIKey:       "yours here",
    APIKeySecret: "yours here",
    PIN:          "yours here", // <--- NEW
})
if err != nil {
    log.Fatalln(err)
}
if authURL != nil {
    fmt.Println("Authentication proceedure initiated, please logging at:", authURL.String())
    fmt.Println("Then restart with the config updated with the validation PIN.")
    os.Exit(0)
}
fmt.Println("Authenticated as:", twitt.GetAuthedUser())

// client is now initialized by the wrapper and can be used directly
tweet, _, err := twitt.Client.Statuses.Update("Yay wrapped tweet !", nil)
// ...

On the second run, the wrapper will load the previous state and detect the ongoing authentification. It will use the PIN code in order to finalize the auth between the app and the user. If authentification went fine, the linked user name will be printed. And the dghubble's go-twitter client accessed directly with the Client wrapper member.

Customizing the state file location

By default state will be saved within token_credentials.json file on the current working directory. Path can be customized by submitting a non empty StateFilePath value within Config.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	APIKey        string
	APIKeySecret  string
	PIN           string
	StateFilePath string // if empty the tokenCredsFile const will be used as path
}

Config contains all the values needed by New()

type Wrapper

type Wrapper struct {

	// wrapped client
	Client *twittgo.Client
	// contains filtered or unexported fields
}

Wrapper handles the 3-legged OAuth process automatically during its initialization. Must be instanciated with New()

func New

func New(conf Config) (w *Wrapper, authorizationURL *url.URL, err error)

New returns an initialized and ready to used Twitter Wrapper. If err is nil but authorizationURL is not, caller must : 1- transmit the URL to its user, let him authorize the account to be used and recover the PIN verifier code. 2- once the PIN has been recovered, call New again with the correct PIN in Config. Intermediary auth state will be loaded automatically from disk. Only a nil err and authorizationURL mean the client within the wrapper can be used.

func (*Wrapper) GetAuthedUser

func (w *Wrapper) GetAuthedUser() string

GetAuthedUser returns the account currently authenticated

func (*Wrapper) SaveStatus

func (w *Wrapper) SaveStatus() (err error)

SaveStatus dump the current auth & requests tokens to disk. Wihtout a restored state, New() will always trigger a new auth process: call it before exiting !

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL