haskell Facebook的例子 [英] haskell facebook example

查看:124
本文介绍了haskell Facebook的例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  { - #LANGUAGE OverloadedStrings# - } 

模块主(

)其中

导入合格的Facebook作为FB
导入Network.HTTP.Conduit(withManager)
import Control.Monad.IO .Class(liftIO)
import System.IO

app :: FB.Credentials
app = FB.Credentialslocalhost249348058430770...

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
参数< - readLn
token< - FB.getUserAccessTokenStep2 url [argument]
withManager $ \manager - > do
FB.runFacebookT应用程序管理器$ do
u< - FB.getUserme[] token
liftIO $ print(FB.userEmail u)

错误

  src / Main.hs :23:18:
无法匹配预期类型`IO t0'
与实际类型`FB.FacebookT
FB.Auth m0 text-0.11.2.0:Data.Text.Internal。文本'
在`FB.getUserAccessTokenStep1'的调用的返回类型
在'do'块的一个stmt中:
fbAuthUrl< - FB.getUserAccessTokenStep1 url perms
在表达式中:
do {fbAuthUrl < - FB.getUserAccessTokenStep1 url perms;
liftIO $ print fbAuthUrl;
参数< - readLn;
token< - FB.getUserAccessTokenStep2 url [argument];
....}

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

解决方案

首先,让我从免责声明中解释一下我从未使用Facebook API或 Conduits 库的答案,所以我不确定如果这段代码实际上没有任何明智的感觉,但是只要输入类型信息,我认为这就是你想做的事情。

  main :: IO()
main = withManager $ \manager - > FB.runFacebookT应用程序管理器$ do
fbAuthUrl< - FB.getUserAccessTokenStep1 url perms
liftIO $ print fbAuthUrl
参数< - liftIO $ readLn
令牌< - FB.getUserAccessTokenStep2 url [argument]
u< - FB.getUserme[](只是令牌)
liftIO $ print(FB.userEmail u)

主要缺点是Haskell中的 main 必须始终的类型为 IO a ,但是您尝试使用类型为的值,将您的。您的实现是在正确的轨道上,但$ code> runFacebookT withManager 需要成为函数中的第一件事。 / p>

按类型,实际的do-block有类型 FacebookT Auth(ResourceT IO)()。 $ code> runFacebookT 函数用于展开 FacebookT 变压器,导致$ code> ResourceT IO() 值,然后由 withManager 处理,生成一个简单的旧的 IO()



另外一个问题是你最初在你的do-block中没有 readLn > liftIO ,这使得类型推断变得混乱。我还向 FB.getUser 呼叫中添加了缺少的


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

解决方案

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.

这篇关于haskell Facebook的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