使用Soup进行基本身份验证的web服务 [英] Consume a webservice with basic authentication using Soup

查看:129
本文介绍了使用Soup进行基本身份验证的web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为gnome-shell扩展的一部分,我尝试使用xmlrpc来使用webservice。 webservice需要一个基本的身份验证头。使用Soup,我得到了下面的代码(基本上来自伟大的openweather扩展的蓝图):
$ b $ pre $ function load_json_async(){

if(_httpSession === undefined){
_httpSession = new Soup.Session();
} else {
//中止以前的请求。
_httpSession.abort();
}

let message = Soup.xmlrpc_message_new(
https://api.sipgate.net/RPC2,
samurai.BalanceGet,
new GLib.Variant('()',1.0)


_httpSession.connect('authenticate',
Lang.bind(
this,function (session,message,auth,retryFlag){
auth.authenticate(xxxxx,xxxxx);
}



_httpSession.queue_message(
message,
Lang.bind(this,
function(_httpSession,message){
try {
if(!message.response_body.data) {
log(hello1+ message.response_body.status)
return;
} else {
log(got message-status:+ message.status_code)

log(message.response_body.data)
} catch(e){
log(exception:+ e)
return;
}
return;
}));
return;
}

我使用Soup来建立连接。认证信号在执行队列回调之前执行。

尽管如此,在回调开始时,response_body保留了状态码401而不是预期的授权。给出的凭据不正确。纠正后,通话结束。但是,您总是需要以这种方式对供应商进行两次调用:首先获取使用BasicAuth的信息,然后再调用第二次调用。



有没有办法直接向第一次调用提供认证信息?

解决方案

可以将授权直接添加到请求头中

  let auth = new Soup.AuthBasic()
auth.authenticate(xxx,xxx);
message.request_headers.append(Authorization,auth.get_authorization(message))

这可以防止没有auth头的第一个请求,并且还允许使用REST服务,这些服务不会在未授权的请求上返回正确的状态码,而是转发到登录页面。


As part of a gnome-shell extension, I try to consume a webservice using xmlrpc. The webservice expects a basic authentication header. Using Soup, I got the following code (basically a blueprint from the great openweather extension):

function load_json_async() {

    if (_httpSession === undefined) {
       _httpSession = new Soup.Session();
    } else {
        // abort previous requests.
        _httpSession.abort();
    }

    let message = Soup.xmlrpc_message_new (
         "https://api.sipgate.net/RPC2", 
         "samurai.BalanceGet", 
         new GLib.Variant('()',1.0)
     )

    _httpSession.connect('authenticate', 
       Lang.bind(
         this,function(session,message, auth,retryFlag){
           auth.authenticate("xxxxx","xxxxx");
         }
       )
     )

    _httpSession.queue_message(
       message, 
       Lang.bind(this, 
           function(_httpSession, message) {
            try {
              if (!message.response_body.data) {
                log("hello1 "+message.response_body.status)
                return;
              } else {
                log("got message-status:"+message.status_code)
              }
              log(message.response_body.data)
            } catch (e) {
              log("exception:"+e)                
              return;
            }
       return;
    }));
    return;
}

I am using Soup for building up the connection. The authenticate signal is executed before the queue-callback is executed.

Still, in the beginning within the callback, the response_body holded the status code 401 instead of the expected authorization. The given credentials where incorrect.After correcting this, the call went through. However, you always need two calls to the provider this way: first to get the information it uses BasicAuth, and the second to actually do the call.

Is there a way to give the authentication information directly with the first call?

解决方案

It's possible to add the authorization directly into the request header

let auth = new Soup.AuthBasic()
auth.authenticate("xxx","xxx");
message.request_headers.append("Authorization",auth.get_authorization(message))

This prevents a first request without auth header and also allows use of REST services that don't return the correct status code on unauthorized requests and forward to a login page instead.

这篇关于使用Soup进行基本身份验证的web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