@hackage google-oauth20.2.2

Google OAuth2 token negotiation

Interacting with the Google OAuth2 authorization API

  1. Prompt the user for a verification code

  2. POST that code to the Google API for a set of tokens (access and refresh)

  3. Use the access token until it expires

  4. Use the refresh token to get a new access token

  5. Repeat from 3

Example usage:

import Data.Monoid
import Network.Google.OAuth2
import Network.HTTP.Conduit
import Network.HTTP.Types (hAuthorization)

import qualified Data.ByteString.Char8 as B8
import qualified Data.ByteString.Lazy.Char8 as L8

main :: IO ()
main = do
    let client = OAuth2Client clientId clientSecret
        scopes = ["https://www.googleapis.com/auth/drive"]

    token <- getAccessToken client scopes Nothing

    request <- parseUrl "https://www.googleapis.com/drive/v2/files"
    response <- withManager $ httpLbs $ authorize token request

    L8.putStrLn $ responseBody response

 where
   authorize token request = request
       -- Note: haddock chokes on curly braces for some reason, so I'm using
       -- parens here instead.
       ( requestHeaders = [(hAuthorization, B8.pack $ "Bearer " <> token)] )

   -- Setup in Google Developers Console
   clientId = "..."
   clientSecret = "..."