'Authentication failed pysftp with private key

Using the Pysftp library to sftp to a server, I am getting Authentication failed when attempting to connect.

However using the same credentials in filezilla I am able to connect.

from pysftp import CnOpts, Connection
from paramiko import RSAKey
import io

private_key_text = '''
-----BEGIN OPENSSH PRIVATE KEY-----
   key removed...
-----END OPENSSH PRIVATE KEY-----
'''

    passphrase = 'my_passphrase'
    private_key = RSAKey.from_private_key(io.StringIO(private_key_text), password=passphrase)
    host = 'host_address'
    username = 'username'
    cnopts = CnOpts()
    target_dir = '/home'
    cnopts.hostkeys = None
    with Connection(host=host, username=username, private_key=private_key, private_key_pass=passphrase,
                    cnopts=cnopts, default_path=target_dir) as sftp:
        print(f'Successfully connected to remote SFTP server [{host}]')
    


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source