簡體   English   中英

在查詢字符串中發送JSON

[英]Send JSON in query String

我正在嘗試使用Facebook登錄到我的Web應用程序,但在最近我開始收到此錯誤之前,它一直工作正常

java.net.URISyntaxException: Illegal character in query at index 43: https://graph.facebook.com/me?access_token={"access_token":"E","token_type":"bearer"}

我正在嘗試使用以下方法解決它

public static void main(String[] args) {
        String accessToken = "{\"access_token\":\"E\",\"token_type\":\"bearer\"}";
        try {

            //String urlStr = "https://graph.facebook.com/me?access_token=" + URLEncoder.encode(accessToken,"UTF-8");
            String urlStr = "https://graph.facebook.com/me?access_token=" +accessToken;
            HttpClient httpclient = new DefaultHttpClient();
            HttpGet httpget = new HttpGet(urlStr);
            httpget.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            String responseBody = httpclient.execute(httpget, responseHandler);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

也嘗試使用發布請求

public static void main(String[] args) {
        String accessToken = "{\"access_token\":\"E\",\"token_type\":\"bearer\"}";
        try {

            //String urlStr = "https://graph.facebook.com/me?access_token=" + URLEncoder.encode(accessToken,"UTF-8");
            String urlStr = "https://graph.facebook.com/me";
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httpget = new HttpPost(urlStr);
            httpget.setEntity(new StringEntity("access_token="+accessToken));
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            String responseBody = httpclient.execute(httpget, responseHandler);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

但它產生以下異常

org.apache.http.client.HttpResponseException: Bad Request
    at org.apache.http.impl.client.AbstractResponseHandler.handleResponse(AbstractResponseHandler.java:69)
    at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:65)
    at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:51)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:222)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:164)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:139)
    at com.simsarak.servlet.facebook.FacebookLogin.main(FacebookLogin.java:178)

試圖使用URLEncoder並將標頭設置為application/json但仍然無法正常工作,這可能是什么問題?

找到了問題,看來我現在只需要從json中提取訪問令牌並單獨發送,我已經通過

private String getUserMailAddressFromJsonResponse(String accessToken, HttpSession httpSession) {

        HttpClient httpclient = new DefaultHttpClient();
        JSONParser parser = new JSONParser();
        JSONObject json = null;
        try {

            if (accessToken != null && !"".equals(accessToken)) {
                json = (JSONObject) parser.parse(accessToken);
                String newUrl = "https://graph.facebook.com/me?access_token=" + json.get("access_token");
                httpclient = new DefaultHttpClient();
                HttpGet httpget = new HttpGet(newUrl);
                ResponseHandler<String> responseHandler = new BasicResponseHandler();
                String responseBody = httpclient.execute(httpget, responseHandler);
...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM