简体   繁体   English

如何通过Gjs使用libsoup进行Basic Auth

[英]How to use Basic Auth with libsoup via Gjs

I'm trying to query github's api with a token. 我正在尝试用令牌查询github的api。 Github's api accepts generated tokens provided that they're sent as a basic auth header. Github的api接受生成的令牌,前提是它们作为基本的auth头发送。

The API do not return HTTP 401 if the call is made without auth which means if one wants to query their api using Basic Auth, one must fill the header pre-emptively rather than doing a round trip. 如果在没有auth的情况下进行调用,则API不返回HTTP 401,这意味着如果想要使用Basic Auth查询其api,则必须先抢先填写标题而不是进行往返。

I'm now trying to query the API using libsoup and Gjs. 我现在正在尝试使用libsoup和Gjs查询API。

I have noticed the SoupAuthManager has a function that seems to match perfectly what I need (soup_auth_manager_use_auth here ) but can't find a way to invoke it. 我注意到SoupAuthManager有一个看起来完全符合我需要的功能( 这里是 soup_auth_manager_use_auth)但找不到调用它的方法。

This can be used to "preload" manager 's auth cache, to avoid an extra HTTP round trip in the case where you know ahead of time that a 401 response will be returned 这可用于“预加载”管理器的auth缓存,以避免在提前知道将返回401响应的情况下额外的HTTP往返

This is what I currently use, but it does not work as the SoupAuthManager is a private object of the session; 这是我目前使用的,但它不起作用,因为SoupAuthManager是会话的私有对象; and has therefore no effect on the actual behaviour of the program 因此对程序的实际行为没有影响

let httpSession = new Soup.Session();
let authUri = new Soup.URI(url);
authUri.set_user(this.handle);
authUri.set_password(this.token);
let message = new Soup.Message({method: 'GET', uri: authUri});

let authManager = new Soup.AuthManager();
let auth = new Soup.AuthBasic({host: 'api.github.com', realm: 'Github Api'});

authManager.use_auth(authUri, auth);
httpSession.queue_message(message, ...);

Are there other methods I could use to force Basic Auth on the first roud trip? 还有其他方法可以用来在第一次旅行中强制使用Basic Auth吗? or are there other library I could use from gjs to call github's API and force the basic auth? 或者是否有其他库我可以使用gjs调用github的API并强制基本身份验证?

I found the solution. 我找到了解决方案。 To link the authorisation to the session, one can use the add_feature function. 要将授权链接到会话,可以使用add_feature函数。 Now this is defined here but it turns out calling it directly doesn't work 现在这是在这里定义的但事实证明直接调用它是行不通的

this._httpSession.add_feature(authManager)

instead it seems to work if called like that: 相反,如果这样调用它似乎工作:

Soup.Session.prototype.add_feature.call(httpSession, authManager);

finally, the github api rejects any call without a user agent, so I added the following: 最后,github api在没有用户代理的情况下拒绝任何调用,所以我添加了以下内容:

httpSession.user_agent = 'blah'

the final code looks like: 最终的代码如下:

let httpSession = new Soup.Session();
httpSession.user_agent = 'blah'
let authUri = new Soup.URI(url);
authUri.set_user(this.handle);
authUri.set_password(this.token);
let message = new Soup.Message({method: 'GET', uri: authUri});

let authManager = new Soup.AuthManager();
let auth = new Soup.AuthBasic({host: 'api.github.com', realm: 'Github Api'});

Soup.Session.prototype.add_feature.call(httpSession, authManager);
httpSession.queue_message(message, function() {...});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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