我试图使用Facebook登录我的网络应用程序,它一直在工作,直到最近我才开始收到此错误
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
,但仍无效,可能是什么问题?
答案 0 :(得分:0)
发现问题,似乎我现在只从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);
...