Update a subscriber custom field using campaign monitor API and python

475 Views Asked by At

I've tried getting this to work every which way I can. I've poured through the campaign monitor API docs and search every possible article on the internet I could find. I didn't find one single example of code to update a subscriber record. I can get the record. I just can't update it and I am hoping someone can help me figure out what I am doing wrong. Thank you!

from createsend import *

def GetSubscribers(token, refresh_token, list_id):
    auth = {
        'access_token': token,
        'refresh_token': refresh_token}
    user_email = '[email protected]'
    subscriber = Subscriber(auth, list_id, user_email).get()

    for i in subscriber.CustomFields:
        for n in i.__dict__.keys():
            print(i.__dict__[n])
            if i.__dict__[n] == "campaign_code":
                print(n, i.__dict__[n])
                print(i.__dict__["Value"])
                campaign_code = i.__dict__["Value"]
                new_campaign_code = campaign_code + "_REPLIED"
                print(new_campaign_code)

                custom_fields = [
                    {"Key": i.__dict__[n], "Value": new_campaign_code}]
                Subscriber.update(user_email, "Subscriber",
                                  custom_fields, True, True, "Yes")

params = {"email": self.email_address}

AttributeError: 'str' object has no attribute 'email_address'

1

There are 1 best solutions below

0
On

I figured it out. This is the correct structure to update a subscriber using the createsend python wrapper:

custom_fields = [{"Key": key, "Value": value}]

subscriber = Subscriber(auth, list_id, user_email).update( new_user_email, "Subscriber", custom_fields, True, "Yes")

This one took a lot of time for me to figure out. I hope this post will help save some time for anyone trying to update subscribers in campaign monitor.