Skip to content

Instantly share code, notes, and snippets.

@pasberth
Created March 3, 2013 03:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pasberth/5074331 to your computer and use it in GitHub Desktop.
Save pasberth/5074331 to your computer and use it in GitHub Desktop.
Conduit で Twitter に認証するサンプル
import Web.Authenticate.OAuth as OAuth
import Data.Conduit
import Network.HTTP.Conduit
import qualified Data.ByteString as BS
import System.IO(hFlush, stdout)
oauth :: OAuth.OAuth
oauth = OAuth.newOAuth
{ OAuth.oauthServerName = "twitter"
, OAuth.oauthRequestUri = "https://twitter.com/oauth/request_token"
, OAuth.oauthAccessTokenUri = "https://api.twitter.com/oauth/access_token"
, OAuth.oauthAuthorizeUri = "https://api.twitter.com/oauth/authorize"
, OAuth.oauthSignatureMethod = OAuth.HMACSHA1
, OAuth.oauthConsumerKey = error "Consumer key"
, OAuth.oauthConsumerSecret = error "Consumer secret"
, OAuth.oauthVersion = OAuth.OAuth10a
}
printAuthorizeUrl :: OAuth -> Credential -> IO ()
printAuthorizeUrl oauth tmp = do
putStr "URL: "
putStrLn $ authorizeUrl oauth tmp
getPIN :: IO BS.ByteString
getPIN = do
putStr "PIN: "
hFlush stdout
BS.getLine
authorize :: IO Credential
authorize = do
tmp <- withManager $ getTemporaryCredential oauth
printAuthorizeUrl oauth tmp
pin <- getPIN
let tmp' = injectVerifier pin tmp
cred <- withManager $ getTokenCredential oauth tmp'
return cred
main = do
(Credential map) <- authorize
let Just accessToken = lookup "oauth_token" map
Just accessTokenSecret = lookup "oauth_token_secret" map
Just userId = lookup "user_id" map
Just screenName = lookup "screen_name" map
putStr "Access Token : " >> print accessToken
putStr "Access Token Secret : " >> print accessTokenSecret
putStr "User ID : " >> print userId
putStr "Screen Name : " >> print screenName
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment