从Google App Engine调用Parse.com REST服务

时间:2014-07-03 23:59:03

标签: google-app-engine rest servlets parse-platform httpurlconnection

我在Parse.com云中创建了一个数据库。我现在需要在我的Google App Engine应用程序中编写一个Servlet来调用Parse上的REST服务。 REST服务需要 用户身份验证,即Parse应用程序ID和Javascript密钥。

...
            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:javascript-key=myJavaScriptKey@api.parse.com/1/classes/GameScore/Ed1nuqPvcm

问题在于我一直在

  

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

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

EDIT。

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 个答案:

答案 0 :(得分:1)

使用标题版本的身份验证:

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