google.drive#

Description#

Google Drive plugin.

  1. Create your Google application, if you don’t have one already, on the developers console.

  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.

  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.

  4. Click on “Credentials”, then “Create credentials” -> “OAuth client ID”.

  5. Select “Desktop app”, enter whichever name you like, and click “Create”.

  6. Click on the “Download JSON” icon next to your newly created client ID. Save the JSON file under <WORKDIR>/credentials/google/client_secret.json.

  7. If you’re running the service on a desktop environment, then you can just start the application. A browser window will open and you’ll be asked to authorize the application - you may be prompted with a warning because you are running a personal and potentially unverified application. After authorizing the application, the process will save the credentials under <WORKDIR>/credentials/google/<list,of,scopes>.json and proceed with the plugin initialization.

  8. If you’re running the service on a headless environment, or you prefer to manually generate the credentials file before copying to another machine, you can run the following command:

    mkdir -p <WORKDIR>/credentials/google
    python -m platypush.plugins.google.credentials \
        'drive,drive.appfolder,drive.photos.readonly' \
        <WORKDIR>/credentials/google/client_secret.json [--noauth_local_webserver]
    

    When launched with --noauth_local_webserver, the script will start a local webserver and print a URL that should be opened in your browser. After authorizing the application, you may be prompted with a code that you should copy and paste back to the script. Otherwise, if you’re running the script on a desktop, a browser window will be opened automatically.

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.

Configuration#

google.drive:
  # [Optional]
  # 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.
  # scopes:   # type=Optional[Collection[str]]

  # [Optional]
  # 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``.
  # secrets_path:   # type=Optional[str]

Dependencies#

pip

pip install oauth2client httplib2 google-auth google-api-python-client

Alpine

apk add py3-google-auth py3-httplib2 py3-oauth2client py3-google-api-python-client

Debian

apt install python3-httplib2 python3-oauth2client python3-google-auth

Fedora

yum install python-google-auth python-oauth2client python-httplib2 python-google-api-client

Arch Linux

pacman -S python-google-api-python-client python-oauth2client python-google-auth python-httplib2

Actions#

Module reference#

class platypush.plugins.google.drive.GoogleDrivePlugin(**kwargs)[source]#

Bases: GooglePlugin

Google Drive plugin.

  1. Create your Google application, if you don’t have one already, on the developers console.

  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.

  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.

  4. Click on “Credentials”, then “Create credentials” -> “OAuth client ID”.

  5. Select “Desktop app”, enter whichever name you like, and click “Create”.

  6. Click on the “Download JSON” icon next to your newly created client ID. Save the JSON file under <WORKDIR>/credentials/google/client_secret.json.

  7. If you’re running the service on a desktop environment, then you can just start the application. A browser window will open and you’ll be asked to authorize the application - you may be prompted with a warning because you are running a personal and potentially unverified application. After authorizing the application, the process will save the credentials under <WORKDIR>/credentials/google/<list,of,scopes>.json and proceed with the plugin initialization.

  8. If you’re running the service on a headless environment, or you prefer to manually generate the credentials file before copying to another machine, you can run the following command:

    mkdir -p <WORKDIR>/credentials/google
    python -m platypush.plugins.google.credentials \
        'drive,drive.appfolder,drive.photos.readonly' \
        <WORKDIR>/credentials/google/client_secret.json [--noauth_local_webserver]
    

    When launched with --noauth_local_webserver, the script will start a local webserver and print a URL that should be opened in your browser. After authorizing the application, you may be prompted with a code that you should copy and paste back to the script. Otherwise, if you’re running the script on a desktop, a browser window will be opened automatically.

__init__(**kwargs)[source]#
Parameters:
copy(file_id: str) dict[source]#

Create a copy of a file.

Parameters:

file_id – File ID.

Returns:

The copied file metadata.

{
  "id": "File ID",
  "mime_type": "plain/text",
  "name": "File name",
  "type": "file"
}

create(name: str, description: str | None = None, mime_type: str | None = None, parents: List[str] | None = None, starred: bool = False) dict[source]#

