2012-07-23 4 views
8

Ich bin mit IBM worklight herumspielen, und versuche, einen Adapter zu erstellen, um einige Daten aus dem Google places API zu füttern.Wie können HTTPS-Anfragen mit serverseitigem JavaScript mit Worklight durchgeführt werden?

Ich möchte diese URL aufzurufen:

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

Ausführung dieser URL arbeitet in einem Browser in Ordnung, und zeigt einige schöne JSON, die ich über Worklight zu erhalten bin versucht.

Die Worklight Adapter in Javascript erstellt wird, ist es das, was ich bisher:

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; 
    } 
} 

Und das ist das Ergebnis, dass ich in der Konsole, wenn ich die oben testen:

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 

Ich denke, dass dies wahrscheinlich verursacht wird, weil der Adapter als HTTP anfordert, während es HTTPS verwenden sollte.

Wenn ich die Anforderung zur Verwendung von HTTP in einem Browser ändere, werden ähnliche Ergebnisse angezeigt.

Frage: Kann ich eine HTTPS-Anfrage stellen, indem ich das obige Javascript ändere, oder verarbeite ich Worklight-Adapter?

Antwort

7

Sieht aus wie googleapis wird nicht funktionieren, wenn Sie Host-Header nicht innerhalb Ihrer Anfrage angeben. Nach es alles Hinzufügen funktioniert wie es sollte:

Dieser Abschnitt XML-Adaptern ist

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

Dies ist JS des Adapters:

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

auch ich ein ähnliches Problem .. Ich erwähnte alle die meisten Inhaltstypen, aber es funktioniert überhaupt nicht. – Sarath

+0

Die Fehlerbeschreibung ist "Der Server hat diese Anfrage abgelehnt, weil die Anfrage Entität in ist ein Format, das von der angeforderten Ressource für die angeforderte Methode nicht unterstützt wird (Nicht unterstützter Medientyp) ".... Ich habe den Inhaltstyp: 'application/json' .. angegeben, aber das Problem besteht weiterhin .. – Sarath

2

in Ihrem Adapter gibt es auch eine {ADAPTER NAME} .xml

Darin unter Konnektivität unter connectionPolicy gibt das Protokoll. Haben Sie es in https geändert und bereitstellen?

Verwandte Themen