Class DefaultFacebookClient
- java.lang.Object
-
- com.restfb.DefaultFacebookClient
-
- All Implemented Interfaces:
FacebookClient
- Direct Known Subclasses:
DefaultInstagramClient
,DefaultInstagramLoginClient
,DefaultThreadsClient
public class DefaultFacebookClient extends Object implements FacebookClient
Default implementation of a Facebook Graph API client.- Author:
- Mark Allen
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static interface
DefaultFacebookClient.Requestor
-
Field Summary
Fields Modifier and Type Field Description protected static String
ACCESS_TOKEN_PARAM_NAME
Reserved access token parameter name.protected String
accessToken
Graph API access token.protected boolean
accessTokenInHeader
static String
ALGORITHM
protected Version
apiVersion
Version of API endpoint.static String
APP_ID
static String
APP_SECRET
protected static String
APP_SECRET_PROOF_PARAM_NAME
Reserved application secret proof parameter name.protected String
appSecret
Graph API app secret.static String
CANNOT_EXTRACT_ACCESS_TOKEN_MESSAGE
static String
CLIENT_ID
static String
CODE
static String
CONNECTION
static String
CONNECTION_TYPE
protected static String
FORMAT_PARAM_NAME
Reserved "result format" parameter name.static String
GRANT_TYPE
protected boolean
httpDeleteFallback
By default, this isfalse
, so real http DELETE is usedprotected static String
IDS_PARAM_NAME
Reserved "multiple IDs" parameter name.protected JsonMapper
jsonMapper
Handles mapping Facebook response JSON to Java objects.protected static String
METHOD_PARAM_NAME
Reserved method override parameter name.static String
PARAM_CLIENT_SECRET
static String
PATH_OAUTH_ACCESS_TOKEN
static String
REDIRECT_URI
static String
SCOPE
protected WebRequestor
webRequestor
HandlesGET
s andPOST
s to the Facebook API endpoint.
-
Constructor Summary
Constructors Modifier Constructor Description protected
DefaultFacebookClient()
DefaultFacebookClient(Version apiVersion)
Creates a Facebook Graph API client with the givenapiVersion
.DefaultFacebookClient(String accessToken, Version apiVersion)
Creates a Facebook Graph API client with the givenaccessToken
.DefaultFacebookClient(String accessToken, WebRequestor webRequestor, JsonMapper jsonMapper, Version apiVersion)
Creates a Facebook Graph API client with the givenaccessToken
.DefaultFacebookClient(String accessToken, String appSecret, Version apiVersion)
Creates a Facebook Graph API client with the givenaccessToken
.DefaultFacebookClient(String accessToken, String appSecret, WebRequestor webRequestor, JsonMapper jsonMapper, Version apiVersion)
Creates a Facebook Graph API client with the givenaccessToken
,webRequestor
, andjsonMapper
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description List<AccessToken>
convertSessionKeysToAccessTokens(String appId, String secretKey, String... sessionKeys)
Converts an arbitrary number ofsessionKeys
to OAuth access tokens.protected String
createBaseUrlForEndpoint(String apiCall, boolean hasAttachment, boolean hasReel)
FacebookClient
createClientWithAccessToken(String accessToken)
creates a newFacebookClient
from an old one.protected String
createEndpointForApiCall(String apiCall, boolean hasAttachment, boolean hasReel)
Given an api call (e.g.DebugTokenInfo
debugToken(String inputToken)
When working with access tokens, you may need to check what information is associated with them, such as its user or expiry.boolean
deleteObject(String object, Parameter... parameters)
Performs a Graph API delete operation on the givenobject
.List<BatchResponse>
executeBatch(BatchRequest... batchRequests)
Executes operations as a batch using the Batch API.List<BatchResponse>
executeBatch(List<BatchRequest> batchRequests)
Executes operations as a batch using the Batch API.List<BatchResponse>
executeBatch(List<BatchRequest> batchRequests, List<BinaryAttachment> binaryAttachments)
Executes operations as a batch with binary attachments using the Batch API.<T> Connection<T>
fetchConnection(String connection, Class<T> connectionType, Parameter... parameters)
Fetches a Graph APIConnection
type, mapping the result to an instance ofconnectionType
.<T> Connection<T>
fetchConnectionPage(String connectionPageUrl, Class<T> connectionType)
Fetches a previous/next page of a Graph APIConnection
type, mapping the result to an instance ofconnectionType
.DeviceCode
fetchDeviceCode(ScopeBuilder scope)
Method to initialize the device access token generation.<T> T
fetchObject(String object, Class<T> objectType, Parameter... parameters)
Fetches a single Graph API object, mapping the result to an instance ofobjectType
.<T> T
fetchObjects(List<String> ids, Class<T> objectType, Parameter... parameters)
Fetches multiple Graph API objects in a single call, mapping the results to an instance ofobjectType
.protected AccessToken
getAccessTokenFromResponse(String response)
FacebookEndpoints
getFacebookEndpointUrls()
FacebookExceptionGenerator
getFacebookExceptionGenerator()
fetch the current facebook exception generator implementing theFacebookExceptionGenerator
interfaceprotected String
getFacebookGraphEndpointUrl()
Returns the base endpoint URL for the Graph API.protected String
getFacebookGraphVideoEndpointUrl()
Returns the base endpoint URL for the Graph APIs video upload functionality.protected String
getFacebookReelsUploadEndpointUrl()
Returns the Facebook Reels Upload endpoint URL for handling the Reels Uploadprotected String
getGenericLoginDialogUrl(String appId, String redirectUri, ScopeBuilder scope, Supplier<String> endpointSupplier, String state, List<Parameter> parameters)
JsonMapper
getJsonMapper()
Gets theJsonMapper
used to convert Facebook JSON to Java objects.String
getLoginDialogUrl(String appId, String redirectUri, ScopeBuilder scope, Parameter... parameters)
generates the login dialog urlString
getLoginDialogUrl(String appId, String redirectUri, ScopeBuilder scope, String state, Parameter... parameters)
generates the login dialog urlString
getLogoutUrl(String next)
generates an logout urlWebRequestor
getWebRequestor()
Gets theWebRequestor
used to talk to the Facebook API endpoints.boolean
isHttpDeleteFallback()
returns if the fallback post method (true
) is used or the http delete (false
)protected String
makeRequest(String endpoint, boolean executeAsPost, boolean executeAsDelete, List<BinaryAttachment> binaryAttachments, Body body, Parameter... parameters)
Coordinates the process of executing the API request GET/POST and processing the response we receive from the endpoint.protected String
makeRequest(String endpoint, boolean executeAsPost, boolean executeAsDelete, List<BinaryAttachment> binaryAttachments, Parameter... parameters)
protected String
makeRequest(String endpoint, Parameter... parameters)
Coordinates the process of executing the API request GET/POST and processing the response we receive from the endpoint.protected String
makeRequestAndProcessResponse(DefaultFacebookClient.Requestor requestor)
AccessToken
obtainAppAccessToken(String appId, String appSecret)
Obtains an access token which can be used to perform Graph API operations on behalf of an application instead of a user.String
obtainAppSecretProof(String accessToken, String appSecret)
Generates anappsecret_proof
value.AccessToken
obtainDeviceAccessToken(String code)
Method to poll Facebook and fetch the Device Access Token.AccessToken
obtainExtendedAccessToken(String appId, String appSecret)
Convenience method which invokesFacebookClient.obtainExtendedAccessToken(String, String, String)
with the current access token.AccessToken
obtainExtendedAccessToken(String appId, String appSecret, String accessToken)
Obtains an extended access token for the given existing, non-expired, short-lived access_token.AccessToken
obtainRefreshedExtendedAccessToken()
Obtain a refreshed Instagram extended access token.AccessToken
obtainUserAccessToken(String appId, String appSecret, String redirectUri, String verificationCode)
Obtains an access token which can be used to perform Graph API operations on behalf of a user.protected Parameter[]
parametersWithAdditionalParameter(Parameter parameter, Parameter... parameters)
Appends the givenparameter
to the givenparameters
array.<T> T
parseSignedRequest(String signedRequest, String appSecret, Class<T> objectType)
Parses a signed request and verifies it against your App Secret.<T> T
publish(String connection, Class<T> objectType, BinaryAttachment binaryAttachment, Parameter... parameters)
Performs a Graph API publish operation on the givenconnection
and includes a file - a photo, for example - in the publish request, and mapping the result to an instance ofobjectType
.<T> T
publish(String connection, Class<T> objectType, Body body, Parameter... parameters)
Performs a Graph API publish operation on the givenconnection
and includes special body in the publish request, and mapping the result to an instance ofobjectType
.<T> T
publish(String connection, Class<T> objectType, Parameter... parameters)
Performs a Graph API publish operation on the givenconnection
, mapping the result to an instance ofobjectType
.<T> T
publish(String connection, Class<T> objectType, List<BinaryAttachment> binaryAttachments, Parameter... parameters)
Performs a Graph API publish operation on the givenconnection
and includes some files - photos, for example - in the publish request, and mapping the result to an instance ofobjectType
.void
setFacebookEndpointUrls(FacebookEndpoints facebookEndpointUrls)
void
setFacebookExceptionGenerator(FacebookExceptionGenerator exceptionGenerator)
override the default facebook exception generator to provide a custom handling for the facebook error objectsvoid
setHeaderAuthorization(boolean accessTokenInHttpHeader)
Switch between access token in header and access token in query parameters (default)void
setHttpDeleteFallback(boolean httpDeleteFallback)
Set totrue
if the facebook http delete fallback should be used.protected String
toParameterString(boolean withJsonParameter, Parameter... parameters)
Generate the parameter string to be included in the Facebook API request.protected String
toParameterString(Parameter... parameters)
Generate the parameter string to be included in the Facebook API request.protected String
urlDecodeSignedRequestToken(String signedRequestToken)
Decodes a component of a signed request received from Facebook using FB's special URL-encoding strategy.protected String
urlEncodedValueForParameterName(String name, String value)
Gets the URL-encoded version of the givenvalue
for the parameter namedname
.protected void
verifyParameterLegality(Parameter... parameters)
Verifies that the provided parameter names don't collide with the ones we internally pass along to Facebook.protected boolean
verifySignedRequest(String appSecret, String algorithm, String encodedPayload, byte[] signature)
Verifies that the signed request is really from Facebook.
-
-
-
Field Detail
-
CLIENT_ID
public static final String CLIENT_ID
- See Also:
- Constant Field Values
-
APP_ID
public static final String APP_ID
- See Also:
- Constant Field Values
-
APP_SECRET
public static final String APP_SECRET
- See Also:
- Constant Field Values
-
SCOPE
public static final String SCOPE
- See Also:
- Constant Field Values
-
CANNOT_EXTRACT_ACCESS_TOKEN_MESSAGE
public static final String CANNOT_EXTRACT_ACCESS_TOKEN_MESSAGE
- See Also:
- Constant Field Values
-
PARAM_CLIENT_SECRET
public static final String PARAM_CLIENT_SECRET
- See Also:
- Constant Field Values
-
CONNECTION
public static final String CONNECTION
- See Also:
- Constant Field Values
-
CONNECTION_TYPE
public static final String CONNECTION_TYPE
- See Also:
- Constant Field Values
-
ALGORITHM
public static final String ALGORITHM
- See Also:
- Constant Field Values
-
PATH_OAUTH_ACCESS_TOKEN
public static final String PATH_OAUTH_ACCESS_TOKEN
- See Also:
- Constant Field Values
-
REDIRECT_URI
public static final String REDIRECT_URI
- See Also:
- Constant Field Values
-
GRANT_TYPE
public static final String GRANT_TYPE
- See Also:
- Constant Field Values
-
CODE
public static final String CODE
- See Also:
- Constant Field Values
-
accessToken
protected String accessToken
Graph API access token.
-
IDS_PARAM_NAME
protected static final String IDS_PARAM_NAME
Reserved "multiple IDs" parameter name.- See Also:
- Constant Field Values
-
apiVersion
protected Version apiVersion
Version of API endpoint.
-
httpDeleteFallback
protected boolean httpDeleteFallback
By default, this isfalse
, so real http DELETE is used
-
accessTokenInHeader
protected boolean accessTokenInHeader
-
webRequestor
protected WebRequestor webRequestor
HandlesGET
s andPOST
s to the Facebook API endpoint.
-
jsonMapper
protected JsonMapper jsonMapper
Handles mapping Facebook response JSON to Java objects.
-
METHOD_PARAM_NAME
protected static final String METHOD_PARAM_NAME
Reserved method override parameter name.- See Also:
- Constant Field Values
-
FORMAT_PARAM_NAME
protected static final String FORMAT_PARAM_NAME
Reserved "result format" parameter name.- See Also:
- Constant Field Values
-
ACCESS_TOKEN_PARAM_NAME
protected static final String ACCESS_TOKEN_PARAM_NAME
Reserved access token parameter name.- See Also:
- Constant Field Values
-
APP_SECRET_PROOF_PARAM_NAME
protected static final String APP_SECRET_PROOF_PARAM_NAME
Reserved application secret proof parameter name.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
DefaultFacebookClient
protected DefaultFacebookClient()
-
DefaultFacebookClient
public DefaultFacebookClient(Version apiVersion)
Creates a Facebook Graph API client with the givenapiVersion
.- Parameters:
apiVersion
- Version of the api endpoint
-
DefaultFacebookClient
public DefaultFacebookClient(String accessToken, Version apiVersion)
Creates a Facebook Graph API client with the givenaccessToken
.- Parameters:
accessToken
- A Facebook OAuth access token.apiVersion
- Version of the api endpoint- Since:
- 1.6.14
-
DefaultFacebookClient
public DefaultFacebookClient(String accessToken, String appSecret, Version apiVersion)
Creates a Facebook Graph API client with the givenaccessToken
.- Parameters:
accessToken
- A Facebook OAuth access token.appSecret
- A Facebook application secret.apiVersion
- Version of the api endpoint- Since:
- 1.6.14
-
DefaultFacebookClient
public DefaultFacebookClient(String accessToken, WebRequestor webRequestor, JsonMapper jsonMapper, Version apiVersion)
Creates a Facebook Graph API client with the givenaccessToken
.- Parameters:
accessToken
- A Facebook OAuth access token.webRequestor
- TheWebRequestor
implementation to use for sending requests to the API endpoint.jsonMapper
- TheJsonMapper
implementation to use for mapping API response JSON to Java objects.apiVersion
- Version of the api endpoint- Throws:
NullPointerException
- IfjsonMapper
orwebRequestor
isnull
.- Since:
- 1.6.14
-
DefaultFacebookClient
public DefaultFacebookClient(String accessToken, String appSecret, WebRequestor webRequestor, JsonMapper jsonMapper, Version apiVersion)
Creates a Facebook Graph API client with the givenaccessToken
,webRequestor
, andjsonMapper
.- Parameters:
accessToken
- A Facebook OAuth access token.appSecret
- A Facebook application secret.webRequestor
- TheWebRequestor
implementation to use for sending requests to the API endpoint.jsonMapper
- TheJsonMapper
implementation to use for mapping API response JSON to Java objects.apiVersion
- Version of the api endpoint- Throws:
NullPointerException
- IfjsonMapper
orwebRequestor
isnull
.
-
-
Method Detail
-
setHeaderAuthorization
public void setHeaderAuthorization(boolean accessTokenInHttpHeader)
Switch between access token in header and access token in query parameters (default)- Parameters:
accessTokenInHttpHeader
-true
use access token as header field,false
use access token as query parameter (default)
-
setFacebookExceptionGenerator
public void setFacebookExceptionGenerator(FacebookExceptionGenerator exceptionGenerator)
override the default facebook exception generator to provide a custom handling for the facebook error objects- Parameters:
exceptionGenerator
- the custom exception generator implementing theFacebookExceptionGenerator
interface
-
getFacebookExceptionGenerator
public FacebookExceptionGenerator getFacebookExceptionGenerator()
fetch the current facebook exception generator implementing theFacebookExceptionGenerator
interface- Returns:
- the current facebook exception generator
-
deleteObject
public boolean deleteObject(String object, Parameter... parameters)
Description copied from interface:FacebookClient
Performs a Graph API delete operation on the givenobject
.- Specified by:
deleteObject
in interfaceFacebookClient
- Parameters:
object
- The ID of the object to delete.parameters
- URL parameters to include in the API call.- Returns:
true
if Facebook indicated that the object was successfully deleted,false
otherwise.
-
fetchConnection
public <T> Connection<T> fetchConnection(String connection, Class<T> connectionType, Parameter... parameters)
Description copied from interface:FacebookClient
Fetches a Graph APIConnection
type, mapping the result to an instance ofconnectionType
.- Specified by:
fetchConnection
in interfaceFacebookClient
- Type Parameters:
T
- Java type to map to.- Parameters:
connection
- The name of the connection, e.g."me/feed"
.connectionType
- Connection type token.parameters
- URL parameters to include in the API call (optional).- Returns:
- An instance of type
connectionType
which contains the requested Connection's data. - See Also:
FacebookClient.fetchConnection(java.lang.String, java.lang.Class, com.restfb.Parameter[])
-
fetchConnectionPage
public <T> Connection<T> fetchConnectionPage(String connectionPageUrl, Class<T> connectionType)
Description copied from interface:FacebookClient
Fetches a previous/next page of a Graph APIConnection
type, mapping the result to an instance ofconnectionType
.- Specified by:
fetchConnectionPage
in interfaceFacebookClient
- Type Parameters:
T
- Java type to map to.- Parameters:
connectionPageUrl
- The URL of the connection page to fetch, usually retrieved viaConnection.getPreviousPageUrl()
orConnection.getNextPageUrl()
.connectionType
- Connection type token.- Returns:
- An instance of type
connectionType
which contains the requested Connection's data. - See Also:
FacebookClient.fetchConnectionPage(java.lang.String, java.lang.Class)
-
fetchObject
public <T> T fetchObject(String object, Class<T> objectType, Parameter... parameters)
Description copied from interface:FacebookClient
Fetches a single Graph API object, mapping the result to an instance ofobjectType
.- Specified by:
fetchObject
in interfaceFacebookClient
- Type Parameters:
T
- Java type to map to.- Parameters:
object
- ID of the object to fetch, e.g."me"
.objectType
- Object type token.parameters
- URL parameters to include in the API call (optional).- Returns:
- An instance of type
objectType
which contains the requested object's data. - See Also:
FacebookClient.fetchObject(java.lang.String, java.lang.Class, com.restfb.Parameter[])
-
createClientWithAccessToken
public FacebookClient createClientWithAccessToken(String accessToken)
Description copied from interface:FacebookClient
creates a newFacebookClient
from an old one.App secret and api version are taken from the original client.
- Specified by:
createClientWithAccessToken
in interfaceFacebookClient
- Parameters:
accessToken
- this accesstoken is used for the new client- Returns:
- a new Facebookclient
-
fetchObjects
public <T> T fetchObjects(List<String> ids, Class<T> objectType, Parameter... parameters)
Description copied from interface:FacebookClient
Fetches multiple Graph API objects in a single call, mapping the results to an instance ofobjectType
.You'll need to write your own container type (
objectType
) to hold the results. See http://restfb.com for an example of how to do this.- Specified by:
fetchObjects
in interfaceFacebookClient
- Type Parameters:
T
- Java type to map to.- Parameters:
ids
- IDs of the objects to fetch, e.g."me", "arjun"
.objectType
- Object type token.parameters
- URL parameters to include in the API call (optional).- Returns:
- An instance of type
objectType
which contains the requested objects' data. - See Also:
FacebookClient.fetchObjects(java.util.List, java.lang.Class, com.restfb.Parameter[])
-
publish
public <T> T publish(String connection, Class<T> objectType, List<BinaryAttachment> binaryAttachments, Parameter... parameters)
Description copied from interface:FacebookClient
Performs a Graph API publish operation on the givenconnection
and includes some files - photos, for example - in the publish request, and mapping the result to an instance ofobjectType
.- Specified by:
publish
in interfaceFacebookClient
- Type Parameters:
T
- Java type to map to.- Parameters:
connection
- The Connection to publish to.objectType
- Object type token.binaryAttachments
- The files to include in the publish request.parameters
- URL parameters to include in the API call.- Returns:
- An instance of type
objectType
which contains the Facebook response to your publish request. - See Also:
FacebookClient.publish(java.lang.String, java.lang.Class, com.restfb.BinaryAttachment, com.restfb.Parameter[])
-
publish
public <T> T publish(String connection, Class<T> objectType, BinaryAttachment binaryAttachment, Parameter... parameters)
Description copied from interface:FacebookClient
Performs a Graph API publish operation on the givenconnection
and includes a file - a photo, for example - in the publish request, and mapping the result to an instance ofobjectType
.- Specified by:
publish
in interfaceFacebookClient
- Type Parameters:
T
- Java type to map to.- Parameters:
connection
- The Connection to publish to.objectType
- Object type token.binaryAttachment
- The file to include in the publish request.parameters
- URL parameters to include in the API call.- Returns:
- An instance of type
objectType
which contains the Facebook response to your publish request. - See Also:
FacebookClient.publish(java.lang.String, java.lang.Class, com.restfb.BinaryAttachment, com.restfb.Parameter[])
-
publish
public <T> T publish(String connection, Class<T> objectType, Parameter... parameters)
Description copied from interface:FacebookClient
Performs a Graph API publish operation on the givenconnection
, mapping the result to an instance ofobjectType
.- Specified by:
publish
in interfaceFacebookClient
- Type Parameters:
T
- Java type to map to.- Parameters:
connection
- The Connection to publish to.objectType
- Object type token.parameters
- URL parameters to include in the API call.- Returns:
- An instance of type
objectType
which contains the Facebook response to your publish request. - See Also:
FacebookClient.publish(java.lang.String, java.lang.Class, com.restfb.Parameter[])
-
publish
public <T> T publish(String connection, Class<T> objectType, Body body, Parameter... parameters)
Description copied from interface:FacebookClient
Performs a Graph API publish operation on the givenconnection
and includes special body in the publish request, and mapping the result to an instance ofobjectType
.- Specified by:
publish
in interfaceFacebookClient
- Type Parameters:
T
- Java type to map to.- Parameters:
connection
- The Connection to publish to.objectType
- Object type token.body
- The body used in the POST request.parameters
- URL parameters to include in the API call.- Returns:
- An instance of type
objectType
which contains the Facebook response to your publish request.
-
getLogoutUrl
public String getLogoutUrl(String next)
Description copied from interface:FacebookClient
generates an logout url- Specified by:
getLogoutUrl
in interfaceFacebookClient
- Parameters:
next
- may be null, url the webpage should redirect after logout- Returns:
- the logout url
-
executeBatch
public List<BatchResponse> executeBatch(BatchRequest... batchRequests)
Description copied from interface:FacebookClient
Executes operations as a batch using the Batch API.- Specified by:
executeBatch
in interfaceFacebookClient
- Parameters:
batchRequests
- The operations to execute.- Returns:
- The execution results in the order in which the requests were specified.
- See Also:
FacebookClient.executeBatch(com.restfb.batch.BatchRequest[])
-
executeBatch
public List<BatchResponse> executeBatch(List<BatchRequest> batchRequests)
Description copied from interface:FacebookClient
Executes operations as a batch using the Batch API.- Specified by:
executeBatch
in interfaceFacebookClient
- Parameters:
batchRequests
- The operations to execute.- Returns:
- The execution results in the order in which the requests were specified.
- See Also:
FacebookClient.executeBatch(java.util.List)
-
executeBatch
public List<BatchResponse> executeBatch(List<BatchRequest> batchRequests, List<BinaryAttachment> binaryAttachments)
Description copied from interface:FacebookClient
Executes operations as a batch with binary attachments using the Batch API.- Specified by:
executeBatch
in interfaceFacebookClient
- Parameters:
batchRequests
- The operations to execute.binaryAttachments
- Binary attachments referenced by the batch requests.- Returns:
- The execution results in the order in which the requests were specified.
- See Also:
FacebookClient.executeBatch(java.util.List, java.util.List)
-
convertSessionKeysToAccessTokens
public List<AccessToken> convertSessionKeysToAccessTokens(String appId, String secretKey, String... sessionKeys)
Description copied from interface:FacebookClient
Converts an arbitrary number ofsessionKeys
to OAuth access tokens.See the Facebook Platform Upgrade Guide for details on how this process works and why you should convert your application's session keys if you haven't already.
- Specified by:
convertSessionKeysToAccessTokens
in interfaceFacebookClient
- Parameters:
appId
- A Facebook application ID.secretKey
- A Facebook application secret key.sessionKeys
- The Old REST API session keys to be converted to OAuth access tokens.- Returns:
- A list of access tokens ordered to correspond to the
sessionKeys
argument list. - See Also:
FacebookClient.convertSessionKeysToAccessTokens(java.lang.String, java.lang.String, java.lang.String[])
-
obtainAppAccessToken
public AccessToken obtainAppAccessToken(String appId, String appSecret)
Description copied from interface:FacebookClient
Obtains an access token which can be used to perform Graph API operations on behalf of an application instead of a user.- Specified by:
obtainAppAccessToken
in interfaceFacebookClient
- Parameters:
appId
- The ID of the app for which you'd like to obtain an access token.appSecret
- The secret for the app for which you'd like to obtain an access token.- Returns:
- The access token for the application identified by
appId
andappSecret
. - See Also:
FacebookClient.obtainAppAccessToken(java.lang.String, java.lang.String)
-
fetchDeviceCode
public DeviceCode fetchDeviceCode(ScopeBuilder scope)
Description copied from interface:FacebookClient
Method to initialize the device access token generation.You receive a
DeviceCode
instance and have to show the user theDeviceCode.getVerificationUri()
and theDeviceCode.getUserCode()
. The user have to enter the user code at the verification url.Save the
DeviceCode.getCode()
to use it later, when polling Facebook with theFacebookClient.obtainDeviceAccessToken(java.lang.String)
method.- Specified by:
fetchDeviceCode
in interfaceFacebookClient
- Parameters:
scope
- List of Permissions to request from the person using your app.- Returns:
- Instance of
DeviceCode
including the information to obtain the Device access token
-
obtainDeviceAccessToken
public AccessToken obtainDeviceAccessToken(String code) throws FacebookDeviceTokenCodeExpiredException, FacebookDeviceTokenPendingException, FacebookDeviceTokenDeclinedException, FacebookDeviceTokenSlowdownException
Description copied from interface:FacebookClient
Method to poll Facebook and fetch the Device Access Token.You have to use this method to check if the user confirms the authorization.
FacebookOAuthException
can be thrown if the authorization is declined or still pending.- Specified by:
obtainDeviceAccessToken
in interfaceFacebookClient
- Parameters:
code
- The device- Returns:
- An extended access token for the given
AccessToken
. - Throws:
FacebookDeviceTokenCodeExpiredException
- theDeviceCode.getCode()
is expired, please fetch a newDeviceCode
.FacebookDeviceTokenPendingException
- the user has not finished the authorisation process, yet. Please poll again later.FacebookDeviceTokenDeclinedException
- the user declined the authorisation. You have to handle this problem.FacebookDeviceTokenSlowdownException
- you tried too often to fetch the device access token. You have to use a larger interval
-
obtainUserAccessToken
public AccessToken obtainUserAccessToken(String appId, String appSecret, String redirectUri, String verificationCode)
Description copied from interface:FacebookClient
Obtains an access token which can be used to perform Graph API operations on behalf of a user.See Access Tokens.
- Specified by:
obtainUserAccessToken
in interfaceFacebookClient
- Parameters:
appId
- The ID of the app for which you'd like to obtain an access token.appSecret
- The secret for the app for which you'd like to obtain an access token.redirectUri
- The redirect URI which was used to obtain theverificationCode
.verificationCode
- The verification code in the Graph API callback to the redirect URI.- Returns:
- The access token for the user identified by
appId
,appSecret
,redirectUri
andverificationCode
. - See Also:
FacebookClient.obtainUserAccessToken(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
-
obtainExtendedAccessToken
public AccessToken obtainExtendedAccessToken(String appId, String appSecret)
Description copied from interface:FacebookClient
Convenience method which invokesFacebookClient.obtainExtendedAccessToken(String, String, String)
with the current access token.- Specified by:
obtainExtendedAccessToken
in interfaceFacebookClient
- Parameters:
appId
- The ID of the app for which you'd like to obtain an extended access token.appSecret
- The secret for the app for which you'd like to obtain an extended access token.- Returns:
- An extended access token for the given
accessToken
. - See Also:
FacebookClient.obtainExtendedAccessToken(java.lang.String, java.lang.String)
-
obtainRefreshedExtendedAccessToken
public AccessToken obtainRefreshedExtendedAccessToken()
Description copied from interface:FacebookClient
Obtain a refreshed Instagram extended access token.This method is used to refresh an existing Instagram extended access token. Extended access tokens expire after a certain period of time, and this method allows you to obtain a new one using the refresh token provided with the original extended access token.
- Specified by:
obtainRefreshedExtendedAccessToken
in interfaceFacebookClient
- Returns:
- A new
AccessToken
object containing the refreshed access token, expiration time, and token type.
-
obtainExtendedAccessToken
public AccessToken obtainExtendedAccessToken(String appId, String appSecret, String accessToken)
Description copied from interface:FacebookClient
Obtains an extended access token for the given existing, non-expired, short-lived access_token.- Specified by:
obtainExtendedAccessToken
in interfaceFacebookClient
- Parameters:
appId
- The ID of the app for which you'd like to obtain an extended access token.appSecret
- The secret for the app for which you'd like to obtain an extended access token.accessToken
- The non-expired, short-lived access token to extend.- Returns:
- An extended access token for the given
accessToken
. - See Also:
FacebookClient.obtainExtendedAccessToken(java.lang.String, java.lang.String, java.lang.String)
-
getAccessTokenFromResponse
protected AccessToken getAccessTokenFromResponse(String response)
-
parseSignedRequest
public <T> T parseSignedRequest(String signedRequest, String appSecret, Class<T> objectType)
Description copied from interface:FacebookClient
Parses a signed request and verifies it against your App Secret.- Specified by:
parseSignedRequest
in interfaceFacebookClient
- Type Parameters:
T
- class of objectType- Parameters:
signedRequest
- The signed request to parse.appSecret
- The secret for the app that can read this signed request.objectType
- Object type token.- Returns:
- An instance of type
objectType
which contains the decoded object embedded withinsignedRequest
.
-
urlDecodeSignedRequestToken
protected String urlDecodeSignedRequestToken(String signedRequestToken)
Decodes a component of a signed request received from Facebook using FB's special URL-encoding strategy.- Parameters:
signedRequestToken
- Token to decode.- Returns:
- The decoded token.
-
getLoginDialogUrl
public String getLoginDialogUrl(String appId, String redirectUri, ScopeBuilder scope, String state, Parameter... parameters)
Description copied from interface:FacebookClient
generates the login dialog url- Specified by:
getLoginDialogUrl
in interfaceFacebookClient
- Parameters:
appId
- The ID of your app, found in your app's dashboard.redirectUri
- The URL that you want to redirect the person logging in back to. This URL will capture the response from the Login Dialog. If you are using this in a webview within a desktop app, this must be set tohttps://www.facebook.com/connect/login_success.html
.scope
- List of Permissions to request from the person using your app.state
- The state parameter is used to prevent CSRF attacks.parameters
- List of additional parameters- Returns:
- the login dialog url
-
getLoginDialogUrl
public String getLoginDialogUrl(String appId, String redirectUri, ScopeBuilder scope, Parameter... parameters)
Description copied from interface:FacebookClient
generates the login dialog url- Specified by:
getLoginDialogUrl
in interfaceFacebookClient
- Parameters:
appId
- The ID of your app, found in your app's dashboard.redirectUri
- The URL that you want to redirect the person logging in back to. This URL will capture the response from the Login Dialog. If you are using this in a webview within a desktop app, this must be set tohttps://www.facebook.com/connect/login_success.html
.scope
- List of Permissions to request from the person using your app.parameters
- List of additional parameters- Returns:
- the login dialog url
-
getGenericLoginDialogUrl
protected String getGenericLoginDialogUrl(String appId, String redirectUri, ScopeBuilder scope, Supplier<String> endpointSupplier, String state, List<Parameter> parameters)
-
verifySignedRequest
protected boolean verifySignedRequest(String appSecret, String algorithm, String encodedPayload, byte[] signature)
Verifies that the signed request is really from Facebook.- Parameters:
appSecret
- The secret for the app that can verify this signed request.algorithm
- Signature algorithm specified by FB in the decoded payload.encodedPayload
- The encoded payload used to generate a signature for comparison against the providedsignature
.signature
- The decoded signature extracted from the signed request. Compared against a signature generated fromencodedPayload
.- Returns:
true
if the signed request is verified,false
if not.
-
debugToken
public DebugTokenInfo debugToken(String inputToken)
Description copied from interface:FacebookClient
When working with access tokens, you may need to check what information is associated with them, such as its user or expiry. To get this information you can use the debug tool in the developer site, or you can use this function.
You must instantiate your FacebookClient using your App Access Token, or a valid User Access Token from a developer of the app.
Note that if your app is set to Native/Desktop in the Advanced settings of your App Dashboard, the underlying GraphAPI endpoint will not work with your app token unless you change the "App Secret in Client" setting to NO. If you do not see this setting, make sure your "App Type" is set to Native/Desktop and then press the save button at the bottom of the page. This will not affect apps set to Web.
The response of the API call is a JSON array containing data and a map of fields. For example:
{ "data": { "app_id": 138483919580948, "application": "Social Cafe", "expires_at": 1352419328, "is_valid": true, "issued_at": 1347235328, "metadata": { "sso": "iphone-safari" }, "scopes": [ "email", "publish_actions" ], "user_id": 1207059 } }
Note that the
issued_at
field is not returned for short-lived access tokens.- Specified by:
debugToken
in interfaceFacebookClient
- Parameters:
inputToken
- The Access Token to debug.- Returns:
- A JsonObject containing the debug information for the accessToken.
- See Also:
FacebookClient.debugToken(java.lang.String)
-
getJsonMapper
public JsonMapper getJsonMapper()
Description copied from interface:FacebookClient
Gets theJsonMapper
used to convert Facebook JSON to Java objects.- Specified by:
getJsonMapper
in interfaceFacebookClient
- Returns:
- The
JsonMapper
used to convert Facebook JSON to Java objects. - See Also:
FacebookClient.getJsonMapper()
-
getWebRequestor
public WebRequestor getWebRequestor()
Description copied from interface:FacebookClient
Gets theWebRequestor
used to talk to the Facebook API endpoints.- Specified by:
getWebRequestor
in interfaceFacebookClient
- Returns:
- The
WebRequestor
used to talk to the Facebook API endpoints. - See Also:
FacebookClient.getWebRequestor()
-
makeRequest
protected String makeRequest(String endpoint, Parameter... parameters)
Coordinates the process of executing the API request GET/POST and processing the response we receive from the endpoint.- Parameters:
endpoint
- Facebook Graph API endpoint.parameters
- Arbitrary number of parameters to send along to Facebook as part of the API call.- Returns:
- The JSON returned by Facebook for the API call.
- Throws:
FacebookException
- If an error occurs while making the Facebook API POST or processing the response.
-
makeRequest
protected String makeRequest(String endpoint, boolean executeAsPost, boolean executeAsDelete, List<BinaryAttachment> binaryAttachments, Parameter... parameters)
-
makeRequest
protected String makeRequest(String endpoint, boolean executeAsPost, boolean executeAsDelete, List<BinaryAttachment> binaryAttachments, Body body, Parameter... parameters)
Coordinates the process of executing the API request GET/POST and processing the response we receive from the endpoint.- Parameters:
endpoint
- Facebook Graph API endpoint.executeAsPost
-true
to execute the web request as aPOST
,false
to execute as aGET
.executeAsDelete
-true
to add a special 'treat this request as aDELETE
' parameter.binaryAttachments
- A list of binary files to include in aPOST
request. Passnull
if no attachment should be sent.parameters
- Arbitrary number of parameters to send along to Facebook as part of the API call.- Returns:
- The JSON returned by Facebook for the API call.
- Throws:
FacebookException
- If an error occurs while making the Facebook API POST or processing the response.
-
obtainAppSecretProof
public String obtainAppSecretProof(String accessToken, String appSecret)
Description copied from interface:FacebookClient
Generates anappsecret_proof
value.See Facebook's 'securing requests' documentation for more info.
- Specified by:
obtainAppSecretProof
in interfaceFacebookClient
- Parameters:
accessToken
- The access token required to generate theappsecret_proof
value.appSecret
- The secret for the app for which you'd like to generate theappsecret_proof
value.- Returns:
- A hex-encoded SHA256 hash as a
String
. - See Also:
FacebookClient.obtainAppSecretProof(java.lang.String, java.lang.String)
-
isHttpDeleteFallback
public boolean isHttpDeleteFallback()
returns if the fallback post method (true
) is used or the http delete (false
)- Returns:
true
if POST is used instead of HTTP DELETE (default)
-
setHttpDeleteFallback
public void setHttpDeleteFallback(boolean httpDeleteFallback)
Set totrue
if the facebook http delete fallback should be used. Facebook allows to use the http POST with the parameter "method=delete" to override the post and use delete instead. This feature allow http client that do not support the whole http method set, to delete objects from facebook- Parameters:
httpDeleteFallback
-true
if the http Delete Fallback is used
-
makeRequestAndProcessResponse
protected String makeRequestAndProcessResponse(DefaultFacebookClient.Requestor requestor)
-
toParameterString
protected String toParameterString(Parameter... parameters)
Generate the parameter string to be included in the Facebook API request.- Parameters:
parameters
- Arbitrary number of extra parameters to include in the request.- Returns:
- The parameter string to include in the Facebook API request.
- Throws:
FacebookJsonMappingException
- If an error occurs when building the parameter string.
-
toParameterString
protected String toParameterString(boolean withJsonParameter, Parameter... parameters)
Generate the parameter string to be included in the Facebook API request.- Parameters:
withJsonParameter
- add additional parameter format with type jsonparameters
- Arbitrary number of extra parameters to include in the request.- Returns:
- The parameter string to include in the Facebook API request.
- Throws:
FacebookJsonMappingException
- If an error occurs when building the parameter string.
-
createEndpointForApiCall
protected String createEndpointForApiCall(String apiCall, boolean hasAttachment, boolean hasReel)
Given an api call (e.g. "me" or "fql.query"), returns the correct FB API endpoint to use.Useful for returning the read-only API endpoint where possible.
- Parameters:
apiCall
- The FB API call (Graph or Old REST API) for which we'd like an endpoint.hasAttachment
- Are we including a multipart file when making this API call?hasReel
- Is the binary attachment used as Facebook reel?- Returns:
- An absolute endpoint URL to communicate with.
- See Also:
BaseFacebookClient.createEndpointForApiCall(java.lang.String,boolean,boolean)
-
createBaseUrlForEndpoint
protected String createBaseUrlForEndpoint(String apiCall, boolean hasAttachment, boolean hasReel)
-
getFacebookGraphEndpointUrl
protected String getFacebookGraphEndpointUrl()
Returns the base endpoint URL for the Graph API.- Returns:
- The base endpoint URL for the Graph API.
-
getFacebookGraphVideoEndpointUrl
protected String getFacebookGraphVideoEndpointUrl()
Returns the base endpoint URL for the Graph APIs video upload functionality.- Returns:
- The base endpoint URL for the Graph APIs video upload functionality.
- Since:
- 1.6.5
-
getFacebookReelsUploadEndpointUrl
protected String getFacebookReelsUploadEndpointUrl()
Returns the Facebook Reels Upload endpoint URL for handling the Reels Upload- Returns:
- the Facebook Reels Upload endpoint URL
-
getFacebookEndpointUrls
public FacebookEndpoints getFacebookEndpointUrls()
-
setFacebookEndpointUrls
public void setFacebookEndpointUrls(FacebookEndpoints facebookEndpointUrls)
-
parametersWithAdditionalParameter
protected Parameter[] parametersWithAdditionalParameter(Parameter parameter, Parameter... parameters)
Appends the givenparameter
to the givenparameters
array.- Parameters:
parameter
- The parameter value to append.parameters
- The parameters to which the givenparameter
is appended.- Returns:
- A new array which contains both
parameter
andparameters
.
-
urlEncodedValueForParameterName
protected String urlEncodedValueForParameterName(String name, String value)
Gets the URL-encoded version of the givenvalue
for the parameter namedname
.Includes special-case handling for access token parameters where we check if the token is already URL-encoded - if so, we don't encode again. All other parameter types are always URL-encoded.
- Parameters:
name
- The name of the parameter whose value should be URL-encoded and returned.value
- The value of the parameter which should be URL-encoded and returned.- Returns:
- The URL-encoded version of the given
value
.
-
verifyParameterLegality
protected void verifyParameterLegality(Parameter... parameters)
Verifies that the provided parameter names don't collide with the ones we internally pass along to Facebook.- Parameters:
parameters
- The parameters to check.- Throws:
IllegalArgumentException
- If there's a parameter name collision.
-
-