Python Friday #131: Working With Bookmarks in Tweepy

Since I concluded my experiments with Tweepy in April, a new and important Twitter endpoint finally arrived: Bookmarks. Let’s have a look how we can access our Twitter bookmarks with Tweepy.

This post is part of my journey to learn Python. You can find the other parts of this series here. You find the code for this post in my PythonFriday repository on GitHub.

 

Update Tweepy

Before we can do anything, we need to make sure we have the newest version of Tweepy. You can install Tweepy or update to the newest version with this command:

Make sure that you have at least version 4.8:

Name: tweepy
Version: 4.10.0
Summary: Twitter library for Python

 

Get the right kind of user token

According to the Twitter API v2 authentication mapping, we need an “OAuth 2.0 Authorization Code with PKCE” to access the bookmark endpoints:

The mapping table shows bookmark endpoints are only supported by PKCE

We can use the OAuth 2.0 Authorization Code Flow with PKCE (User Context) to get the right token with Tweepy:

This is similar to the token we used before, but with a different protocol.

You must ask for the scopes “bookmark.read“, “bookmark.write“,”tweet.read” and “users.read“. Otherwise, your calls to the bookmark endpoint will end with this error:

tweepy.errors.Forbidden: 403 Forbidden

We can put the token in our .env file and access it where we want to work with bookmarks:

 

Bookmark a tweet

We can bookmark a tweet with the bookmark() method:

In the response we get the confirmation that this tweet got added to our bookmarks:

Tweet 1536895050176667649 bookmarked: True

 

Read your bookmarks

We can get up to 800 bookmarks of the authenticated user with the method get_bookmarks():

As with search in the V2 client, we need to declare what data we want. In the example above we ask for the tweet itself, the user who tweeted it, the media files that may be attached to the tweet and the metrics. That gives us an output like this:

————————————————–
1534959411185209348 (2022-06-09 18:03:36+00:00) – Python Software Foundation (@ThePSF) [https://pbs.twimg.com/profile_images/1517584461017370624/DF3DpXUW_normal.jpg]:
Check out our latest blog post, where you’ll see our 2021 Annual Report! https://t.co/rjkzvLwLXu

2022 is well underway, but there’s still time to reflect on the challenges and successes of 2021, and to give kudos to the many people behind our work and progress last year. https://t.co/O1EwPMkv4h

retweets: 46 | likes: 183
Media attachment: #https://pbs.twimg.com/media/FU1Dz2BWUAc2DoN.png – 400×524 – Alt: The cover image for the PSF’s 2021 annual report, which features a drawing of a python slithering diagonally across to almost fill the page, in neon versions of the Python and PSF colors – blue and yellow. The words “The 20th ANNIVERSARY of the PYTHON SOFTWARE FOUNDATION” are written on the snake’s body. The background is very dark blue, and the feel of the cover is noisy and neon (almost a 20s take on 80s aesthetic?). “2021 Annual Report” is in the top left corner. The Python Softward Foundation logo in monochrome yellow to match the ssnake is in the top right corner. White lightning bolts surround the snake as well as geometric squiggles and lines in complementary ombre’d colors.
————————————————–
1536895050176667649 (2022-06-15 02:15:08+00:00) – Python Hub (@PythonHub) [https://pbs.twimg.com/profile_images/1152895149594284032/wb1uTnTk_normal.jpg]:
Python 3.10.5 is available

The latest bugfix drop for Python 3.10 is here: Python 3.10.5. This release packs more than 230 bugfixes and docs changes.

https://t.co/0HlfPhSVNz

retweets: 11 | likes: 40
————————————————–

 

Remove a bookmarked tweet

We can delete a bookmarked tweet with the remove_bookmark() method:

If we run this code, the tweet is no longer bookmarked:

Tweet 1536895050176667649 bookmarked: False

 

Conclusion

The bookmark endpoint was the final missing part to access Twitter through the API. This gap is now closed, and the new methods are straightforward. However, there is the challenge of getting the right token, that took me a while to figure out. I hope this post gives you a smoother experience.

2 thoughts on “Python Friday #131: Working With Bookmarks in Tweepy”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.