2013-03-13 65 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