<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Nikhil C on Medium]]></title>
        <description><![CDATA[Stories by Nikhil C on Medium]]></description>
        <link>https://medium.com/@cn.april?source=rss-8d327fd89e06------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*HG2CU6X-lL3kXzaq</url>
            <title>Stories by Nikhil C on Medium</title>
            <link>https://medium.com/@cn.april?source=rss-8d327fd89e06------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 21:35:13 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@cn.april/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Posting to twitter with Python — The experience and code]]></title>
            <link>https://medium.com/@cn.april/posting-to-twitter-with-python-the-experience-and-code-fe62418e5af1?source=rss-8d327fd89e06------2</link>
            <guid isPermaLink="false">https://medium.com/p/fe62418e5af1</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[tweepy]]></category>
            <category><![CDATA[automate]]></category>
            <category><![CDATA[twitter-automation]]></category>
            <category><![CDATA[lifehacks]]></category>
            <dc:creator><![CDATA[Nikhil C]]></dc:creator>
            <pubDate>Wed, 08 Nov 2023 06:04:30 GMT</pubDate>
            <atom:updated>2023-11-08T06:13:29.180Z</atom:updated>
            <content:encoded><![CDATA[<h3>Posting to twitter with Python — The experience and code</h3><p>I recently participated in #30daysofJavascript event and someone suggested that we share this on twitter/X. I do like to read tweets on twitter but I am not a tweeting person. Nonetheless, I decided to go for it. As they say,</p><blockquote>To improve is to change; to be perfect is to change often</blockquote><p>Back to the topic. So I had</p><ol><li>to post to twitter with a screenshot of the challenge that I had solved for the day</li><li>with some hashtags.</li></ol><p>While this is a minute task, I did not enjoy it in the least. So, my mind began to wonder if I could skip the posting part. I supported my mind by quickly browsing the internet and sure enough there was a way.</p><h3>Getting Started:</h3><h4>1. Setting up the Environment</h4><p>So listing down the requirements for posting to twitter using python</p><ol><li><a href="https://www.python.org/downloads/">python</a> — any version greater than 3.7<em>(Mine is 3.7.9)</em></li><li><a href="https://pypi.org/project/tweepy/">tweepy</a> — latest version (We are using this to reduce our work of going through the API documentation and exploration completely. Believe me, this makes it a lot easier to handle.)</li><li>subscribing to the twitter API on <a href="https://developer.twitter.com/en">https://developer.twitter.com/en</a></li></ol><p>So, I will not be covering how to install python. You can find a ton of articles for that on the internet.</p><p>To install tweepy, here is the link — <a href="https://pypi.org/project/tweepy/">https://pypi.org/project/tweepy/</a></p><p>I used the very simple command,</p><pre>pip install tweepy</pre><p>Now we have completed installing the dependencies.</p><h4>2. Subscribing to the twitter API</h4><ol><li>Go to <a href="https://developer.twitter.com/">https://developer.twitter.com/</a></li><li>Signin with your twitter account.</li><li>Once you login, there will be a default project created for you.</li><li>Click on the key icon beside your project to regenerate the keys</li><li>Regenerate the keys and copy them somewhere</li></ol><h4>3. Working on the code</h4><p>I created a blank file named <em>tweeter.py. </em>I am aware that my naming sense isn’t very great!</p><p>My very first implementation of the code(Ref 1 and 2) failed giving me a</p><blockquote>403 forbidden error.</blockquote><p>The second implementation gave an error(added a cat pic to the post to make it more complicated, lol!)</p><blockquote>453 — You currently have access to a subset of Twitter API v2 endpoints and limited v1.1 endpoints (e.g. media post, oauth) only. If you need access to this endpoint, you may need a different access level.</blockquote><p>For a beginner like me, this was very confusing. Apparently, I understood that there are 2 versions of the twitter API. But I did not know this before or what this information meant exactly.</p><p>So, I believed that the tweepy library was trying to call the twitter API v1.1 instead of the new 2.0. So I started searching the internet on how to call the API 2.0 via tweepy. But of course, as every beginner eventually realizes, his understandings are often flawed.</p><p>So, the eye-opener for me was the stackoverflow answer mentioned in reference 4 below. So I had to post the tweet using twitter APIv2.0 and upload the cat pic using APIv1.1.</p><p>So, go to <a href="https://developer.twitter.com/">https://developer.twitter.com/</a> again.</p><ol><li>Create a ‘client’ and generate credentials</li><li>Give <strong><em>Read and Write</em></strong> access on the next page(We need the <strong><em>write</em></strong> access to be able to post tweets!)</li><li>Now, click on Regenerate for the “Access token and Secret”.</li><li>The access token and secret will now be created with Read and Write permissions. This should resolve the 1st error that I mentioned</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4725ed163d51640141dfe7b83a26ad29/href">https://medium.com/media/4725ed163d51640141dfe7b83a26ad29/href</a></iframe><p>And here is the tweet:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/858/1*_jpXEu0hXR7heD40VKq7rw.png" /><figcaption>Automated Twitter Post</figcaption></figure><iframe src="https://cdn.embedly.com/widgets/media.html?type=text%2Fhtml&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;schema=twitter&amp;url=https%3A//twitter.com/ultimatenyx/status/1721746049319198745%3Fs%3D20&amp;image=https%3A//i.embed.ly/1/image%3Furl%3Dhttps%253A%252F%252Fabs.twimg.com%252Ferrors%252Flogo46x38.png%26key%3Da19fcc184b9711e1b4764040d3dc5c07" width="500" height="281" frameborder="0" scrolling="no"><a href="https://medium.com/media/1d7863af359ac65f9b8b1568e81bc42c/href">https://medium.com/media/1d7863af359ac65f9b8b1568e81bc42c/href</a></iframe><p>And this marks the end of the post! The API and tweepy documentation are extensive and you can use it for a lot of cool stuff. Feel free to explore!</p><p>References:<br><a href="https://www.mattcrampton.com/blog/step_by_step_tutorial_to_post_to_twitter_using_python_part_two-posting_with_photos/">https://www.mattcrampton.com/blog/step_by_step_tutorial_to_post_to_twitter_using_python_part_two-posting_with_photos/</a></p><p><a href="https://developer.twitter.com/en/docs/tutorials/tweeting-media-v2#:~:text=You%20can%20use%20the%20simple_upload,Tweet%20you%20want%20to%20post">https://developer.twitter.com/en/docs/tutorials/tweeting-media-v2#:~:text=You%20can%20use%20the%20simple_upload,Tweet%20you%20want%20to%20post</a>.</p><p>[<a href="https://gist.github.com/sparack/e8021298115f4a1289a637e25f284daf">https://gist.github.com/sparack/e8021298115f4a1289a637e25f284daf</a>]</p><p><a href="https://stackoverflow.com/questions/70129448/how-to-create-tweet-using-tweepy-api-v-2/76509550#76509550">https://stackoverflow.com/questions/70129448/how-to-create-tweet-using-tweepy-api-v-2/76509550#76509550</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fe62418e5af1" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>