private FacebookClient.AccessToken getFacebookUserToken(String code, String redirectUrl) throws IOException { String appId = "YOUR_APP_ID"; String secretKey = "YOUR_SECRET_KEY"; WebRequestor wr = new DefaultWebRequestor(); WebRequestor.Response accessTokenResponse = wr.executeGet( "https://graph.facebook.com/oauth/access_token?client_id=" + appId + "&redirect_uri=" + redirectUrl + "&client_secret=" + secretKey + "&code=" + code); return DefaultFacebookClient.AccessToken.fromQueryString(accessTokenResponse.getBody()); }
/** * @see com.restfb.WebRequestor#executePost(String, * String) */ @Override public Response executePost(String url, String parameters) throws IOException { return executePost(url, parameters, null); }
@Override public Response executeGet(String url) throws IOException { return execute(url, HttpMethod.GET); }
httpUrlConnection = openConnection(new URL(url + (!binaryAttachments.isEmpty() ? "?" + parameters : ""))); httpUrlConnection.setReadTimeout(DEFAULT_READ_TIMEOUT_IN_MS); customizeConnection(httpUrlConnection); .append(createFormFieldName(binaryAttachment)).append("\"; filename=\"") .append(binaryAttachment.getFilename()).append("\""); write(binaryAttachment.getData(), outputStream, MULTIPART_DEFAULT_BUFFER_SIZE); fillHeaderAndDebugInfo(httpUrlConnection); Response response = fetchResponse(httpUrlConnection); if (autocloseBinaryAttachmentStream && !binaryAttachments.isEmpty()) { for (BinaryAttachment binaryAttachment : binaryAttachments) { closeQuietly(binaryAttachment.getData()); closeQuietly(outputStream); closeQuietly(httpUrlConnection);
private Response execute(String url, HttpMethod httpMethod) throws IOException { HTTP_LOGGER.debug("Making a {} request to {}", httpMethod.name(), url); HttpURLConnection httpUrlConnection = null; try { httpUrlConnection = openConnection(new URL(url)); httpUrlConnection.setReadTimeout(DEFAULT_READ_TIMEOUT_IN_MS); httpUrlConnection.setUseCaches(false); httpUrlConnection.setRequestMethod(httpMethod.name()); // Allow subclasses to customize the connection if they'd like to - set // their own headers, timeouts, etc. customizeConnection(httpUrlConnection); httpUrlConnection.connect(); HTTP_LOGGER.trace("Response headers: {}", httpUrlConnection.getHeaderFields()); fillHeaderAndDebugInfo(httpUrlConnection); Response response = fetchResponse(httpUrlConnection); HTTP_LOGGER.debug("Facebook responded with {}", response); return response; } finally { closeQuietly(httpUrlConnection); } }
customizeConnection(httpUrlConnection); .getBytes(ENCODING_CHARSET)); write(binaryAttachment, outputStream, MULTIPART_DEFAULT_BUFFER_SIZE); closeQuietly(binaryAttachment); closeQuietly(outputStream); closeQuietly(httpUrlConnection);
customizeConnection(httpUrlConnection); closeQuietly(httpUrlConnection);
/** * Creates a Facebook Graph API client with the given {@code accessToken}. * * @param accessToken * A Facebook OAuth access token. * @param apiVersion * Version of the api endpoint * @since 1.6.14 */ public DefaultFacebookClient(String accessToken, Version apiVersion) { this(accessToken, null, new DefaultWebRequestor(), new DefaultJsonMapper(), apiVersion); }
@Override public Response executePost(String url, String parameters) throws IOException { return executePost(url, parameters, null); }
@Override public Response executeDelete(String url) throws IOException { return execute(url, HttpMethod.DELETE); }
/** * Creates a Facebook Graph API client with the given {@code apiVersion}. * * @param apiVersion * Version of the api endpoint */ public DefaultFacebookClient(Version apiVersion) { this(null, null, new DefaultWebRequestor(), new DefaultJsonMapper(), apiVersion); }
/** * Creates a Facebook Graph API client with the given {@code accessToken}. * * @param accessToken A Facebook OAuth access token. */ public DefaultFacebookClient(String accessToken) { this(accessToken, new DefaultWebRequestor(), new DefaultJsonMapper()); }
/** * Creates a Facebook API client with the given <a * href="http://developers.facebook.com/docs/guides/upgrade#oauth">OAuth * access token</a>. * * @param accessToken An OAuth access token. * @throws NullPointerException If {@code accessToken} is {@code null}. * @throws IllegalArgumentException If {@code accessToken} is a blank string. * @since 1.5 */ public DefaultLegacyFacebookClient(String accessToken) { this(accessToken, new DefaultWebRequestor(), new DefaultJsonMapper()); }
/** * Creates a Facebook Graph API client with the given {@code accessToken}. * * @param accessToken * A Facebook OAuth access token. * @param appSecret * A Facebook application secret. * @param apiVersion * Version of the api endpoint * @since 1.6.14 */ public DefaultFacebookClient(String accessToken, String appSecret, Version apiVersion) { this(accessToken, appSecret, new DefaultWebRequestor(), new DefaultJsonMapper(), apiVersion); }
/** * Creates a Facebook API client with the given API key and secret key (Legacy * authentication). * * @param apiKey A Facebook API key. * @param secretKey A Facebook application secret key. * @throws NullPointerException If either parameter is {@code null}. * @throws IllegalArgumentException If either parameter is a blank string. * @deprecated You should use {@link #DefaultLegacyFacebookClient(String)} * instead. Facebook is moving to OAuth and will stop supporting * the old authentication scheme soon. */ @Deprecated public DefaultLegacyFacebookClient(String apiKey, String secretKey) { this(apiKey, secretKey, new DefaultWebRequestor(), new DefaultJsonMapper()); }
/** * Ctor. * @param fapp Facebook app * @param fkey Facebook key */ public PsFacebook(final String fapp, final String fkey) { this( new JdkRequest( new Href(PsFacebook.ACCESS_TOKEN_URL) .with(PsFacebook.CLIENT_ID, fapp) .with(PsFacebook.CLIENT_SECRET, fkey) .toString() ), new DefaultWebRequestor(), fapp, fkey ); }
/** * Ctor. * @param fapp Facebook app * @param fkey Facebook key */ public PsFacebook(final String fapp, final String fkey) { this( new JdkRequest( new Href(PsFacebook.ACCESS_TOKEN_URL) .with(PsFacebook.CLIENT_ID, fapp) .with(PsFacebook.CLIENT_SECRET, fkey) .toString() ), new DefaultWebRequestor(), fapp, fkey ); }