[英]Basic way of sending HTTP/POST in Haskell using http-conduit
[英]Post request to Google sheets API using http-conduit in Haskell
我正在嘗試使用http-conduit向電子表格API創建發布請求。 不幸的是,發帖請求不起作用,並且Google表格服務器沒有提供任何關於我在請求中做錯了什么的反饋。
我正在使用https://developers.google.com/google-apps/spreadsheets/創建POST請求,以向電子表格添加新行。
我寫的代碼是:
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
現在,當我用正確的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>
我得到以下回應:
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}
但是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>
有人可以讓我知道怎么了嗎? 提前致謝。
如果未正確傳遞授權信息,則Google Sheets API服務器會發回200。我確定我的錯誤並正確傳遞了授權令牌后,它對我有用。 谷歌雖然發回200 OK卻很奇怪。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.