httr POST 请求正文中带有身份验证令牌
httr POST request with auth token in body
我正在寻找 POST 一个数组,通过他们的 API 通过 httr
包在电子邮件服务提供商中创建一个新的配置文件。 API 文档可以在 here.
中找到
通过文档提供的 curl 命令的复制和粘贴有效,但我的实现抛出 403。为了在此处发布,密钥已被替换。
我真的很难确定这个问题。我对 API 调用相当陌生,尤其是 POST,我觉得我缺少一个核心概念。
我的尝试:
identify_body <- '{
"token" : "public_key_goes_here",
"properties" : {
"$email" : "thomas.jefferson@klaviyo.com",
"$first_name" : "Thomas",
"$last_name" : "Jefferson",
"Plan" : "Premium",
"SignUpDate" : "2016-05-01 10:10:00"
}
}'
httr::POST("https://a.klaviyo.com/api/identify",
body = identify_body,
content_type_json(),
verbose())
结果:
-> POST /api/identify HTTP/1.1
-> Host: a.klaviyo.com
-> User-Agent: libcurl/7.59.0 r-curl/3.3 httr/1.4.0
-> Accept-Encoding: gzip, deflate
-> Accept: application/json, text/xml, application/xml, */*
-> Content-Type: application/json
-> Content-Length: 222
->
>> {
>> "token" : "public_key_goes_here",
>> "properties" : {
>> "$email" : "thomas.jefferson@klaviyo.com",
>> "$first_name" : "Thomas",
>> "$last_name" : "Jefferson",
>> "Plan" : "Premium",
>> "SignUpDate" : "2016-05-01 10:10:00"
>> }
>> }
<- HTTP/1.1 403 Forbidden
<- Content-Encoding: gzip
<- Content-Type: text/html
<- Date: Mon, 04 Jan 2021 22:02:12 GMT
<- Server: nginx
<- Vary: Accept-Encoding
<- Vary: Cookie
<- Content-Length: 779
<- Connection: keep-alive
<-
Response [https://a.klaviyo.com/api/identify]
Date: 2021-01-04 22:02
Status: 403
Content-Type: text/html
Size: 1.46 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="robots" content="NONE,NOARCHIVE">
<title>403 Forbidden</title>
<style type="text/css">
html * { padding:0; margin:0; }
body * { padding:10px 20px; }
来自文档
The Server-Side APIs use the same request and response formats. Requests are made with a GET request to the specified endpoint with a single parameter, data, which is a JSON object that has been base64 and URL encoded
您需要将请求转换为 GET 请求并将 JSON 转换为 base64 / URL 编码字符串并作为名为 data
.[=12 的查询参数传递=]
或者您可以使用他们提供的帮助程序库之一。
我正在寻找 POST 一个数组,通过他们的 API 通过 httr
包在电子邮件服务提供商中创建一个新的配置文件。 API 文档可以在 here.
通过文档提供的 curl 命令的复制和粘贴有效,但我的实现抛出 403。为了在此处发布,密钥已被替换。
我真的很难确定这个问题。我对 API 调用相当陌生,尤其是 POST,我觉得我缺少一个核心概念。
我的尝试:
identify_body <- '{
"token" : "public_key_goes_here",
"properties" : {
"$email" : "thomas.jefferson@klaviyo.com",
"$first_name" : "Thomas",
"$last_name" : "Jefferson",
"Plan" : "Premium",
"SignUpDate" : "2016-05-01 10:10:00"
}
}'
httr::POST("https://a.klaviyo.com/api/identify",
body = identify_body,
content_type_json(),
verbose())
结果:
-> POST /api/identify HTTP/1.1
-> Host: a.klaviyo.com
-> User-Agent: libcurl/7.59.0 r-curl/3.3 httr/1.4.0
-> Accept-Encoding: gzip, deflate
-> Accept: application/json, text/xml, application/xml, */*
-> Content-Type: application/json
-> Content-Length: 222
->
>> {
>> "token" : "public_key_goes_here",
>> "properties" : {
>> "$email" : "thomas.jefferson@klaviyo.com",
>> "$first_name" : "Thomas",
>> "$last_name" : "Jefferson",
>> "Plan" : "Premium",
>> "SignUpDate" : "2016-05-01 10:10:00"
>> }
>> }
<- HTTP/1.1 403 Forbidden
<- Content-Encoding: gzip
<- Content-Type: text/html
<- Date: Mon, 04 Jan 2021 22:02:12 GMT
<- Server: nginx
<- Vary: Accept-Encoding
<- Vary: Cookie
<- Content-Length: 779
<- Connection: keep-alive
<-
Response [https://a.klaviyo.com/api/identify]
Date: 2021-01-04 22:02
Status: 403
Content-Type: text/html
Size: 1.46 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="robots" content="NONE,NOARCHIVE">
<title>403 Forbidden</title>
<style type="text/css">
html * { padding:0; margin:0; }
body * { padding:10px 20px; }
来自文档
The Server-Side APIs use the same request and response formats. Requests are made with a GET request to the specified endpoint with a single parameter, data, which is a JSON object that has been base64 and URL encoded
您需要将请求转换为 GET 请求并将 JSON 转换为 base64 / URL 编码字符串并作为名为 data
.[=12 的查询参数传递=]
或者您可以使用他们提供的帮助程序库之一。