简体   繁体   English

在Haskell中使用http-conduit将请求发布到Google Sheets API

[英]Post request to Google sheets API using http-conduit in Haskell

I am trying to create a post request to spreadsheets API using http-conduit. 我正在尝试使用http-conduit向电子表格API创建发布请求。 Unfortunately the post request does not work and the google sheets server does not provide any feedback as to what I am doing wrong in my request. 不幸的是,发帖请求不起作用,并且Google表格服务器没有提供任何关于我在请求中做错了什么的反馈。

I am using https://developers.google.com/google-apps/spreadsheets/ to create a POST request to add a new row to my spreadsheet. 我正在使用https://developers.google.com/google-apps/spreadsheets/创建POST请求,以向电子表格添加新行。

The code I wrote is: 我写的代码是:

import Network.HTTP.Conduit
import Network.HTTP.Types (hAuthorization)
import Network.Google.OAuth2
import qualified Data.ByteString.Char8 as B8
import qualified Data.ByteString.Builder as B
import qualified Data.ByteString.Lazy as L

scopes = ["https://spreadsheets.google.com/feeds"]

createToken client = getAccessToken client scopes (Just "./key.txt")

authorize token request = request
  { requestHeaders = [(hAuthorization, B8.pack $ "Bearer " ++ token)] }

-- post::String -> OAuth2Client ->String -> IO Status
post url client postData = do
  print "inside post request"
  token <- createToken client
  request <- parseUrl url
  -- $ "<?xml version='1.0' encoding='utf-8'?>" ++
  let pd = B.toLazyByteString $ B.string7  postData
  let req = request{
                method = "POST"
              , requestHeaders = [("Content-Type", "application/atom+xml"),
                                  ("Content-Length",B8.pack $ show $ length postData)]
              , requestBody = RequestBodyLBS pd
              }
  let postReq = authorize token postReq
  res <- withManager $ httpLbs req
  print  res
  return $ responseStatus  res

now when I am calling post with correct url and string data 现在,当我用正确的URL和字符串数据呼叫帖子时

   <entry xmlns="http://www.w3.org/2005/Atom"
       xmlns:gsx="http://schemas.google.com/spreadsheets/2006/extended">
      <gsx:name>Pork</gsx:name>
      <gsx:category>meat</gsx:category>
      <gsx:healthiness>questionable</gsx:healthiness>
      <gsx:type>2</gsx:type>
   </entry>

