我希望通过 httr
包的 API 发布一个数组,以便在电子邮件服务提供商中创建新配置文件。 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; }
答案 0 :(得分:1)
来自文档
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
的查询参数传递。
或者您可以使用他们提供的帮助库之一。