2013-03-13 66 views
0

我正在寻找一种方法以编程方式设置一个HTTP适配器的凭据? 是某人有一个例子吗?编程修改工作灯适配器凭证

是否可以修改适配器实现js来覆盖凭据?

的东西,如:

function getMyAdapters(path) { 

var tok = "myuser:mypw"; 
var hash = Base64Encoder.encode(tok); 
var headers="{'User-Agent':'Mozilla'"+"Authentication: Basic }"+hash; 
var input = { 
     method : 'get', 
     returnedContentType : 'json', 
     headers: headers, 
     path : path 
    }; 


return WL.Server.invokeHttp(input); 
} 

但失败了,因为它没有找到Base64Encoder。

任何想法? http://en.wikipedia.org/wiki/Basic_access_authentication

此外,你应该做到以下几点:

回答

0

首先,这里描述你应该使用“授权”,而不是“认证”

创建一个Java类,这样的事情(你”会需要清理和调整它当然):

public class Idan { 
     public String basicHash(String user, String password) { 
      BASE64Encoder base64Encoder = new BASE64Encoder(); 
      String authorization = user + ":" + password; 
      return base64Encoder.encode(authorization.getBytes()); 
     } 

     // to test: 
     public static void main(String[] args) { 
     Idan i = new Idan(); 
     System.out.println(i.basicHash("idan", "somepassword")); 
    } 
} 

在适配器的.js文件,在全球范围宣告:

var idan = new org.Idan(); 

而且程序:

function test(){ 
WL.Logger.debug(idan.basicHash("username_test", "secret_password")); 

}

+0

感谢您它帮助我理解我的错误;-) – ITDoVe 2013-03-14 15:24:23

+0

很高兴我能帮忙。作为回答请注明这个问题。 – 2013-03-14 16:52:02

+0

最后,我在这里http://stackoverflow.com/questions/15432608/change-http-url-in-worklight-adapter实施的方法,让我也提供REST式API的URL。 – ITDoVe 2013-03-17 13:39:11