使用HTTP-conduit时Haskell类型错误



我正在为yesod应用程序进行oauth2身份验证,并且我的类型错误我真的不明白。目前,该代码被打破了,我有几个:: IO ()的s和 undefined s四处乱逛以帮助我隔离类型错误,但相关代码是:

getAccessToken :: Manager -> OAuth2 -> ExchangeToken -> IO (OAuth2Result Errors OAuth2Token)
getAccessToken manager oa code = do
  let (uri, defaultBody) = accessTokenUrl oa code
  let body = defaultBody <> [ ("client_id", TE.encodeUtf8 . oauthClientId $ oa )
                            , ("client_secret", TE.encodeUtf8 . oauthClientSecret $ oa)
                            , ("resource", TE.encodeUtf8 . oauthClientId $ oa)
                            ]
  response <- performOAuth2PostRequest manager oa uri body
  return undefined
performOAuth2PostRequest :: Manager -> OAuth2 -> URI -> PostBody -> IO (Response ByteString)
performOAuth2PostRequest manager oa uri body  = do
  defaultReq <- uriToRequest uri
  let addBasicAuth = applyBasicAuth (TE.encodeUtf8 . oauthClientId $ oa)
                                    (TE.encodeUtf8 . oauthClientSecret $ oa)
  let req = (addBasicAuth . updateRequestHeaders Nothing) defaultReq
  (httpLbs (urlEncodedBody body req) manager) :: IO (Response ByteString)

请注意,我专门将httpLbs (urlEnc...) manager操作的类型设置为使用ScopedTypeVariables扩展程序的IO (Response ByteString)。另外,代码的行应该是IO动作,因为它是在IO操作的最高级别执行的。

实际上,我进行了GHCI会话,并做到了:

Network.OAuth.OAuth2.HttpClient Network.OAuth.OAuth2.Internal 
Network.HTTP.Conduit Data.Functor Prelude> :t httpLbs
httpLbs
  :: Control.Monad.IO.Class.MonadIO m =>
     Request
     -> Manager -> m (Response Data.ByteString.Lazy.Internal.ByteString)

证实我的理解httpLbs应该产生MonadIO m => m (Response ByteString)

,但这是我遇到的错误:

• Couldn't match type ‘Response
                         Data.ByteString.Lazy.Internal.ByteString’
                 with ‘IO (Response ByteString)’
  Expected type: Manager -> IO (Response ByteString)
    Actual type: Manager
                 -> Response Data.ByteString.Lazy.Internal.ByteString
• The function ‘httpLbs’ is applied to two arguments,
  its type is ‘Request
               -> m1 (Response Data.ByteString.Lazy.Internal.ByteString)’,
  it is specialized to ‘Request
                        -> Manager -> Response Data.ByteString.Lazy.Internal.ByteString’

为什么GHC专门将m用于Response而不是IO?我该如何修复?

您尚未包含您的导入语句,因此很难调试。我最好的猜测是,您已导入Network.HTTP.Simple,它提供了不需要明确Manager参数的功能。我从提供预期类型的错误消息中猜测这一点:

Request -> m1 (Response Data.ByteString.Lazy.Internal.ByteString)

解决方案:更改导入或删除Manager参数。

相关内容

最新更新