Facebook刷新访问令牌:服务器返回HTTP响应代码:400

时间:2012-04-23 16:34:20

标签: java facebook facebook-access-token

我正在尝试通过调用详细here的新Facebook端点来自动刷新用户OAuth2.0 access_token

我正在构建一个Java(SEAM)Web应用程序,facebook身份验证是使用服务器端流程完成的

我一直收到响应代码400,任何人都可以看到下面的代码有什么问题,因为我不确定我哪里出错...

由于

public static String refreshFBAccessToken(String existingAccessToken)
        throws Exception{
    try {
        // Construct data
        String data = URLEncoder.encode("client_id", "UTF-8") + "=" + URLEncoder.encode(FacebookApp.appId, "UTF-8");
        data += "&" + URLEncoder.encode("client_secret", "UTF-8") + "=" + URLEncoder.encode(FacebookApp.appSecret, "UTF-8");
        data += "&" + URLEncoder.encode("grant_type", "UTF-8") + "=" + URLEncoder.encode("fb_exchange_token", "UTF-8");
        data += "&" + URLEncoder.encode("fb_exchange_token", "UTF-8") + "=" + URLEncoder.encode(existingAccessToken, "UTF-8");

        // Send data
        URL url = new URL("https://graph.facebook.com/oauth/access_token?");
        URLConnection connection = url.openConnection();
        connection.setDoOutput(true);
        OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
        writer.write(data);
        writer.flush();

        HttpURLConnection httpConn = (HttpURLConnection)connection;
        InputStream is;
        if (httpConn.getResponseCode() >= 400) {
            is = httpConn.getInputStream();
            System.err.println("Input Stream..."+is.toString());
        } else {
            is = httpConn.getErrorStream();
            System.err.println("Error Stream..."+is.toString());
        }

        while((line = reader.readLine()) != null) {
            System.err.println("Response: "+line);
            builder.append(line);
        }
        return builder.toString();
    } catch (Exception e) {
        System.err.println(e);
    }
    return null;
}

0 个答案:

没有答案