0

我正在尝试使用 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: Server returned HTTP response code: 400 for URL: https://graph.facebook.com/me?access_token=CAAC7APV7WpoBAHVfr2ChZAK4wVrQZCjNSbro3LgABvoFSMMSHmiloS5m95z3DCeNsZBoOHFsClrgBVIqZCCwg8JZCK3Xd0fq6uyu8GJbYNENFQCDKz25IsguBSXuReapPvZA3ZC3BuJVLPwpZAfVCZCqFW0wj6o6ZA6nXO5JzCutZBAum2cJQjiBwctFkzxWqxinz8ZD 在 sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) 在 sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source) 在 biztopia.facebook.web.FacebookController.requestUserInfo(FacebookController.java :318) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 在 java.lang.reflect.Method.invoke(来源不明)

~ ~ ~ ~

当网络浏览器(资源管理器)上的请求为“ https://graph.facebook.com/me?access_token=Token value”时,我感到很困惑。

请有解决方案的人帮助我。

4

2 回答 2

0

我找到了解决方案。即在调用 API 时将 appsecret_proof 参数附加为“ http://graph.facebook.com/me?access_token= {access_token value}$appsecret_proof={appsecret_proof value}”。

如果您不想附加 appsecret_proof 参数,那么您可以在您的应用管理站点上将设置更改为 no use appsecret_proof 参数。管理站点菜单设置>高级>要求服务器 API 调用的 AppSecret Proof -> 设置为禁用。

于 2013-11-02T05:58:51.553 回答
0

这样做:

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;
于 2013-10-30T10:04:17.977 回答