简体   繁体   English

登录404 Parse.com REST API Java

[英]Login 404 Parse.com REST API Java

We decided to build our own twitter login for aesthetic reasons rather than use ParseTwitterUtils.login(), and we are having to login through the REST API (unless someone has a better idea on how to get a session token for a user with twitterAuth). 我们出于审美原因决定构建自己的Twitter登录,而不是使用ParseTwitterUtils.login(),并且我们必须通过REST API登录(除非有人对如何使用twitterAuth为用户获取会话令牌有更好的主意) 。

So currently it is set up as such: 所以目前它是这样设置的:

private class ParseLogin extends AsyncTask<String, String, Boolean> {

    JSONObject authData;
    JSONObject wrapper;

    @Override
    protected void onPreExecute() {
        try {
            authData = new JSONObject();
            wrapper = new JSONObject();
            JSONObject twitterAuth = new JSONObject();
            twitterAuth.put("id", Long.toString(twitterUser.getId()));
            twitterAuth.put("screen_name", twitterUser.getScreenName());
            twitterAuth.put("consumer_key", CONSUMER_KEY);
            twitterAuth.put("consumer_secret", CONSUMER_SECRET);
            twitterAuth.put("auth_token", accessToken.getToken());
            twitterAuth.put("auth_secret", accessToken.getTokenSecret());
            authData.put("twitter", twitterAuth);
            wrapper.put("authData", authData);


        } catch (JSONException e)
        {
            e.printStackTrace();
        }
    }
    @Override
    protected Boolean doInBackground(String... args) {
        // Send the HttpPostRequest and receive a JSONObject in return
        JSONObject jsonObjRecv = JSONRequest.SendHttpGet("https://api.parse.com/1/users/", wrapper);

        return true;
    }
    @Override
    protected void onPostExecute(Boolean response) {




    }

}

Which then in turns sends the login request to the REST API via here 然后依次通过此处将登录请求发送到REST API

public static JSONObject SendHttpPost(String URL, JSONObject jsonObjSend) {

    try {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpGetWithEntity httpPostRequest = new HttpGetWithEntity(URL);

        StringEntity se;
        se = new StringEntity(jsonObjSend.toString());

        // Set HTTP parameters
        httpPostRequest.setEntity(se);
        httpPostRequest.setHeader("X-Parse-Application-Id", APP KEY);
        httpPostRequest.setHeader("X-Parse-REST-API-Key", REST API KEY);
        httpPostRequest.setHeader("Accept", "application/json");
        httpPostRequest.setHeader("Content-type", "application/json");

        long t = System.currentTimeMillis();
        HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest);
        Log.i(TAG, "HTTPResponse received in [" + (System.currentTimeMillis()-t) + "ms]");

        // Get hold of the response entity (-> the data):
        HttpEntity entity = response.getEntity();

        if (entity != null) {
            // Read the content stream
            InputStream instream = entity.getContent();
            Header contentEncoding = response.getFirstHeader("Content-Encoding");
            if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
                instream = new GZIPInputStream(instream);
            }

            // convert content stream to a String
            String resultString= convertStreamToString(instream);
            Log.d("JSON RESULT", resultString);
            instream.close();
            resultString = resultString.substring(1,resultString.length()-1); // remove wrapping "[" and "]"

            // Transform the String into a JSONObject
            JSONObject jsonObjRecv = new JSONObject(resultString);
            // Raw DEBUG output of our received JSON object:
            Log.i(TAG,"<JSONObject>\n"+jsonObjRecv.toString()+"\n</JSONObject>");

            return jsonObjRecv;

Any ideas on the 404? 对404有什么想法吗?

Edit: Fixed - Use /Users/ instead of /Login/ for 3rd party auth, changes from GET to POST 编辑:固定-使用/ Users /代替/ Login /进行第三方身份验证,从GET更改为POST

  private class ParseLogin extends AsyncTask<String, String, Boolean> {

    JSONObject authData;
    JSONObject wrapper;

    @Override
    protected void onPreExecute() {
        try {
            authData = new JSONObject();
            wrapper = new JSONObject();
            JSONObject twitterAuth = new JSONObject();
            twitterAuth.put("id", Long.toString(twitterUser.getId()));
            twitterAuth.put("screen_name", twitterUser.getScreenName());
            twitterAuth.put("consumer_key", CONSUMER_KEY);
            twitterAuth.put("consumer_secret", CONSUMER_SECRET);
            twitterAuth.put("auth_token", accessToken.getToken());
            twitterAuth.put("auth_token_secret", accessToken.getTokenSecret());
            authData.put("twitter", twitterAuth);
            wrapper.put("authData", authData);


        } catch (JSONException e)
        {
            e.printStackTrace();
        }
    }
    @Override
    protected Boolean doInBackground(String... args) {
        // Send the HttpPostRequest and receive a JSONObject in return
        JSONObject jsonObjRecv = JSONRequest.SendHttpGet("https://api.parse.com/1/users/", wrapper);

        return true;
    }
    @Override
    protected void onPostExecute(Boolean response) {

    }

And then the Get (which is actually a post, just need to changed the method name) 然后是Get(实际上是一个帖子,只需要更改方法名称)

public static JSONObject SendHttpGet(String URL, JSONObject jsonObjSend) {

    try {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPost httpPostRequest = new HttpPost(URL);

        StringEntity se;
        se = new StringEntity(jsonObjSend.toString());

        // Set HTTP parameters
        httpPostRequest.setEntity(se);
        httpPostRequest.setHeader("X-Parse-Application-Id", APP ID);
        httpPostRequest.setHeader("X-Parse-REST-API-Key", REST API KEY);
        httpPostRequest.setHeader("Accept", "application/json");
        httpPostRequest.setHeader("Content-type", "application/json");
        //httpPostRequest.setHeader("Accept-Encoding", "gzip"); // only set this parameter if you would like to use gzip compression

        long t = System.currentTimeMillis();
        HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest);
        Log.i(TAG, "HTTPResponse received in [" + (System.currentTimeMillis()-t) + "ms]");

        // Get hold of the response entity (-> the data):
        HttpEntity entity = response.getEntity();

        if (entity != null) {
            // Read the content stream
            InputStream instream = entity.getContent();
            Header contentEncoding = response.getFirstHeader("Content-Encoding");
            if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
                instream = new GZIPInputStream(instream);
            }

            // convert content stream to a String
            String resultString= convertStreamToString(instream);
            Log.d("JSON RESULT", resultString);
            instream.close();
            resultString = resultString.substring(1,resultString.length()-1); // remove wrapping "[" and "]"

            // Transform the String into a JSONObject
            JSONObject jsonObjRecv = new JSONObject(resultString);
            // Raw DEBUG output of our received JSON object:
            Log.i(TAG,"<JSONObject>\n"+jsonObjRecv.toString()+"\n</JSONObject>");

            return jsonObjRecv;
        }

    }
    catch (Exception e)
    {
        // More about HTTP exception handling in another tutorial.
        // For now we just print the stack trace.
        e.printStackTrace();
    }
    return null;
}