I get a response of : 我得到以下回应:

  Response {responseStatus = Status {statusCode = 200, statusMessage = "OK"},    responseVersion = HTTP/1.1, responseHeaders = [("Content-      Type","application/binary"),("X-Robots-Tag","noindex, nofollow, nosnippet"),("P3P","CP=\"This is not a P3P policy! See https://support.google.com/accounts/answer/151657?hl=en for more info.\""),("P3P","CP=\"This is not a P3P policy! See https://support.google.com/accounts/answer/151657?hl=en for more info.\""),("Date","Tue, 08 Dec 2015 23:52:23 GMT"),("Expires","Tue, 08 Dec 2015 23:52:23 GMT"),("Cache-Control","private, max-age=0"),("X-Content-Type-Options","nosniff"),("X-Frame-Options","SAMEORIGIN"),("X-XSS-Protection","1; mode=block"),("Content-Length","0"),("Server","GSE"),("Set-Cookie","NID=74=CW9_VUnww09KLa-geN3Oxz2Zdh17ognD-Lw3rsXDRUkdOeaiOOKGM4ZBqPXPlp6yxvApu5yqttLjgpqesV1SP4qsza1ZW6ompUGznQqNmVcU2dmyzWebeY8D-NZUijLI;Domain=.google.com;Path=/;Expires=Wed, 08-Jun-2016 23:52:23 GMT;HttpOnly"),("Set-Cookie","NID=74=o-ILV0SQxX_i8EZDtP1jfCU5McNvdDc9FSuZ6FO8ZwQBrFuLg9oxiNtNAnxBKVubB0C4vL2n8LW5NBQMcmpXMvmaYcO0wZbRs4AxpQ96GFPfz5-y9KRZwJ86m8XIjK46;Domain=.google.com;Path=/;Expires=Wed, 08-Jun-2016 23:52:23 GMT;HttpOnly"),("Alternate-Protocol","443:quic,p=0"),("Alt-Svc","clear")], responseBody = "", responseCookieJar = CJ {expose = [Cookie {cookie_name = "NID", cookie_value = "74=o-ILV0SQxX_i8EZDtP1jfCU5McNvdDc9FSuZ6FO8ZwQBrFuLg9oxiNtNAnxBKVubB0C4vL2n8LW5NBQMcmpXMvmaYcO0wZbRs4AxpQ96GFPfz5-y9KRZwJ86m8XIjK46", cookie_expiry_time = 2016-06-08 23:52:23 UTC, cookie_domain = "google.com", cookie_path = "/", cookie_creation_time = 2015-12-08 23:52:25.6609874 UTC, cookie_last_access_time = 2015-12-08 23:52:25.6609874 UTC, cookie_persistent = True, cookie_host_only = False, cookie_secure_only = False, cookie_http_only = True}]}, responseClose' = ResponseClose}

But the OAuth-Playground gives me: 但是OAuth-Playground给了我:

   HTTP/1.1 201 Created
   Alternate-protocol: 443:quic,p=0
   Content-length: 1048
   X-xss-protection: 1; mode=block
   X-robots-tag: noindex, nofollow, nosnippet
   X-content-type-options: nosniff
   Gdata-version: 1.0
   Transfer-encoding: chunked
   Set-cookie: NID=74=fuRYIEz3fRhSSdeqgNf9nCOi3IDTxo5I2FBkhv94fb5RqTRs-B5CXExV7ot9_vsDYryi8AvWJWoF1MXj-7syryXXzzfERwAskFQ5T1WA0Rzvi6xjIvsoLWRbvqbI08AF;Domain=.google.com;Path=/;Expires   =Wed, 08-Jun-2016 23:25:55 GMT;HttpOnly,    NID=74=nm8Q6cWmCmjvaoJgzXBMrg5w9QJzelMfloleHmIkjNw06jI-kMG8U2NyHdb0O3McHFHtCz4IbIAvfJtCD-EWxw25vmyGyGnBCgnsQqUw8GBc09oe7lyoSeXE7LjQQOKv;Domain=.google.com;Path=/;Expires=Wed, 08-Jun-2016 23:25:55 GMT;HttpOnly
   Expires: Tue, 08 Dec 2015 23:25:55 GMT
   Vary: Accept, X-GData-Authorization, GData-Version
   Server: GSE
   -content-encoding: gzip
   Location: https://spreadsheets.google.com/feeds/list/1hIEq4AAauzI8INelQRIvgxBhmzX44qAB_1QQpFJQ2Xo/od6/private/full/cyevm/246h23pn1mc9h0
   Cache-control: private, max-age=0, must-revalidate, no-transform
   Date: Tue, 08 Dec 2015 23:25:55 GMT
   P3p: CP="This is not a P3P policy! See https://support.google.com/accounts/answer/151657?hl=en for more info.",    CP="This is not a P3P policy! See https://support.google.com/accounts/answer/151657?hl=en for more info."
   Alt-svc: clear
   Content-type: application/atom+xml; charset=UTF-8
   X-frame-options: SAMEORIGIN
   Content-location: https://spreadsheets.google.com/feeds/list/1hIEq4AAauzI8INelQRIvgxBhmzX44qAB_1QQpFJQ2Xo/od6/private/full/cyevm/246h23pn1mc9h0
   <?xml version='1.0' encoding='UTF-8'?>
   <entry xmlns='http://www.w3.org/2005/Atom' xmlns:gsx='http://schemas.google.com/spreadsheets/2006/extended'>
   <id>https://spreadsheets.google.com/feeds/list/1hIEq4AAauzI8INelQRIvgxBhmzX44qAB_1QQpFJQ2Xo/od6/private/full/cyevm</id>
   <updated>2015-12-06T01:22:19.799Z</updated>
   <category scheme='http://schemas.google.com/spreadsheets/2006' term='http://schemas.google.com/spreadsheets/2006#list'/>
   <title type='text'>Pork</title>
   <content type='text'>category: meat, healthiness: questionable, type: 2</content>
   <link rel='self' type='application/atom+xml' href='https://spreadsheets.google.com/feeds/list/1hIEq4AAauzI8INelQRIvgxBhmzX44qAB_1QQpFJQ2Xo/od6/private/full/cyevm'/>
   <link rel='edit' type='application/atom+xml' href='https://spreadsheets.google.com/feeds/list/1hIEq4AAauzI8INelQRIvgxBhmzX44qAB_1QQpFJQ2Xo/od6/private/full/cyevm/246h23pn1mc9h0'/>
   <gsx:name>Pork</gsx:name>
   <gsx:category>meat</gsx:category>
   <gsx:healthiness>questionable</gsx:healthiness>
   <gsx:type>2</gsx:type>
   </entry>

Can someone please let me know what is wrong? 有人可以让我知道怎么了吗? Thanks in advance. 提前致谢。

If authorization information is not passed correctly, google sheets API server sends back 200. After I identified my mistake and passed authorization token correctly it worked for me. 如果未正确传递授权信息,则Google Sheets API服务器会发回200。我确定我的错误并正确传递了授权令牌后,它对我有用。 It is weird why google sends back 200 OK though. 谷歌虽然发回200 OK却很奇怪。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM