def write_credentials(self, credentials=None, profile=None,
                          cache_token=None):
        """Write credentials.

        Write credentials to credentials file. Performs ``upsert``.

        Args:
            cache_token (bool): If ``True``, stores ``access_token`` in token store. Defaults to ``True``.
            credentials (class): Read-only credentials.
            profile (str): Credentials profile. Defaults to ``'default'``.

        Returns:
            int: Affected document ID.

        """
        d = {
            'profile': profile,
            'client_id': credentials.client_id,
            'client_secret': credentials.client_secret,
            'refresh_token': credentials.refresh_token
        }
        if cache_token:
            d.update({'access_token': credentials.access_token})
        with self.lock:
            return self.db.upsert(
                d, self.query.profile == profile
            )
Write credentials.

        Write credentials to credentials file. Performs ``upsert``.

        Args:
            cache_token (bool): If ``True``, stores ``access_token`` in token store. Defaults to ``True``.
            credentials (class): Read-only credentials.
            profile (str): Credentials profile. Defaults to ``'default'``.

        Returns:
            int: Affected document ID.