ReloadOrRestartUnit

Given that I just spent two hours to write three lines of code, I share here my initial problem and the final solution for those looking for the same thing in the future (including myself).

The problem is: in a graphical Python application running as a normal user, obtain authorization to restart a system service. The quick and dirty solution involves the execution of a command starting with gksu, but this method has been discouraged in favor of PolicyKit.

So I started looking for PolicyKit documentation. Which lacks any viable examples. And, as now discovered after a long journey across multiple sources and failed tries, pretty useless.

To restart a service, just ask systemd via DBus (using GDBus). Caring to explicit that the method requires authorization. GDBus himself will provide to raise an authorization dialog for the user, unlock the unlockable, probably contact PolicyKit, and whatever it happens behind the scenes.

from gi.repository import GLib, Gio

bus = Gio.bus_get_sync(Gio.BusType.SYSTEM, None)  
bus.call_sync('org.freedesktop.systemd1',  
              '/org/freedesktop/systemd1',
              'org.freedesktop.systemd1.Manager',
              'ReloadOrRestartUnit',
              GLib.Variant('(ss)', ('apache2.service', 'replace')),
              None,
              Gio.DBusCallFlags.ALLOW_INTERACTIVE_AUTHORIZATION,
              -1,
              None)