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();

この場合(下記ソース参照)、上記のaccess_tokenを利用して自身の情報を取得する際に例外(code:400)が発生しました。

        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 (不明なソース) で sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream (不明なソース) で biztopia.facebook.web.FacebookController.requestUserInfo(FacebookController.java) :318) で sun.reflect.NativeMethodAccessorImpl.invoke0(ネイティブ メソッド) で sun.reflect.NativeMethodAccessorImpl.invoke(不明なソース) で sun.reflect.DelegatingMethodAccessorImpl.invoke(不明なソース) で java.lang.reflect.Method.invoke(出典不明)

~ ~ ~ ~

「 https://graph.facebook.com/me?access_token=Token value 」としてWebブラウザ(エクスプローラー)でリクエストすると成功なのかと戸惑います。

解決策を持っている私を助けてください。

4

2 に答える 2

0

解決策を見つけました。これは、呼び出し API に appsecret_proof パラメータを " http://graph.facebook.com/me?access_token= {access_token value}$appsecret_proof={appsecret_proof value} として追加することです。

appsecret_proof パラメータを追加したくない場合は、アプリ管理サイトで 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 に答える