GithubHelp home page GithubHelp logo

yoda-yoda / gmail-api-wrapper Goto Github PK

View Code? Open in Web Editor NEW
26.0 4.0 2.0 54 KB

Gmail API Wrapper - A Python Client

Home Page: https://pypi.python.org/pypi/gmail-api-wrapper/

License: Other

Python 97.94% Makefile 2.06%
gmail-api gmail-api-wrapper gmail-api-python-client gmail gmail-api-python

gmail-api-wrapper's Introduction

Gmail API Wrapper For Python

This is a wrapper for the google-api-python-client library by Google.

Behind the scenes ----------------

Installation

pip install --upgrade gmail-api-wrapper

Setup

gmail-api-wrapper uses OAuth authentication

Generate a client ID and client secret Here under section Step 1: Turn on the Gmail API. Follow steps a to h.

After you have downloaded the client_secret.json file, move it to the ~/.credentials/ directory in your home directory.

Env Variables

Set the following env variables.

# Space delimited string
export GAW_SCOPES="'gmail-scopes-1 'gmail-scope-2'"

# client_secret json file path

# If you're using custom SSL certs, set GAW_CA_CERTS_PEM_FILE env variable
export GAW_CA_CERTS_PEM_FILE=<path-to-custom-pem-ssl-cert>

# The user Id to use. Default to 'me'. A special identifier for gmail
# that refers to the email address used to do OAuth2 handshake
export GAW_USER_ID=<[email protected]>

# Your preferred application name. Defaults to 'Gmail API Wrapper'
export GAW_APPLICATION_NAME=<your-preferred-application-name>

# Disable ssl certs validation. Defaults to False
export GAW_DISABLE_SSL_CERTS=True

Usage - READ

from gmail_api_wrapper.crud.read import GmailAPIReadWrapper


gmail_api = GmailAPIReadWrapper()


# Check unread messages. Returns a list of dicts in the below format
gmail_api.check_new_mail()

>>> [

        {
            'subject': 'Sample Subject',
            'base64_msg_body': 'base64string',
            'from:' 'exapmle@mail_server.com'
            'date': '2017-09-16T10:57:12.4323'
        },
    ]

# Check new mail from specific sender. Returns a list of dicts above
gmail_api.check_new_mail(sender='example@mail_server.com')



# Alternatively, you get all unread messages from a specific sender
gmail_api.get_unread_messages(sender='example@mail_server.com')

>>> [

        {
            'subject': 'Sample Subject',
            'base64_msg_body': 'base64string',
            'from:' 'exapmle@mail_server.com'
            'date': '2017-09-16T10:57:12.4323'
        },
    ]




# Get all labels present. Returns a list of strings
gmail_api.get_labels()

>>> ['INBOX', 'UNREAD', 'SPAM', 'DRAFTS']



# Get total message count. Returns a formatted json object
gmail_api.get_total_messages()

>>> {
        'Total Messages': 2017,
        'Total Threads': 123,
        'Email Address': 'example@mail_server.com'
    }


# Get a list of messages. Defaults to INBOX if no label is specified
gmail_api.list_messages()

>>> [

        {
            'subject': 'Sample Subject',
            'base64_msg_body': 'base64string',
            'from:' 'exapmle@mail_server.com'
            'date': '2017-09-16T10:57:12.4323'
        },
    ]


# Get a list of messages in DRAFTS and SPAM
gmail_api.list_messages(labels=['DRAFTS', 'SPAM'])

>>> [

        {
            'subject': 'Sample Subject',
            'base64_msg_body': 'base64string',
            'from:' 'exapmle@mail_server.com'
            'date': '2017-09-16T10:57:12.4323'
        },
    ]


# Get a specific message. `message_id` passed must be a google message id object
gmail_api.get_message('message_id')

Usage - WRITE

from gmail_api_wrapper.crud.write import GmailAPIWriteWrapper

api = GmailAPIWriteWrapper()


# compose new mail
api.compose_mail(subject='API Wrapper', body='Py client', to='email1,email2')

>>> {
        'id': 'blah-blah',
        'labelsIds': ['INBOX', 'SENT', 'UNREAD'],
        'threadId': 'blah-blah'
    }

