2014-07-03 129 views
1

我在Parse.com云中创建了一个数据库。我现在需要在Google App Engine应用程序中编写一个Servlet来调用Parse上的REST服务。 REST服务需要 用户认证,它是Parse应用程序标识和Javascript键。从Google App Engine调用Parse.com REST服务

... 
      URL url = new URL("https://api.parse.com/1/classes/OBJECT"); 
      HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
      connection.setRequestProperty("Content-Type", "application/json"); 

      Base64 enc = new Base64(); 
      String userpassword = "{PARSE_APP_ID}" + ":" + "javascript-key={PARSE_JS_KEY}"; 
      String encodedAuthorization = enc.encodeBase64String(userpassword.getBytes()); 
      connection.setRequestProperty("Authorization", "Basic " + encodedAuthorization); 
      connection.setRequestMethod("GET"); 
... 

我使用org.apache.commons.codec.binary.Base64进行编码以获得REST调用认证。

的Parse.com REST API建议使用以下格式要求做出HTTP调用:

https://myAppID:[email protected]/1/classes/GameScore/Ed1nuqPvcm 

的问题是,我一直得到

{ “错误”: “未经授权” }

是否有任何人使用经过身份验证的REST服务的经验?谢谢!

编辑。

URL url = new URL("https://api.parse.com/1/classes/OBJECT"); 
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
connection.setRequestMethod("GET"); 
connection.setRequestProperty("X-Parse-Application-Id", "{APP_ID}"); 
connection.setRequestProperty("X-Parse-REST-API-Key", "REST_ID"); 
connection.setRequestProperty("Content-Type", "application/json"); 

我仍然得到了同样的错误,而responseCode是“200”。

干杯,

回答

1

使用认证头版本来代替:

connection.setRequestProperty("X-Parse-Application-Id", "app id here"); 
connection.setRequestProperty("X-Parse-REST-API-Key", "rest key here"); 
+0

感谢您的答复。但是在我做出改变后,我仍然遇到同样的错误。 – alextc

+0

你确定你使用正确的应用程序ID和其他密钥吗?未经授权的消息是另有说明。 – Fosco

+0

https://www.evernote.com/shard/s53/sh/0b483880-ef94-4918-8e18-2056263b0bc1/7c4d070a001adb36cbb602f497989fc4 – Fosco