<?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 Denis Luiz on Medium]]></title>
        <description><![CDATA[Stories by Denis Luiz on Medium]]></description>
        <link>https://medium.com/@denisluiz?source=rss-a8afd371ac8a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*UvxW4QiJfMJqrnYXnInpTQ.png</url>
            <title>Stories by Denis Luiz on Medium</title>
            <link>https://medium.com/@denisluiz?source=rss-a8afd371ac8a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 10:26:59 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@denisluiz/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[Python with Google Sheets Service Account: Step by Step]]></title>
            <link>https://denisluiz.medium.com/python-with-google-sheets-service-account-step-by-step-8f74c26ed28e?source=rss-a8afd371ac8a------2</link>
            <guid isPermaLink="false">https://medium.com/p/8f74c26ed28e</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[google-sheets]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[backend]]></category>
            <category><![CDATA[api]]></category>
            <dc:creator><![CDATA[Denis Luiz]]></dc:creator>
            <pubDate>Wed, 21 Mar 2018 22:52:28 GMT</pubDate>
            <atom:updated>2022-07-26T09:24:47.991Z</atom:updated>
            <content:encoded><![CDATA[<p>A couple of days ago I had my first experience with Google Sheets API. I thought it would be an easy task but well… not.</p><p>I&#39;ve been through the official Google API docs and ok, all the methods can be found there but they&#39;re lacking one big and important thing: a good walkthrough. After my disappointment with the official website, I did what pretty much everyone would do, Googled it =D. I found much more valuable information and finally after a few hours, lots of sites, lots of errors, code, and some coffee, I was able to get my simple script to work.</p><p>After all this trouble I thought it would be interesting to share a simple step-by-step guide to make things happen and so, here I am.</p><p>In this case, we will be using a <strong>service account</strong>, meaning that the authentication will be validated on the back end.</p><p>Please make sure you have Python and pip installed. If you don&#39;t know how to do that you should look <a href="https://www.python.org/">here</a> and <a href="https://pip.pypa.io/en/stable/installing/">here</a>.</p><p>Ok, let&#39;s begin \o/</p><p>The first thing you need to do is to create a project. You know when you authenticate on some app using your Google Account and then you get the message: &quot;some app needs your permission to…&quot;? It&#39;s the same thing, we will be creating this &quot;app&quot;.</p><p>To do that, simply go to <a href="https://console.developers.google.com/projectselector/apis/library?pli=1&amp;supportedpurview=project">Google Console</a> and create a new project. In this case, I named my project <strong>Inserting Data Through API</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1022/1*WNmm3sfIkQf3VA8ouPVItg.png" /></figure><p>The next step is to enable the APIs we are going to use. You&#39;ll see at the top of this very same page a link called: <strong>Enable APIs and Services</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VGB_8N5Qy3sEov-kPIqI-w.png" /></figure><p>Search for the <strong>Google Drive API</strong> and <strong>Enable it</strong>.</p><p><em>By doing this, </em><strong><em>Google Sheets API</em></strong><em> should be enabled automatically but it&#39;s good to have a second check, just in case.</em></p><p>The next step is to create our credentials. For that, click on Credentials on the left panel and then <strong>Create Credentials</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dfrpGEhywT7VZW0DiI3bgQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/854/1*ME58mjvWiUMjKb9vwTO67g.png" /></figure><p>When asked about what kind of credential select <strong>Service Account Key</strong>.</p><p>Click on the Service Account combo and select <strong>New Service Account</strong>. Give it a name and select a role (<strong>Project -&gt; Editor</strong> should be more than enough for all purposes we are trying to achieve here).</p><p>Select <strong>JSON</strong> for the <strong>key type</strong> then click <strong>Create</strong>. A JSON file will be downloaded. Move this file to the directory where your python script will be created and rename it to client_secret.json.</p><p>Good, we are halfway through.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hBkTsITMyA-iLegU71Db4Q.png" /></figure><p>Now we are good to create our spreadsheet in Google Sheets and share it with our credentials. To do that, open your client_secret.json file, copy the <strong>client_email</strong> information, and <strong>share your spreadsheet with this email</strong>. Please be certain to share it with the &quot;<strong>can edit</strong>&quot; option.</p><p>Ok, now that all the bureaucratic needs have been attended to, we can finally start writing our script to insert data into our spreadsheet.</p><p>The first thing to do is to install the libraries from Google API and that’s why we need pip ;-)</p><p>Go to your environment’s terminal app and run <strong>pip install</strong> <strong>google-auth-httplib2</strong> and <strong>pip install google-api-python-client</strong>. These are both necessary to authenticate our Google’s secret credentials and to connect with the spreadsheet so make sure you have them installed.</p><p>As with any Python script, we will begin importing the necessary stuff we need. In this case that would be the Google libraries we&#39;ve installed before:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/844/1*lThkoJRrD3Yh_BLEE1zT3A.png" /></figure><p>The <strong>scopes</strong> are needed for the type of authorisation you need. More information can be found <a href="https://developers.google.com/identity/protocols/googlescopes#sheetsv4">here</a>.</p><p>With the <strong>secret file</strong> and the <strong>scopes,</strong> we can authenticate on Google and create a service to list the methods we need.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0qUdiMVOsquMLA2fpMp5oQ.png" /></figure><p>A very important piece of information that we need now is the <strong>spreadsheet_id</strong> and this can be found in the URL of your spreadsheet.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1QhoxsBaUr65GUqkRibJLA.png" /></figure><p>Now you can go to <em>infinity and beyond</em>. All you have to do is to get your data, determine the range of the spreadsheet you want to update, and execute the method.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-TUpAGz1_dKJZNlHCLmIlQ.png" /></figure><p>Your full script should look something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f429f4813be4bfedb3f87008364383bb/href">https://medium.com/media/f429f4813be4bfedb3f87008364383bb/href</a></iframe><p>And that&#39;s all. Simple right? Why complicate so much?</p><p>In this case, we have just inserted data into the spreadsheet but if you need all other methods, these can be found <a href="https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/update">here</a>. All you have to do is to change the data, the range and the method.</p><p>I truly hope that this tutorial helped you in some way (especially if it saved you hours of search and work \o/)</p><p>That&#39;s all folks.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o7sH7jgDznvnjbJVgKOXWw.jpeg" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8f74c26ed28e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>