Problem with keyring.get_credential() returning None

227 Views Asked by At

I am trying to use the keyring library from Python 3 to store and retrieve credentials for a user. The following simple example should store a password, read it back, and then read both the username and password, but get_credential() is returning None. Did I miss something?

keyring.set_password("KRDemo", "user01", "password")

pw = keyring.get_password("KRDemo", "user01")
print("Password:", pw)

cr = keyring.get_credential("KRDemo", None)
if cr is not None:
    print("Username:", cr.username)
    print("Password:", cr.password)

I'm on a Linux Mint 20.3 host with Cinnamon desktop.

1

There are 1 best solutions below

0
Prasaanth Sakthivel On

I am also having the same problem when I run this with sudo it throws None then tried without sudo

Try this below code with sudo and without sudo


import keyring

class Store:
    def __init__(self):
        self.servicename = "your_service_name"
        self.username = "your_username"

    def setkey(self, password):
        keyring.set_password(self.servicename, self.username, password)

    def getkey(self):
        return keyring.get_password(self.servicename, self.username)

    def getsess(self):
        sess = keyring.get_password(self.servicename, self.username)
        return sess

    def deletekey(self):
        if keyring.get_password(self.servicename, self.username):
            if keyring.delete_password(self.servicename, self.username):
                return True
        return False
    
ob = Store()
print(ob.getkey())
# ob.deletekey()