简体   繁体   中英

How to Store OAuth Credentials With Gnome Keyring

I'm trying to build a simple Vala/Gtk Twitter app, and I've managed to send tweets; however, the user has to authenticate every time, which entails going to a URL, clicking to give my App permission to post, copying the PIN, and pasting said PIN into my App. For each tweet.

I'd like to store this authentication information in the GNOME Keyring; however, I barely know anything about OAuth and I know nothing about the Keyring.

How can I store OAuth data into the Gnome Keyring? I'll accept answers in any language, although bonus points will be awarded for Vala answers. :)

You can use libsecret library which communicates with "Secret Service" through Dbus protocol .

First you need to define a password schema , which will be used later for token store/extraction .

Vala example :

var example_schema = new Secret.Schema ("org.yor_schema.name",Secret,SchemaFlags.NONE,
    "number", Secret.SchemaAttributeType.INTEGER,
    "string", Secret.SchemaAttributeType.STRING);

Now you should store your token :

var attributes = new GLib.HashTable<string,string> ();
attributes["number"] = "18";
attributes["string"] = "Hello";

Secret.password_storev.begin(example_schema,attributes,Secret.COLLECTION_DFAULT,
    "Label","Token",null,(obj,async_res) => {
        bool res = Secret.password_store.end(async_res);
        /* Password has been stored - do something ... */
});

To extract stored token :

var attributes = new GLib.HashTable<string,string> ();
attributes["number"] = "18";
attributes["string"] = "Hello";

Secret.password_lookupv.begin(example_schema,attributes,null,(obj,async_res) => {
    String token = Secret.password_lookup.end(async_res);
});

The package name called libsecret-1 .

To compile , add following flag to your makefile .

AM_VALAFLAGS = \
    --pkg=libsecret-1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM