pythonrestpaginationpinterest

Pinterest API 'bookmark' always returning the same string and data


I am trying to use pagination the way it is instructed in the Pinterest API Documentation, by passing 'bookmark' as a parameter to the next GET request in order to get the next batch of data.

However, the data returned is the EXACT same as the initial data I had received (without passing 'bookmark') and the value of 'bookmark' is also the same!

With this issue present, I keep receiving the same data over and over and can't get the entirety of the data. In my case I'm trying to list all campaigns.

Here is my python code:

url = f'https://api.pinterest.com/v5/ad_accounts/{ad_account_id}/campaigns'
payload = f"page_size=25"
headers = {
    "Accept": "text/plain",
    "Content-Type": "application/x-www-form-urlencoded",
    "Authorization": f"Bearer {access_token}"
}
response = requests.request("GET", url, data=payload, headers=headers)
print(response)
feed = response.json()
print(feed)
bookmark=''
if 'bookmark' in feed:
    bookmark = feed['bookmark']

print(bookmark)

while(bookmark != '' and bookmark != None and bookmark != 'null'):
    url = f'https://api.pinterest.com/v5/ad_accounts/{ad_account_id}/{level}s'
    payload = f"page_size=25&bookmark={bookmark}"
    headers = {
        "Accept": "text/plain",
        "Content-Type": "application/x-www-form-urlencoded",
        "Authorization": f"Bearer {access_token}"
    }
    response = requests.request("GET", url, data=payload, headers=headers)
    print(response)
    feed = response.json()
    print(feed)
    bookmark = feed['bookmark']
    print(bookmark)

Solution

  • I think your condition in while is wrong, therefore you end up in the same loop. I'm currently also working with Pinterest API and below is my modified implementation how to get a list of ad accounts.

    Basically you're testing if the bookmark is None. If yes, then you can return the result, otherwise you append the bookmark into query parameters and call the endpoint again.

    from app.api.http_client import HttpClient
    
    
    class PinterestAccountsClient():
    
        def get_accounts(self, credentials) -> list[dict]:
            headers = {
                'Authorization': f"Bearer {credentials('access_token')}"
            }
            params = {
                'page_size': 25
            }
    
            accounts = []
            found_last_page = False
            while not found_last_page:
                try:
                    response = HttpClient.get(self.listing_url, headers=headers, params=params)
                    items = response.get('items', [])
                    bookmark = response.get('bookmark')
    
                    if bookmark is None:
                        found_last_page = True
                    else:
                        params['bookmark'] = bookmark
    
                    accounts.extend([{
                        'id': account['id'],
                        'name': account['name']
                    } for account in items])
    
            return accounts