import com.google.common.collect.Maps; import org.apache.http.HttpEntity; import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.entity.ContentType; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.Map; /** * @author 26628327 */ public class FacebookApiClient { private static final Logger logger = LoggerFactory.getLogger(FacebookApiClient.class); private static String paramsToString(Map<String, String> params) { StringBuilder stringBuilder = new StringBuilder(); try { for (String key : params.keySet()) { stringBuilder.append(String.format("&%s=%s", key, URLEncoder.encode(params.get(key), StandardCharsets.UTF_8.toString()))); } } catch (UnsupportedEncodingException e) { logger.warn("{}: encode url parameters failed", e.getMessage()); } return stringBuilder.length() > 0 ? "?".concat(stringBuilder.substring(1)) : ""; } private static HttpResponse sendRequest(CloseableHttpClient httpclient, HttpUriRequest httpost) throws ClientProtocolException, IOException { HttpResponse response = null; response = httpclient.execute(httpost); return response; } private static String parseResponse(HttpResponse response) { HttpEntity entity = response.getEntity(); Charset charset = ContentType.getOrDefault(entity).getCharset(); if (charset != null) { logger.info(charset.name()); } String body = null; try { body = EntityUtils.toString(entity, "utf-8"); } catch (IOException e) { logger.warn("{}: cannot parse the entity", e.getMessage()); } return body; } public static String get(String url, Map<String, String> params) { String body = null; CloseableHttpClient httpClient = null; try { httpClient = HttpClientBuilder.create().build(); String queryString = paramsToString(params); HttpGet get = new HttpGet(url.concat(queryString)); get.addHeader("Accept-Charset", "utf-8"); // 设置网络代理 RequestConfig defaultRequestConfig = RequestConfig.custom() .setSocketTimeout(5000) .setConnectTimeout(5000) .setConnectionRequestTimeout(5000) .build(); // 外网代理地址和端口(注:是http请求,网上的一些https证书请求应该是不正确的,端口是http端口,不是socks端口) RequestConfig requestConfig = RequestConfig.copy(defaultRequestConfig). setProxy(new HttpHost("127.0.0.1", 10809)) .build(); get.setConfig(requestConfig); HttpResponse response = sendRequest(httpClient, get); body = parseResponse(response); } catch (IOException e) { logger.error("send post request failed: {}", e); } finally { try { httpClient.close(); } catch (IOException e) { e.printStackTrace(); } } return body; } public static void main(String[] args) { // HashMap<String, String> params = new HashMap<String, String>(); // String fields = "id,name,email"; // params.put("access_token", "112312EAAH32r3T83IBAAwgYqjAdqeJJ3ZArROdbBOAGcjZBmjQpCP1NlAMtSP6kZCp2CQkZCtZAb4CoDIz0dqljSkvJ67DzKQV***"); // params.put("fields", fields); // String body = get("https://graph.facebook.com/me", params); // System.out.println(body); Map map = Maps.newHashMap(); map.put("access_token", "XXXX5569XXX857%7C8e1bf610fe93cXXXXXX073dae8814"); map.put("input_token", "123123123123122"); String body = get("https://graph.facebook.com/debug_token", map); System.out.println(body); } }
facebook国内服务端访问api
于 2022-08-01 16:23:20 首次发布