![](/img/trans.png)
[英]Rails - How to add contacts to SendGrid marketing campaigns via API
[英]Get all contacts from sendgrid via api
我正在開發一個應用程序,我在使用 api 注冊期間將用戶添加到 sendgrid:
PUT /marketing/contacts
我還在注冊期間根據用戶的狀態(數據庫數據)將用戶添加到特定列表。
假設我在 sendgrid 列表 A、列表 B、列表 C 中有 3 個列表
現在我每小時運行一個 cron 並檢查用戶的狀態,並根據我想將用戶從列表 A 移動到列表 B 的條件
正如我檢查的那樣,我可以使用相同的 api 將用戶添加到新列表:
PUT /marketing/contacts
我可以從之前的列表中刪除用戶,我可以使用 api:
DELETE /marketing/lists/{id}/contacts
但是之前將用戶添加到哪個列表中並從 sendgrid 獲取聯系人 ID 我需要從 sendgrid 獲取聯系人,我使用的是 api:
GET /marketing/contacts
但是這個 api 只返回最后 50 個數據,分頁選項也不存在。
我還嘗試了 api:
GET https://api.sendgrid.com/v3/contactdb/recipients?page_size=100&page=1
但是這個 api 也給我返回錯誤
error:
{
"errors": [
{
"field": null,
"message": "access forbidden"
}
]
}
但是 api 密鑰沒問題,因為營銷 API 使用相同的 api 密鑰,而 api 密鑰是在具有完全訪問權限的情況下生成的。
https://sendgrid.com/docs/API_Reference/api_v3.html https://sendgrid.com/docs/API_Reference/Web_API_v3/Marketing_Campaigns/contactdb.html
有人可以幫助我通過任何其他 api 從 sendgrid 獲取所有聯系人,或者如果我在上述 api 中缺少任何參數。
聯系支持后,這是我得到的:
為了獲取所有現有聯系人,您可以使用此端點: https://api.sendgrid.com/v3/marketing/contacts/exports
發布/營銷/聯系人/出口
這將返回id
,您使用它來GET /v3/marketing/contacts/exports/<id>
,接收文件。 所以下載它,打開並閱讀 csv。 不完全是我想要的,但它可能會為其他人做這項工作。
關於這個 endoint 的文檔在這里。
我最近經歷了同樣的事情,所以想添加我所做的,希望這能幫助遇到同樣問題的任何人。
這是一段快速而骯臟的代碼,我用它來測試如何獲取整個聯系人列表而實際上不需要在腳本外部打開文件。
request = {
"list_ids": [list_id]
}
response = sg.client.marketing.contacts.exports.post(
request_body=request
)
export_json = response.body.decode('utf8').replace("'", '"')
export_data = json.loads(export_json)
export_id = export_data['id']
urls = []
while True:
response = sg.client.marketing.contacts.exports._(export_id).get()
export_status_json = response.body.decode('utf8').replace("'", '"')
export_status_data = json.loads(export_status_json)
urls = export_status_data['urls']
if len(urls) == 0:
time.sleep(10)
else:
break
for url in urls:
response = requests.get(url)
out_file = gzip.decompress(response.content).decode('utf8'))
pd = pandas.read_csv(io.StringIO(out_file), dtype=str)
如果您想要 json,請將其添加到請求正文並將其讀取為
for url in urls:
response = requests.get(url)
out_file = gzip.decompress(response.content).decode('utf8').replace("'", '"')
print(out_file)
對我來說,我們公司正在使用傳統營銷,所以我必須使用另一個端點 POST v3/contactdb/recipient
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.