<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: George Salukvadze</title>
    <description>The latest articles on DEV Community by George Salukvadze (@giosal).</description>
    <link>https://dev.to/giosal</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F67110%2Fccfdf822-65b7-4c14-b3d3-0553b75d5d22.jpg</url>
      <title>DEV Community: George Salukvadze</title>
      <link>https://dev.to/giosal</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/giosal"/>
    <language>en</language>
    <item>
      <title>Online expense tracker — Part 1 — Setting up access to Gmail</title>
      <dc:creator>George Salukvadze</dc:creator>
      <pubDate>Mon, 20 Apr 2020 11:01:19 +0000</pubDate>
      <link>https://dev.to/giosal/online-expense-tracker-part-1-setting-up-access-to-gmail-kmh</link>
      <guid>https://dev.to/giosal/online-expense-tracker-part-1-setting-up-access-to-gmail-kmh</guid>
      <description>&lt;p&gt;Original post appeared &lt;a href="https://giosal.ge/blog/index.php/2020/04/18/online-expense-tracker-part-1-setting-up-access-to-gmail/"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I’ve decided to write a python program that parses my emails from Gmail and finds the emails that contain information about my online spending — online shopping as well as monthly subscriptions.&lt;br&gt;
So this is a series of posts describing how to do it. The content will be updated as I add new posts.&lt;/p&gt;
&lt;h1&gt;
  
  
  Contents
&lt;/h1&gt;

&lt;p&gt;Part 1 — Setting up access to Gmail&lt;/p&gt;
&lt;h2&gt;
  
  
  Part 1
&lt;/h2&gt;

&lt;p&gt;This part is relatively simple, all you need to do is follow this tutorial here (&lt;a href="https://developers.google.com/gmail/api/quickstart/python"&gt;https://developers.google.com/gmail/api/quickstart/python&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;I will outline most important points&lt;/p&gt;
&lt;h3&gt;
  
  
  Prerequisites
&lt;/h3&gt;

&lt;p&gt;Python 2.6 or greater&lt;br&gt;
The pip package management tool&lt;br&gt;
A Google account with Gmail enabled&lt;br&gt;
I do recommend using Python 3 and pip3 to get the best and latest&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 1: Turn on the Gmail API
&lt;/h3&gt;

&lt;p&gt;Click this button to create a new Cloud Platform project and automatically enable the Gmail API&lt;br&gt;
In resulting dialog click DOWNLOAD CLIENT CONFIGURATION and save the file credentials.json to your working directory.&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 2: Install the Google Client Library
&lt;/h3&gt;

&lt;p&gt;Run the following command to install the library using pip:&lt;br&gt;
&lt;code&gt;pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib&lt;/code&gt;&lt;br&gt;
or you can run pip3 instead of pip if you’re using python3&lt;/p&gt;
&lt;h3&gt;
  
  
  Step 3: Set up the sample
&lt;/h3&gt;

&lt;p&gt;Create a new file with any name in your working directory and copy in the following code:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;from __future__ import print_function&lt;br&gt;
import pickle&lt;br&gt;
import os.path&lt;br&gt;
from googleapiclient.discovery import build&lt;br&gt;
from google_auth_oauthlib.flow import InstalledAppFlow&lt;br&gt;
from google.auth.transport.requests import Request&lt;br&gt;
SCOPES = ['https://www.googleapis.com/auth/gmail.readonly']&lt;br&gt;
def main():&lt;br&gt;
    creds = None&lt;br&gt;
    if os.path.exists('token.pickle'):&lt;br&gt;
        with open('token.pickle', 'rb') as token:&lt;br&gt;
            creds = pickle.load(token)&lt;br&gt;
    # If there are no (valid) credentials available, let the user log in.&lt;br&gt;
    if not creds or not creds.valid:&lt;br&gt;
        if creds and creds.expired and creds.refresh_token:&lt;br&gt;
            creds.refresh(Request())&lt;br&gt;
        else:&lt;br&gt;
            flow = InstalledAppFlow.from_client_secrets_file(&lt;br&gt;
                'credentials.json', SCOPES)&lt;br&gt;
            creds = flow.run_local_server(port=0)&lt;br&gt;
        with open('token.pickle', 'wb') as token:&lt;br&gt;
            pickle.dump(creds, token)&lt;br&gt;
    service = build('gmail', 'v1', credentials=creds)&lt;br&gt;
    results = service.users().labels().list(userId='me').execute()&lt;br&gt;
    labels = results.get('labels', [])&lt;br&gt;
    if not labels:&lt;br&gt;
        print('No labels found.')&lt;br&gt;
    else:&lt;br&gt;
        print('Labels:')&lt;br&gt;
        for label in labels:&lt;br&gt;
            print(label['name'])&lt;br&gt;
        # Call the Gmail API to fetch INBOX&lt;br&gt;
        results = service.users().messages().list(userId='me', labelIds=['INBOX']).execute()&lt;br&gt;
        messages = results.get('messages', [])&lt;br&gt;
        for message in messages:&lt;br&gt;
            msg = service.users().messages().get(userId='me', id=message['id']).execute()&lt;br&gt;
            print(msg['snippet'])&lt;br&gt;
if __name__ == '__main__':&lt;br&gt;
    main()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Be aware that by default messages().list returns only the first page (within UI) of your emails. In my case, Gmail is set to show 100 emails per page, so it returns 100 latest emails. If you want to display more, just add maxResults=number after labelIds.&lt;/p&gt;

&lt;h3&gt;
  
  
  Sources
&lt;/h3&gt;

&lt;p&gt;Gmail API Python Quickstart — (&lt;a href="https://developers.google.com/gmail/api/quickstart/python"&gt;https://developers.google.com/gmail/api/quickstart/python&lt;/a&gt;)&lt;/p&gt;

</description>
      <category>expense</category>
      <category>tracker</category>
      <category>python</category>
    </item>
  </channel>
</rss>
