2012-07-23 109 views
8

我正在玩IBM worklight,並試圖創建一個適配器來從Google places API提供一些數據。如何使用Worklight使用serverside javascript製作HTTPS請求?

我想打電話給這個網址:

https://maps.googleapis.com/maps/api/place/search/json?key=AIzaSyCTlPms1pvhzeoRrBao5qW-DJMI_CWcbAM&location=52.0700,1.1400&radius=10000&sensor=false&name=coffee 

執行此網址在瀏覽器中正常工作,並顯示我想通過工作燈來獲得一些不錯的JSON。

工作燈適配器在Javascript中創建的,這是我到目前爲止有:

function getCoffeeHouses() { 

    var input = { 
     method : 'get', 
     returnedContentType : 'json', 
     path : 'maps/api/place/search/json', 
     parameters : { 
      'key'  : 'AIzaSyCTlPms1pvhzeoRrBao5qW-DJMI_CWcbAM', 
      'location' : '52.0700,1.1400', 
      'radius' : '10000', 
      'sensor' : 'false', 
      'name'  : 'coffee' 
     } 
    }; 

    var response = WL.Server.invokeHttp(input); 

// Extract latitude and longitude from the response. 
    var type = typeof response; 
    if ("object" == type) { 
     if (true == response["isSuccessful"]) { 
      // Return JSON object with lat and lng. 
      return response["results"]; 
     } 
     else { 
      // Returning null. Web request was not successful. 
      return null; 
     } 
    } 
    else { 
     // Returning null. Response is not an object. 
     return null; 
    } 
} 

這是我在控制檯得到的結果,當我測試上面:

Failed to parse JSON string 
<!DOCTYPE html> 
<html lang=en> 
    <meta charset=utf-8> 
    <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width"> 
    <title>Error 404 (Not Found)!!1</title> 
    <style> 
    *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}} 
    </style> 
    <a href=//www.google.com/><img src=//www.google.com/images/errors/logo_sm.gif alt=Google></a> 
    <p><b>404.</b> <ins>That’s an error.</ins> 
    <p>The requested URL <code>/maps/api/place/search/json?key=AIzaSyCTlPms1pvhzeoRrBao5qW-DJMI_CWcbAM&amp;location=52.0700%2C1.1400&amp;radius=10000&amp;sensor=false&amp;name=coffee</code> was not found on this server. <ins>That’s all we know.</ins> 
Caused by: java.io.IOException: Unexpected character '<' on line 1, column 1 
[2012-07-23 11:08:57] An error occurred while invoking procedure CoffeeFinder/getCoffeeHouses parameters: { 
    "arr": [ 
    ] 
} 
null 
Caused by: null 

我想,這可能是因爲適配器正在請求HTTP,而它應該使用HTTPS。

如果我改變在瀏覽器中使用HTTP的請求,它會顯示類似的結果。

問題:我可以通過更改上面的Javascript來製作HTTPS請求,還是我誤解了worklight適配器?

回答

7

看起來像googleapis不會工作,如果你沒有在你的請求中指定主機頭。 將它添加後一切正常,因爲它應該:

這是適配器的XML部分

<connectivity> 
    <connectionPolicy xsi:type="http:HTTPConnectionPolicyType"> 
     <protocol>https</protocol> 
     <domain>maps.googleapis.com</domain> 
     <port>443</port>    
    </connectionPolicy> 
    <loadConstraints maxConcurrentConnectionsPerNode="2" /> 
</connectivity> 

這是適配器的JS:

function doGet() { 

var input = { 
    method : 'get', 
    returnedContentType : 'json', 
    path : 'maps/api/place/search/json', 
    headers: { 
     Host: 'maps.googleapis.com' 
    }, 
    parameters : { 
     'key'  : 'AIzaSyCTlPms1pvhzeoRrBao5qW-DJMI_CWcbAM', 
     'location' : '52.0700,1.1400', 
     'radius' : '10000', 
     'sensor' : 'false', 
     'name'  : 'coffee' 
    } 
}; 

var response = WL.Server.invokeHttp(input); 
return response; 

}

+0

我也有類似的問題..我提到了所有大多數內容類型,但它根本不起作用。 – Sarath 2013-03-11 08:05:52

+0

錯誤描述是「服務器拒絕了此請求,因爲請求實體處於所請求資源不支持請求方法的格式(不支持的媒體類型)「....我指定了Content-Type:'application/json'..但仍然存在問題。 – Sarath 2013-03-11 08:09:27

2
在適配器

還有一個{適配器名} .XML

在這裏面,下連接connectionPolicy下,有協議。 您是否將其更改爲https並部署?