如何在Haskell中执行Google自定义搜索(使用gogol)?
我正在尝试使用Gogol执行Google自定义搜索。我有下面的代码,但它不能编译,我不明白错误信息告诉我为什么。如何让程序编译以便Google搜索起作用?如何在Haskell中执行Google自定义搜索(使用gogol)?
searchCoversCustomly :: Text -> IO Search
searchCoversCustomly phrase = do
env <- G.newEnv -- create google environment
rVols <- G.runResourceT . G.runGoogle env $ do -- run request
let request = (cSEList phrase) -- request construction
& (cselCx .~ Just cx)
. (cselNum .~ 3)
. (cselSearchType .~ Just Image)
G.send request
return rVols
where cx = "..."
这将导致以下错误消息:
src/GoogleInteraction.hs:56:12: error:
• Ambiguous type variable ‘s0’ arising from a use of ‘G.newEnv’
prevents the constraint ‘(G.AllowScopes s0)’ from being solved.
Probable fix: use a type annotation to specify what ‘s0’ should be.
These potential instances exist:
instance G.AllowScopes '[]
-- Defined in ‘Network.Google.Auth.Scope’
...plus one instance involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In a stmt of a 'do' block: env <- G.newEnv
In the expression:
do { env <- G.newEnv;
rVols <- G.runResourceT . G.runGoogle env
$ do { let ...;
G.send request };
return rVols }
我对谷歌图书还与果戈理工作代码工作片段。不同之处在于我在那里提供了一个相关的范围,如Gogol文档instruct。
对于在gogol-books' docs中记载的BooksScope的书籍,但在gogol-customsearch的文档中没有提及此范围。
如何让程序编译以便Google搜索起作用?谢谢!
===编辑:包括
• Couldn't match expected type ‘IO a0’
with actual type ‘k10 '[] -> k10 '[]’
• Probable cause: ‘G.forbid’ is applied to too few arguments
In the second argument of ‘(<$>)’, namely ‘G.forbid’
错误是说,类型检查想不通使用哪种AllowScopes
实例,因为你没”更改为G.newEnv <$> forbid
===后的错误信息指定范围。由于我对自定义搜索API的细节知之甚少,因此我将不得不推测该怎么做。
假设有自定义的搜索没有的OAuth2范围 - 这似乎是的情况下,由于缺乏预定义作用域的果戈里-customsearch(正如你指出),以及在this question讨论 - 您可以使用Network.Google.Auth.Scopes
中的forbid
“在没有范围授权的情况下注释凭证”。
searchCoversCustomly phrase = do
env <- G.newEnv <&> G.forbid
-- etc.
嗨,感谢您解释错误消息并提出修复建议。 我认为没有OAuth2作用域是正确的,因为谷歌的自定义搜索与附加键= ...键值对一起工作来认证请求。 你可能会解释'禁止'的类型吗?它是'k'[] - > k'[]',但我不明白。这就像'身份证',但与镜头和列表?如何在列表中没有类型(如'[a] - > [a]')? 感谢您花时间回答。 – Arthur
我问,因为我不知道我应该通过什么来禁止 – Arthur
@Arthur [1/2]''[]' - 注意''' - 是* type-level *列表;更具体地说,它是一个用作类型注释的空列表。 '禁止'确实是一个专门的'id';它唯一的目的是确保范围类型注释是''[]'。同样,所有'envScopes。〜booksScope'都将范围类型参数设置为类型级别列表''“[”https://www.googleapis.com/auth/books“]',就像'allow booksScope'做。('envScopes'成为可能的附加事物是,作为一个镜头,它可以用来方便地从环境中通过'Proxy'的方式检索范围。) – duplode
它抱怨的类型变量代表'凭据'(不管那是..我对谷歌API一无所知)。你需要在某处指定类型,或许用'env :: Env Something user2407038
感谢您的解释。如果我正确理解Google自定义搜索,那么您使用附加到URL的key = ... keyvalue-pair进行身份验证。 我在链接凭证文档中看不到Key类型,所以也许这样做不可能? – Arthur