Create a file.

Parameters:
  • name – File name.

  • description – File description.

  • mime_type – File MIME type.

  • parents – List of folder IDs that will contain the file (default: drive root).

  • starred – If True then the file will be marked as starred.

Returns:

The created file metadata.

{
  "id": "File ID",
  "mime_type": "plain/text",
  "name": "File name",
  "type": "file"
}

delete(file_id: str)[source]#

Delete a file from Google Drive.

Parameters:

file_id – File ID.

download(file_id: str, path: str) str[source]#

Download a Google Drive file locally.

Parameters:
  • file_id – Path of the file to upload.

  • path – Path of the file to upload.

Returns:

The local file path.

empty_trash()[source]#

Empty the Drive bin.

files(filter: str | None = None, folder_id: str | None = None, limit: int | None = 100, drive_id: str | None = None, spaces: str | List[str] | None = None, order_by: str | List[str] | None = None) List[dict][source]#

Get the list of files.

Parameters:
  • filter – Optional filter (default: None). See Google Drive API docs for the supported syntax.

  • folder_id – Drive folder ID to search (default: get all files).

  • limit – Maximum number of entries to be retrieves (default: 100).

  • drive_id – Shared drive ID to search (default: None).

  • spaces

    Drive spaces to search. Supported values:

    • drive

    • appDataFolder

    • photos

  • order_by

    Order the results by a specific attribute or list of attributes (default: None). Supported attributes:

    • createdTime

    • folder

    • modifiedByMeTime

    • modifiedTime

    • name

    • name_natural

    • quotaBytesUsed

    • recency

    • sharedWithMeTime

    • starred

    • viewedByMeTime

    Attributes will be sorted in ascending order by default. You can change that by by appending “desc” separated by a space to the attribute you want in descending order - e.g. ["folder", "createdTime desc", "modifiedTime desc"].

Returns:

[
  {
    "id": "File ID",
    "mime_type": "plain/text",
    "name": "File name",
    "type": "file"
  }
]

get(file_id: str)[source]#

Get the information of a file on the Drive by file ID.

Parameters:

file_id – File ID.

Returns:

{
  "id": "File ID",
  "mime_type": "plain/text",
  "name": "File name",
  "type": "file"
}

update(file_id: str, name: str | None = None, description: str | None = None, add_parents: List[str] | None = None, remove_parents: List[str] | None = None, mime_type: str | None = None, starred: bool | None = None, trashed: bool | None = None) dict[source]#

Update the metadata or the content of a file.

Parameters:
  • file_id – File ID.

  • name – Set the file name.

  • description – Set the file description.

  • add_parents – Add the file to these parent folder IDs.

  • remove_parents – Remove the file from these parent folder IDs.

  • mime_type – Set the file MIME type.

  • starred – Change the starred flag.

  • trashed – Move/remove from trash.

Returns:

The updated file metadata.

{
  "id": "File ID",
  "mime_type": "plain/text",
  "name": "File name",
  "type": "file"
}

upload(path: str, mime_type: str | None = None, name: str | None = None, description: str | None = None, parents: List[str] | None = None, starred: bool = False, target_mime_type: str | None = None) dict[source]#

Upload a file to Google Drive.

Parameters:
  • path – Path of the file to upload.

  • mime_type – MIME type of the source file (e.g. “image/jpeg”).

  • name – Name of the target file. Default: same name as the source file.

  • description – File description.

  • parents – List of folder IDs that will contain the file (default: drive root).

  • starred – If True, then the uploaded file will be marked as starred by the user.

  • target_mime_type – Target MIME type. Useful if you want to e.g. import a CSV file as a Google Sheet (use “application/vnd.google-apps.spreadsheet), or an ODT file to a Google Doc (use “application/vnd.google-apps.document). See the official documentation for a complete list of supported types.

Returns:

The uploaded file metadata.

{
  "id": "File ID",
  "mime_type": "plain/text",
  "name": "File name",
  "type": "file"
}