3

I am stuck with haskell types.

{-# LANGUAGE OverloadedStrings #-}

module Main (
    main
) where

import qualified Facebook as FB
import Network.HTTP.Conduit (withManager)
import Control.Monad.IO.Class (liftIO)
import System.IO

app :: FB.Credentials
app = FB.Credentials "localhost" "249348058430770" "..."

url :: FB.RedirectUrl
url = "http://localhost/fb"

perms :: [FB.Permission]
perms = ["user_about_me", "email"]

main :: IO ()
main = do
    fbAuthUrl <- FB.getUserAccessTokenStep1 url perms
    liftIO $ print fbAuthUrl
    argument <- readLn
    token <- FB.getUserAccessTokenStep2 url [argument]
    withManager $ \manager -> do
        FB.runFacebookT app manager $ do
            u <- FB.getUser "me" [] token
            liftIO $ print (FB.userEmail u)

error

src/Main.hs:23:18:
    Couldn't match expected type `IO t0'
                with actual type `FB.FacebookT
                                    FB.Auth m0 text-0.11.2.0:Data.Text.Internal.Text'
    In the return type of a call of `FB.getUserAccessTokenStep1'
    In a stmt of a 'do' block:
      fbAuthUrl <- FB.getUserAccessTokenStep1 url perms
    In the expression:
      do { fbAuthUrl <- FB.getUserAccessTokenStep1 url perms;
           liftIO $ print fbAuthUrl;
           argument <- readLn;
           token <- FB.getUserAccessTokenStep2 url [argument];
           .... }

package http://hackage.haskell.org/package/fb

3
  • 4
    The type main :: liftIO doesn't make any sense. Do you mean main :: IO ()? Jul 21, 2012 at 22:25
  • I don't know ? I don't return any IO () like putStrLn ? Jul 21, 2012 at 23:25
  • Help us to help you: 1. When asking for help with an error, always include the full error message in your question. 2. When asking about a package that didn't come bundled with your compiler, always tell us the package name.
    – dave4420
    Jul 22, 2012 at 1:16

1 Answer 1

3

First, let me preface this answer by the disclaimer that I've never actually used the Facebook API or the Conduits library, so I'm not sure if this code actually does anything sensible, but by going with just the type information, I think this is what you were trying to do

main :: IO ()
main = withManager $ \manager -> FB.runFacebookT app manager $ do
    fbAuthUrl <- FB.getUserAccessTokenStep1 url perms
    liftIO $ print fbAuthUrl
    argument <- liftIO $ readLn
    token <- FB.getUserAccessTokenStep2 url [argument]
    u <- FB.getUser "me" [] (Just token)
    liftIO $ print (FB.userEmail u)

The main pitfall is that main in Haskell must always have the type IO a, but you are trying to use a value of type FacebookT Auth m () as your main. Your implementation is on the right track, but the runFacebookT and withManager need to be the first thing in the function.

Type-wise, the actual do-block has the type FacebookT Auth (ResourceT IO) (). The runFacebookT function is used to unwrap the FacebookT transformer, resulting in a ResourceT IO () value, which is in turn processed by withManager to produce a plain old IO ().

One additional problem was that you originally had a readLn in your do-block without liftIO, which was confusing the type-inference. I also added the missing Just to the FB.getUser call.

4
  • 2
    main in Haskell can actually be IO a. Jul 22, 2012 at 10:52
  • print works perfect but i get user error (Prelude.readIO: no parse) when doing readLn? Jul 22, 2012 at 15:50
  • 1
    What are you trying to input? readLn is expecting to get something like ("foo", "bar")
    – shang
    Jul 22, 2012 at 16:43
  • the response code from facebook after accepting the app permission? http://localhost/fb?code=AQBsA9rMt6...aXvBbXIuR8#_=_ Jul 22, 2012 at 17:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.