0

我正在为 Yesod 应用程序进行 OAuth2 身份验证,但我遇到了一个我真的不明白的类型错误。代码现在坏了,我扔了几个:: IO ()'s 和s 来帮助我隔离类型错误,但相关代码是:undefined

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操作的类型设置为IO (Response ByteString)使用ScopedTypeVariables扩展名。此外,这行代码应该是一个 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? 我如何解决它?

4

1 回答 1

1

您没有包含导入语句,因此很难调试。不过,我最好的猜测是您已经导入Network.HTTP.Simple了 ,它提供了不需要显式Manager参数的函数。我从提供预期类型的​​错误消息中猜到了这一点:

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

解决方案:要么更改导入,要么删除Manager参数。

于 2017-11-30T09:42:31.670 回答