简体   繁体   English

通过 api 从 sendgrid 获取所有联系人

[英]Get all contacts from sendgrid via api

I'm working on a app where I add the user to sendgrid during sign up using the api:我正在开发一个应用程序,我在使用 api 注册期间将用户添加到 sendgrid:

PUT /marketing/contacts

I also add the user to specific list based on the status(DB data) of the user during signup.我还在注册期间根据用户的状态(数据库数据)将用户添加到特定列表。

Say I've 3 lists in sendgrid List A, List B, List C假设我在 sendgrid 列表 A、列表 B、列表 C 中有 3 个列表

Now I run a cron on every hour and check the status of the user and based on the condition I want to move the user from say List A to List B现在我每小时运行一个 cron 并检查用户的状态,并根据我想将用户从列表 A 移动到列表 B 的条件

As I checked I can add the user to the new list using the same api:正如我检查的那样,我可以使用相同的 api 将用户添加到新列表:

PUT /marketing/contacts

And I can remove the user from the previous list I can use the api:我可以从之前的列表中删除用户,我可以使用 api:

DELETE /marketing/lists/{id}/contacts

But to in which list the user was previously added and to get the contact id from sendgrid I need to get the contacts from sendgrid, I'm using the api:但是之前将用户添加到哪个列表中并从 sendgrid 获取联系人 ID 我需要从 sendgrid 获取联系人,我使用的是 api:

GET /marketing/contacts

But this api is only returning last 50 data and pagination option is also not there.但是这个 api 只返回最后 50 个数据,分页选项也不存在。

I also tried the api:我还尝试了 api:

GET https://api.sendgrid.com/v3/contactdb/recipients?page_size=100&page=1

But this api is also returning me error但是这个 api 也给我返回错误

error:
{
  "errors": [
     {
       "field": null,
       "message": "access forbidden"
     }
  ]
}

But the api key is fine because the marketing apis works with the same api key and the api key is generated with full access.但是 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 https://sendgrid.com/docs/API_Reference/api_v3.html https://sendgrid.com/docs/API_Reference/Web_API_v3/Marketing_Campaigns/contactdb.html

Can someone please help me to get all contacts from sendgrid via any other api or if there is any params I'm missing in the above mentioned apis.有人可以帮助我通过任何其他 api 从 sendgrid 获取所有联系人,或者如果我在上述 api 中缺少任何参数。

After contacting support, this is what I got:联系支持后,这是我得到的:

In order to fetch all the existing contacts you can use this endpoint: https://api.sendgrid.com/v3/marketing/contacts/exports为了获取所有现有联系人,您可以使用此端点: https://api.sendgrid.com/v3/marketing/contacts/exports
POST /marketing/contacts/exports发布/营销/联系人/出口

This will return id , which you use to GET /v3/marketing/contacts/exports/<id> , receiving a file .这将返回id ,您使用它来GET /v3/marketing/contacts/exports/<id> ,接收文件 So download it, open and read csv.所以下载它,打开并阅读 csv。 Not exactly what I wanted, but it might do the job for others.不完全是我想要的,但它可能会为其他人做这项工作。

Docs about this endoint here .关于这个 endoint 的文档在这里

I recently went through the same thing so want to add what I did in the hopes that this helps anyone with the same issue.我最近经历了同样的事情,所以想添加我所做的,希望这能帮助遇到同样问题的任何人。

This is quick and dirty code that I used to test out how to grab an entire contact list without actually needing to open the file outside the script.这是一段快速而肮脏的代码,我用它来测试如何获取整个联系人列表而实际上不需要在脚本外部打开文件。

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)

If you want json, add that to the request body and read it as如果您想要 json,请将其添加到请求正文并将其读取为

for url in urls:
    response = requests.get(url)
    out_file = gzip.decompress(response.content).decode('utf8').replace("'", '"')


    print(out_file)

For me, our company is using legacy marketing so i have to use another endpoint POST v3/contactdb/recipient对我来说,我们公司正在使用传统营销,所以我必须使用另一个端点 POST v3/contactdb/recipient

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

相关问题 Rails-如何通过API将联系人添加到SendGrid营销活动中 - Rails - How to add contacts to SendGrid marketing campaigns via API 尝试通过 SendGrid 联系人 API 添加新联系人时出现错误 400 - Error 400 when trying to add new contact via SendGrid Contacts API 通过 Sendgrid 阻止的所有电子邮件,如何再次发送电子邮件? - All emails blocked via Sendgrid, how to get emails delivered again? Sendgrid使用Web API获取所有传递的电子邮件 - Sendgrid get all delivered email using web API 是否可以通过SendGrid API迭代收集? - Is it possible to iterate over collection via SendGrid API? 通过API 3.0 SendGrid邀请队友 - SendGrid Invite Teammate via API 3.0 从sendgrid API发送的附件中通过回形针将图像上传到s3 - Uploading an image to s3 via paperclip from an attachment sent by the sendgrid API "通过 SendGrid API 从命令行发送复杂的 html 作为电子邮件正文" - Sending a complex html as the body of email from the command line via an SendGrid API 从其所有先前的消息和元数据(Sendgrid Parse API / PHP)中提取电子邮件消息本身? - Extract email message itself from all its prior messages and meta data (Sendgrid Parse API/PHP)? 如何获取Azure SendGrid API密钥 - How to get Azure SendGrid API Key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM