如何使用Gnome Keyring存储OAuth凭据

web*_*rc2 2 oauth vala gnome-keyring-daemon

我正在尝试构建一个简单的Vala/Gtk Twitter应用程序,我已设法发送推文; 但是,用户必须每次都进行身份验证,这需要转到URL,单击以授予我的App发布权限,复制PIN以及将所述PIN粘贴到我的应用程序中.每条推文.

我想将此身份验证信息存储在GNOME Keyring中; 但是,我对OAuth几乎一无所知,而且我对Keyring一无所知.

如何将OAuth数据存储到Gnome Keyring中?我将接受任何语言的答案,但Vala答案将获得奖励积分.:)

Ale*_*der 5

您可以使用通过Dbus协议与"特勤服务"通信的libsecret库.

首先,您需要定义一个密码模式,稍后将用于令牌存储/提取.

Vala示例:

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

现在你应该存储你的令牌:

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 ... */
});
Run Code Online (Sandbox Code Playgroud)

要提取存储的令牌:

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);
});
Run Code Online (Sandbox Code Playgroud)

包名称调用libsecret-1.

要编译,请在makefile中添加以下标志.

AM_VALAFLAGS = \
    --pkg=libsecret-1
Run Code Online (Sandbox Code Playgroud)