-1

参考haskell facebook 示例效果很好,但现在我不知道如何将它拆分到一个单独的模块中,以便我可以用它做一些有用的事情。另外我不知道我需要为 fbEmail fbUrl 放置什么类型,以及是否需要在每个模块中放置 OverloadedStrings?也许我的思考过程是错误的,并且不可能将导入 Network.HTTP.Conduit (withManager) 与 main 分开?

登录.hs

{-# LANGUAGE OverloadedStrings, NoMonomorphismRestriction #-}
module Login (
    fbUrl,
    fbEmail
) where

import qualified Facebook as FB
import Network.HTTP.Conduit (withManager)
import Data.Text
import Data.ByteString.Internal (ByteString)

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

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

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

fbUrl = FB.getUserAccessTokenStep1 url perms

fbEmail c = withManager $ \manager -> FB.runFacebookT app manager $ do
    t <- FB.getUserAccessTokenStep2 url [c]
    u <- FB.getUser "me" [] (Just t)
    return $ FB.userEmail u

主文件

module Main (
  main
) where

import Login
import System.IO

main :: IO ()
main = do
    u <- fbUrl
    print u
    a <- readLn
    e <- fbEmail a
    print e

我收到以下错误

src/Main.hs:11:10:
Couldn't match expected type `IO t0'
            with actual type `fb-0.9.6:Facebook.Monad.FacebookT
                                fb-0.9.6:Facebook.Monad.Auth m0 Data.Text.Internal.Text'
In a stmt of a 'do' block: u <- fbUrl
In the expression:
  do { u <- fbUrl;
       print u;
       a <- readLn;
       e <- fbEmail a;
       .... }
In an equation for `main':
    main
      = do { u <- fbUrl;
             print u;
             a <- readLn;
             .... }

代码更新 6:见答案

4

1 回答 1

2

总结评论中发生的事情,这个问题基本上是三重的。您尝试返回 aMaybe Text而不是 a FacebookT ... (Maybe Text),由于缺少 a return,缺少类型签名导致单态限制踢 inm ,最后您忘记了runFacebookTin main

编辑:第四个问题是你给fbEmail了一个太多态的类型,类型检查器抱怨类型变量a实际上必须是完全 (ByteString, ByteString)的。编辑:在这里使用类型变量表明该函数可以应用于任何东西,但事实并非如此,因为getUserAccessTokenStep2需要一个成对的列表ByteStrings(而不仅仅是一个任意列表)。

编辑:第五个问题是由于FacebookT接管任何monad 的要求太弱(withManager 要求它是一堆更具体的东西,例如MonadIO)。正如您在错误中看到的那样,它是一长串的事情,您最好只删除类型签名fbEmail并启用NoMonomorphismRestriction扩展名(只需将其添加到您拥有的扩展名列表中OverloadedStrings

请注意,通常你应该有明确的类型声明,但我对管道包不太熟悉(我更喜欢管道人:P),我不知道那里是否有一些约束同义词技巧它不那么冗长。

Unless these were just silly typos you might want to read up on monads and monad transformers in one of the many tutorials floating around (or check the haskell wiki).

Also questions such as these might be considered a little too specific for stackoverflow (see the FAQ) and more appropriate to the haskell IRC channel (I'll flag it once we've managed to got it running the way you want, but in future I suggest you try the IRC channel on FreeNode :)).

于 2012-07-22T23:53:44.880 回答