How to get email addresses of each Pledge using API?

HI there

I am trying to use the API (in Python) to get a list of all the email addresses for each pledge on a campaign.

I have the following code which returns the User IDs of all pledges made on a campaign:

def get_emails():

emails = set()

pledge_endpoint = 'https://www.patreon.com/api/oauth2/api/campaigns/{}/pledges?include=patron.null'.format(campaign_id)

def get_emails_from_page(url):
    request = urllib.request.Request(url)

    request.add_header('Authorization', 'Bearer {}'.format(access_token))
    result = urlopen(request).read().decode('utf-8')
    resultJson = json.loads(result)

    for i in range(len(resultJson['data'])):
        if resultJson['data'][i]['attributes']['declined_since'] == None:
            user_id = resultJson['data'][i]['relationships']['patron']['data']['id']
    if resultJson['links'].get('next'):
        next_page = resultJson['links']['next']
        get_emails_from_page(next_page)

get_emails_from_page(pledge_endpoint)

return(list(emails))

How do I now use those User IDs in an API call to access their email address?

I tried the following code on this endpoint https://docs.patreon.com/#get-api-oauth2-v2-members-id but got a HTTP Error 401: UNAUTHORIZED error message:

member_endpoint = 'https://www.patreon.com/api/oauth2/v2/members/{}'.format(user_id)
            member_request = urllib.request.Request(member_endpoint)
            request.add_header('Authorization', 'Bearer {}'.format(access_token))
            member_result = urlopen(member_request).read().decode('utf-8')
            member_resultJson = json.loads(member_result)

I note from the docs that this end point " Requires the campaigns.members scope." - but I don’t know what this means and so this may be where I’m going wrong.

TIA

Small update to my code re Members endpoint:

member_endpoint = 'https://www.patreon.com/api/oauth2/v2/members/{}'.format(user_id)
            member_request = urllib.request.Request(member_endpoint)
            member_request.add_header('Authorization', 'Bearer {}'.format(access_token))
            member_result = urlopen(member_request).read().decode('utf-8')
            member_resultJson = json.loads(member_result)

Had another reference to request earlier in code so changed to member_request, and now I am getting a 404 Not Found error instead of Not Authorised.

Have figured it out, so for anyone else trying to do this in Python, this is the code I used (it excludes any Patrons with a “declined_since” timestamp):

     def get_emails():

emails = []
   api_client = patreon.API(access_token)
    all_pledges = []
    cursor = None
    while True:
        pledges_response = api_client.fetch_page_of_pledges(campaign_id, 25, cursor=cursor)
        all_pledges += pledges_response.data()
        cursor = api_client.extract_cursor(pledges_response)
        if not cursor:
            break

pledge_info = [{
    'email': pledge.relationship('patron').attribute('email'),
    'declined_since': pledge.attribute('declined_since')
} for pledge in all_pledges]

for pledge in pledge_info:
    if pledge['declined_since'] == None:
        emails.append(pledge['email'])

return(emails)

(indenting not formatted correctly above but you will hopefully get the idea!)

1 Like