# compose new mail with cc and bcc
api.compose_mail(subject='API Wrapper', body='Py client', to='email1,email2', cc='email1,email2', bcc='email1,email2')

>>> {
        'id': 'blah-blah',
        'labelsIds': ['INBOX', 'SENT', 'UNREAD'],
        'threadId': 'blah-blah'
    }

# create new label
api.create_label(name='Integrations')

>>> {
        'id': 'blah-blah',
        'labelListVisibility': 'labelShow',
        'messageListVisibility': 'show',
        'name': 'Integrations'
    }

Authors

gmail-api-wrapper's People

Contributors

dkaranja avatar qqgg231 avatar yoda-yoda avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

gmail-api-wrapper's Issues

Cannot Read Credential Files

Hi, I followed the instruction downloading credential file and rename it as "client_secret.json" before putting it into ~/.credentials/ directory. However, Still met a problem when initialize a object from GmailAPIReadWrapper(). Any idea to solve the problem? Thanks.
(Ubuntu 16.0.4)

Python 3.7.0 (default, Jun 28 2018, 13:15:42)
[GCC 7.2.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.

from gmail_api_wrapper.crud.read import GmailAPIReadWrapper

gmail_api = GmailAPIReadWrapper()
Traceback (most recent call last):
File "", line 1, in
File "/home/Tai-chunWei/workspace/gmail-api-wrapper/gmail_api_wrapper/crud/read.py", line 75, in init
self.gmail_api = GmailAPIConnection().gmail_api_connect()
File "/home/Tai-chunWei/workspace/gmail-api-wrapper/gmail_api_wrapper/connection.py", line 115, in gmail_api_connect
service = google_api.get_service()
File "/home/Tai-chunWei/workspace/gmail-api-wrapper/gmail_api_wrapper/connection.py", line 97, in get_service
creds = self._get_credentials()
File "/home/Tai-chunWei/workspace/gmail-api-wrapper/gmail_api_wrapper/connection.py", line 83, in _get_credentials
credentials = store.get()
File "/home/Tai-chunWei/anaconda3/lib/python3.7/site-packages/oauth2client/client.py", line 407, in get
return self.locked_get()
File "/home/Tai-chunWei/anaconda3/lib/python3.7/site-packages/oauth2client/file.py", line 54, in locked_get
credentials = client.Credentials.new_from_json(content)
File "/home/Tai-chunWei/anaconda3/lib/python3.7/site-packages/oauth2client/client.py", line 302, in new_from_json
module_name = data['_module']
KeyError: '_module'

AttributeError: MIMEText instance has no attribute 'as_bytes'

environment:
Linux instance-1 4.9.0-6-amd64 #1 SMP Debian 4.9.88-1+deb9u1 (2018-05-07) x86_64 GNU/Linux
pip:
gmail-api-wrapper==0.1.1a2

code:

# -*- coding: utf8 -*-
from gmail_api_wrapper.crud.write import GmailAPIWriteWrapper

api = GmailAPIWriteWrapper()
# compose new mail
import datetime
nowstr = datetime.datetime.strftime(datetime.datetime.now(), "%Y-%m-%d %H%M%S")
subject = "test"
body = nowstr
print api.compose_mail(subject=subject, body=body, to='[email protected]')

error:


Traceback (most recent call last):
  File "sendmail.py", line 10, in <module>
    print api.compose_mail(subject=subject, body=body, to='[email protected]')
  File "/xxxxx/.local/lib/python2.7/site-packages/gmail_api_wrapper/crud/write.py", line 92, in compose_mail
    b64_raw = base64.urlsafe_b64encode(message.as_bytes())
AttributeError: MIMEText instance has no attribute 'as_bytes'

I tried the gmail API authority success, but gmail_api.check_new_mail() failed

I did the second test on the read, error:
C:\Anaconda3\python.exe C:/Users/liumin/PycharmProjects/StockA/GmailWrapperRead.py
C:\Anaconda3\lib\site-packages\oauth2client_helpers.py:255: UserWarning: Cannot access C:\Users\name.credentials\gmail-api-wrapper-py.json: No such file or directory
warnings.warn(_MISSING_FILE_MESSAGE.format(filename))

It did open a browser, but still gave me an final error after the above error:
==noauth_local_webserver (it popped up an login page, I login, webpage showed authorization success)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.