Skip to content

Instantly share code, notes, and snippets.

Forked from miikka/Simple.hs
Created July 19, 2014 16:58
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
{-# LANGUAGE PackageImports #-}
You need to register your Twitter application at <>
to get the consumer key and secret needed for OAuth. When connecting to
Twitter for the first time, do this:
let consumer = Consumer "consumer key" "consumer secret"
token <- authenticate
This authenticates your application against Twitter. You should save the
token. It has a Binary instance, so you could just use encodeFile from
If you are the only user of your Twitter application, you can choose to use
the single access token. In that case use singleAccessToken to generate a
token - you can run it every time as it's actually a pure operation
(although its type doesn't look like that).
token <- singleAccessToken consumer "access token" "access secret"
Now you are ready to tweet:
tweet consumer token "Hello world!"
module Web.Twitter.Simple where
import Data.Maybe (fromJust)
import "mtl" Control.Monad.Trans
import Network.OAuth.Consumer
import Network.OAuth.Http.Request
import Network.OAuth.Http.Response
import Network.OAuth.Http.HttpClient
import Network.OAuth.Http.PercentEncoding
reqUrl = fromJust . parseURL $ ""
accUrl = fromJust . parseURL $ ""
tweetUrl = fromJust . parseURL $ ""
authUrl = ("" ++)
. findWithDefault ("oauth_token","") . oauthParams
data Consumer = Consumer
{ key :: String
, secret :: String }
deriving (Show, Eq)
authenticate :: Consumer -> IO Token
authenticate consumer = unCurlM . runOAuth $ do
ignite $ Application (key consumer) (secret consumer) OOB
oauthRequest HMACSHA1 Nothing reqUrl
cliAskAuthorization authUrl
oauthRequest HMACSHA1 Nothing accUrl
singleAccessToken :: Consumer -> String -> String -> IO Token
singleAccessToken consumer accToken accSecret = unCurlM . runOAuth $ do
let app = Application (key consumer) (secret consumer) OOB
let newToken = [("oauth_token", accToken)
,("oauth_token_secret", accSecret)]
ignite app
token <- getToken
return $ AccessToken app (fromList newToken `union` oauthParams token)
tweet :: Consumer -> Token -> String -> IO Response
tweet consumer token message = unCurlM . runOAuth $ do
ignite $ Application (key consumer) (secret consumer) OOB
putToken token
-- I can't figure out how to put the status in the POST body, but putting
-- it in the query string works.
let body = "status=" ++ encode message
request = tweetUrl { method = POST
, qString = fromList [("status", message)]
serviceRequest HMACSHA1 Nothing request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment