1

我正在尝试通过调用此处详述的新 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;
}
4

0 回答 0