Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

36 lines (33 sloc) 1.4 kb
module Drive
( getDriveFileContents
) where
import Prelude
import Data.Text.Encoding (encodeUtf8)
import qualified Data.Text as T
import qualified Google.Drive.V2 as Drive
import qualified Google.Drive.V2.Types as DriveT
import Network.HTTP.Conduit (Manager, httpLbs, responseBody, Request (..), parseUrl)
import Network.HTTP.Types (hAuthorization)
import Data.ByteString.Lazy (toChunks)
import Data.Text.Encoding (decodeUtf8)
import Data.Default (def)
import Data.Conduit (runResourceT)
import Data.Text (Text, unpack)
import Data.Monoid ((<>))
import Control.Applicative ((<$>))
import GoogleOAuth2 (AccessToken (..))
getDriveFileContents :: AccessToken
-> Text -- ^ File ID
-> Manager -- ^ HTTP Manager
-> IO Text
getDriveFileContents (AccessToken at) fileId manager = do
let params = def { Drive.parameterOauth_token = Just at }
file <- responseBody <$> Drive.getFile params fileId
downloadUrl <- maybe (fail "couldn't retrieve file download URL") return $ DriveT.fileDownloadUrl file
runResourceT $ do
req <- parseUrl $ unpack downloadUrl
let authHeader = (hAuthorization, "Bearer " <> encodeUtf8 at)
req' = req { requestHeaders = authHeader : requestHeaders req }
lazyByteStringToText . responseBody <$> httpLbs req' manager
where
lazyByteStringToText = T.concat . map decodeUtf8 . toChunks
Jump to Line
Something went wrong with that request. Please try again.