Pinterest API“书签”总是返回相同的字符串和数据

发布于 2025-01-09 03:34:02 字数 1241 浏览 1 评论 0原文

我尝试按照 Pinterest API 文档中指示的方式使用分页,将“书签”作为参数传递给下一个 GET 请求,以便获取下一批数据。

但是,返回的数据与我收到的初始数据完全相同(没有传递“书签”),并且“书签”的值也相同!

由于存在此问题,我不断地一遍又一遍地接收相同的数据,并且无法获取完整的数据。就我而言,我试图列出所有活动。

这是我的Python代码:

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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云裳 2025-01-16 03:34:02

我认为您在 while 中的条件是错误的,因此您最终会陷入相同的循环。我目前也在使用 Pinterest API,下面是我修改后的实现,如何获取广告帐户列表。

基本上,您正在测试书签是否为None。如果是,则可以返回结果,否则将书签附加到查询参数中并再次调用端点。

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文