2023-10-01 15:37:20 +02:00
|
|
|
from typing import Collection, Optional
|
|
|
|
|
2018-01-17 03:16:59 +01:00
|
|
|
from platypush.plugins import Plugin
|
|
|
|
|
|
|
|
|
|
|
|
class GooglePlugin(Plugin):
|
|
|
|
"""
|
2023-10-01 15:37:20 +02:00
|
|
|
Integrates with the Google APIs using the google-api-python-client.
|
|
|
|
|
2018-06-23 01:00:43 +02:00
|
|
|
This class is extended by ``GoogleMailPlugin``, ``GoogleCalendarPlugin`` etc.
|
2023-10-01 15:37:20 +02:00
|
|
|
|
2018-01-17 03:16:59 +01:00
|
|
|
In order to use Google services (like GMail, Maps, Calendar etc.) with
|
|
|
|
your account you need to:
|
|
|
|
|
|
|
|
1. Create your Google application, if you don't have one already, on
|
2023-10-01 15:37:20 +02:00
|
|
|
the `developers console <https://console.developers.google.com>`_.
|
|
|
|
|
|
|
|
2. You may have to explicitly enable your user to use the app if the app
|
|
|
|
is created in test mode. Go to "OAuth consent screen" and add your user's
|
|
|
|
email address to the list of authorized users.
|
2018-01-17 03:16:59 +01:00
|
|
|
|
2023-10-01 15:37:20 +02:00
|
|
|
3. Select the scopes that you want to enable for your application, depending
|
|
|
|
on the integrations that you want to use.
|
|
|
|
See https://developers.google.com/identity/protocols/oauth2/scopes
|
|
|
|
for a list of the available scopes.
|
2018-01-17 03:16:59 +01:00
|
|
|
|
2023-10-01 15:37:20 +02:00
|
|
|
4. Click on "Credentials", then "Create credentials" -> "OAuth client ID".
|
2018-01-17 03:16:59 +01:00
|
|
|
|
2023-10-01 15:37:20 +02:00
|
|
|
5 Select "Desktop app", enter whichever name you like, and click "Create".
|
2018-01-17 03:16:59 +01:00
|
|
|
|
2023-10-01 15:37:20 +02:00
|
|
|
6. Click on the "Download JSON" icon next to your newly created client ID.
|
2018-06-23 01:00:43 +02:00
|
|
|
|
2023-10-01 15:37:20 +02:00
|
|
|
7. Generate a credentials file for the required scope:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
mkdir -p <WORKDIR>/credentials/google
|
|
|
|
python -m platypush.plugins.google.credentials \
|
|
|
|
'calendar.readonly' \
|
|
|
|
<WORKDIR>/credentials/google/client_secret.json
|
2018-06-23 01:00:43 +02:00
|
|
|
|
2018-01-17 03:16:59 +01:00
|
|
|
"""
|
|
|
|
|
2023-10-01 15:37:20 +02:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
scopes: Optional[Collection[str]] = None,
|
|
|
|
secrets_path: Optional[str] = None,
|
|
|
|
**kwargs
|
|
|
|
):
|
2018-06-23 01:00:43 +02:00
|
|
|
"""
|
2023-10-01 15:37:20 +02:00
|
|
|
:param scopes: List of scopes required by the API.
|
|
|
|
See https://developers.google.com/identity/protocols/oauth2/scopes
|
|
|
|
for a list of the available scopes. Override it in your configuration
|
|
|
|
only if you need specific scopes that aren't normally required by the
|
|
|
|
plugin.
|
|
|
|
:param secrets_path: Path to the client secrets file.
|
|
|
|
You can create your secrets.json from https://console.developers.google.com.
|
|
|
|
Default: ``<PLATYPUSH_WORKDIR>/credentials/google/client_secret.json``.
|
2018-06-23 01:00:43 +02:00
|
|
|
"""
|
|
|
|
|
2023-10-01 15:37:20 +02:00
|
|
|
from platypush.plugins.google.credentials import (
|
|
|
|
get_credentials,
|
|
|
|
default_secrets_file,
|
|
|
|
)
|
2023-07-22 23:02:44 +02:00
|
|
|
|
2020-07-02 02:07:57 +02:00
|
|
|
super().__init__(**kwargs)
|
2019-03-14 01:12:39 +01:00
|
|
|
self._scopes = scopes or []
|
2023-10-01 15:37:20 +02:00
|
|
|
self._secrets_path: str = secrets_path or default_secrets_file
|
2018-01-17 03:16:59 +01:00
|
|
|
|
2019-03-18 01:06:10 +01:00
|
|
|
if self._scopes:
|
2023-10-01 15:37:20 +02:00
|
|
|
scopes = " ".join(sorted(self._scopes))
|
|
|
|
try:
|
|
|
|
self.credentials = {
|
|
|
|
scopes: get_credentials(scopes, secrets_file=self._secrets_path)
|
|
|
|
}
|
|
|
|
except AssertionError as e:
|
|
|
|
self.logger.warning(str(e))
|
2019-03-18 01:06:10 +01:00
|
|
|
else:
|
|
|
|
self.credentials = {}
|
2018-01-17 03:16:59 +01:00
|
|
|
|
2023-10-01 15:37:20 +02:00
|
|
|
def get_service(
|
|
|
|
self, service: str, version: str, scopes: Optional[Collection[str]] = None
|
|
|
|
):
|
2019-03-14 01:12:39 +01:00
|
|
|
import httplib2
|
|
|
|
from apiclient import discovery
|
|
|
|
|
|
|
|
if scopes is None:
|
2023-10-01 15:37:20 +02:00
|
|
|
scopes = getattr(self, "scopes", [])
|
2019-03-14 01:12:39 +01:00
|
|
|
|
2023-10-01 15:37:20 +02:00
|
|
|
scopes = " ".join(sorted(scopes))
|
2019-03-14 01:12:39 +01:00
|
|
|
credentials = self.credentials[scopes]
|
|
|
|
http = credentials.authorize(httplib2.Http())
|
|
|
|
return discovery.build(service, version, http=http, cache_discovery=False)
|
2018-01-17 03:16:59 +01:00
|
|
|
|
2019-03-14 01:12:39 +01:00
|
|
|
|
|
|
|
# vim:sw=4:ts=4:et:
|