Skip to content

Instantly share code, notes, and snippets.

@alucky0707
Created November 18, 2012 04:42
Show Gist options
  • Save alucky0707/4103595 to your computer and use it in GitHub Desktop.
Save alucky0707/4103595 to your computer and use it in GitHub Desktop.
ConduitでTwitterにpostします
module Main where
{-# LANGUAGE OverloadedStrings #-}
import System.IO
import Control.Monad.IO.Class (liftIO)
import Codec.Binary.UTF8.String (encode)
import Data.ByteString
import Data.Conduit
import qualified Data.Conduit.Binary as CB
import Network.HTTP.Conduit
import Network.TLS.Extra
import Network (withSocketsDo)
import Web.Authenticate.OAuth
-- もちろん自分のを入れてください
consumerKey = "....."
consumerSecret = "....."
accessToken = "....."
accessTokenSecret = "....."
oauth = newOAuth {
oauthRequestUri = "https://api.twitter.com/oauth/request_token",
oauthAccessTokenUri = "https://api.twitter.com/oauth/access_token",
oauthAuthorizeUri = "https://api.twitter.com/oauth/authorize",
oauthConsumerKey = consumerKey,
oauthConsumerSecret = consumerSecret}
credential = newCredential accessToken accessTokenSecret
-- これないとTwitterに不正なシーケンスだっておこられます
encodeUTF8 :: String -> ByteString
encodeUTF8 = pack . encode
main = withSocketsDo $ runResourceT $ do
manager <- liftIO $ newManager def
request <- liftIO $ parseUrl "https://api.twitter.com/1.1/statuses/update.json"
let postRequest = urlEncodedBody [(encodeUTF8 "status",encodeUTF8 "Haskellより")] request
signedRequest <- signOAuth oauth credential postRequest
response <- http signedRequest manager
responseBody response $$+- CB.sinkHandle stdout
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment