[英]Fail to Facebook api request using URLConnection in JAVA
我正在嘗試使用Java開發Facebook API。
從Facebook獲得access_token作為流是成功的。
String access_token_url = targetURI +
"client_id=" + appId +
"&client_secret=" + appSecret +
"&code=" + code +
"&redirect_uri=" + redirectURI; // 호출되지 않음
URL url = new URL (access_token_url);
URLConnection urlConn = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
String accessTokenStr="";
while ((accessTokenStr=in.readLine()) != null) {
int endPnt = accessTokenStr.indexOf("&");
access_token = accessTokenStr.substring(13, endPnt);
System.out.println("@@@@@@@@@@@@@ access_token = " + access_token);
}
in.close();
這種情況(請參見以下資源)發生了異常(代碼:400),原因是使用上面的access_token獲取了自己的信息。
String access_userInfo_url = "https://graph.facebook.com/me?" + "access_token=" + access_token;
System.out.println("@@@@@@@@@@@ access_userInfo_url==============" + access_userInfo_url);
URL url = new URL (access_userInfo_url);
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
BufferedReader input = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
urlConn.setRequestMethod("GET");
//urlConn.setConnectTimeout(1000);
//urlConn.setReadTimeout(1000);
//urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//urlConn.setRequestProperty("Content-Length", String.valueOf(access_userInfo_url.length()));
String userInfoStr="";
while ((userInfoStr=input.readLine()) != null) {
System.out.println("@@@@@@@@@@@@@ userInfoStr = " + userInfoStr);
}
input.close();
接收到的興奮消息作為流。
java.io.IOException異常:服務器返回的HTTP響應代碼:400網址: https://graph.facebook.com/me?access_token=CAAC7APV7WpoBAHVfr2ChZAK4wVrQZCjNSbro3LgABvoFSMMSHmiloS5m95z3DCeNsZBoOHFsClrgBVIqZCCwg8JZCK3Xd0fq6uyu8GJbYNENFQCDKz25IsguBSXuReapPvZA3ZC3BuJVLPwpZAfVCZCqFW0wj6o6ZA6nXO5JzCutZBAum2cJQjiBwctFkzxWqxinz8ZD在sun.net.www.protocol.http.HttpURLConnection.getInputStream(來源不明)在sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(未知源)位於biztopia.facebook.web.FacebookController.requestUserInfo(FacebookController.java:318)位於sun.reflect.NativeMethodAccessorImpl.invoke0(本地方法)位於sun.reflect java.lang.reflect.Method.invoke的.NativeMethodAccessorImpl.invoke(未知源)java.lang.reflect.Method.invoke的。
~~~~
我很困惑,當在網絡瀏覽器(瀏覽器)上以“ https://graph.facebook.com/me?access_token=Token value”的形式請求時,它是成功的。
請幫我解決問題的人。
這樣做:
public String getUserInfo(String access_token) throws MalformedURLException, ProtocolException, IOException {
try {
String connection = connectionGet("https://graph.facebook.com/me?access_token=" + access_token, "");
return connection;
} catch (Exception e) {
return null;
}
}
public static String connectionGet(String url, String parameter) throws MalformedURLException, ProtocolException, IOException {
URL url1 = new URL(url);
HttpURLConnection request1 = (HttpURLConnection) url1.openConnection();
request1.setRequestMethod("GET");
request1.connect();
String responseBody = convertStreamToString(request1.getInputStream());
return responseBody;
}
private static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
} catch (IOException e) {
} finally {
try {
is.close();
} catch (IOException e) {
}
}
return sb.toString();
}
必要的進口將是:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
我找到了解決方案。 也就是說,在調用API處將appsecret_proof參數附加為“ http://graph.facebook.com/me?access_token= {access_token值} $ appsecret_proof = {appsecret_proof值}”。
如果您不想追加appsecret_proof參數,則可以在應用程序管理站點上將設置更改為“不使用appsecret_proof”參數。 管理站點菜單設置為>高級>服務器API調用需要AppSecret證明->設置為禁用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.