Fixed it. 修复。 Should have been a POST to /Users/ rather than a GET from /Login/. 应该是到/ Users /的POST,而不是/ Login /的GET。 Sharing incase anyone else runs into this. 共享,以防其他任何人遇到这个问题。

private class ParseLogin extends AsyncTask { 私有类ParseLogin扩展了AsyncTask {

    JSONObject authData;
    JSONObject wrapper;

    @Override
    protected void onPreExecute() {
        try {
            authData = new JSONObject();
            wrapper = new JSONObject();
            JSONObject twitterAuth = new JSONObject();
            twitterAuth.put("id", Long.toString(twitterUser.getId()));
            twitterAuth.put("screen_name", twitterUser.getScreenName());
            twitterAuth.put("consumer_key", CONSUMER_KEY);
            twitterAuth.put("consumer_secret", CONSUMER_SECRET);
            twitterAuth.put("auth_token", accessToken.getToken());
            twitterAuth.put("auth_token_secret", accessToken.getTokenSecret());
            authData.put("twitter", twitterAuth);
            wrapper.put("authData", authData);


        } catch (JSONException e)
        {
            e.printStackTrace();
        }
    }
    @Override
    protected Boolean doInBackground(String... args) {
        // Send the HttpPostRequest and receive a JSONObject in return
        JSONObject jsonObjRecv = JSONRequest.SendHttpGet("https://api.parse.com/1/users/", wrapper);

        return true;
    }
    @Override
    protected void onPostExecute(Boolean response) {

    }

And then the Get (which is actually a post, just need to changed the method name) 然后是Get(实际上是一个帖子,只需要更改方法名称)

public static JSONObject SendHttpGet(String URL, JSONObject jsonObjSend) {

    try {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPost httpPostRequest = new HttpPost(URL);

        StringEntity se;
        se = new StringEntity(jsonObjSend.toString());

        // Set HTTP parameters
        httpPostRequest.setEntity(se);
        httpPostRequest.setHeader("X-Parse-Application-Id", APP ID);
        httpPostRequest.setHeader("X-Parse-REST-API-Key", REST API KEY);
        httpPostRequest.setHeader("Accept", "application/json");
        httpPostRequest.setHeader("Content-type", "application/json");
        //httpPostRequest.setHeader("Accept-Encoding", "gzip"); // only set this parameter if you would like to use gzip compression

        long t = System.currentTimeMillis();
        HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest);
        Log.i(TAG, "HTTPResponse received in [" + (System.currentTimeMillis()-t) + "ms]");

        // Get hold of the response entity (-> the data):
        HttpEntity entity = response.getEntity();

        if (entity != null) {
            // Read the content stream
            InputStream instream = entity.getContent();
            Header contentEncoding = response.getFirstHeader("Content-Encoding");
            if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
                instream = new GZIPInputStream(instream);
            }

            // convert content stream to a String
            String resultString= convertStreamToString(instream);
            Log.d("JSON RESULT", resultString);
            instream.close();
            resultString = resultString.substring(1,resultString.length()-1); // remove wrapping "[" and "]"

            // Transform the String into a JSONObject
            JSONObject jsonObjRecv = new JSONObject(resultString);
            // Raw DEBUG output of our received JSON object:
            Log.i(TAG,"<JSONObject>\n"+jsonObjRecv.toString()+"\n</JSONObject>");

            return jsonObjRecv;
        }

    }
    catch (Exception e)
    {
        // More about HTTP exception handling in another tutorial.
        // For now we just print the stack trace.
        e.printStackTrace();
    }
    return null;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM