Interface FacebookClient
-
- All Known Implementing Classes:
DefaultFacebookClient
,DefaultInstagramClient
,DefaultInstagramLoginClient
,DefaultThreadsClient
public interface FacebookClient
Specifies how a Facebook Graph API client must operate.If you'd like to...
- Fetch an object: use
fetchObject(String, Class, Parameter...)
orfetchObjects(List, Class, Parameter...)
- Fetch a connection: use
fetchConnection(String, Class, Parameter...)
- Execute operations in batch: use
executeBatch(BatchRequest...)
orexecuteBatch(List, List)
- Publish data: use
publish(String, Class, Parameter...)
orpublish(String, Class, BinaryAttachment, Parameter...)
- Delete an object: use
deleteObject(String, Parameter...)
You may also perform some common access token operations. If you'd like to...
- Extend the life of an access token: use
obtainExtendedAccessToken(String, String, String)
- Obtain an access token for use on behalf of an application instead of a user, use
obtainAppAccessToken(String, String)
. - Convert old-style session keys to OAuth access tokens: use
convertSessionKeysToAccessTokens(String, String, String...)
- Verify and extract data from a signed request: use
parseSignedRequest(String, String, Class)
- Author:
- Mark Allen, Scott Hernandez, Mattia Tommasone, Chris Petersen, Josef Gierbl, Broc Seib
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description List<AccessToken>
convertSessionKeysToAccessTokens(String appId, String secretKey, String... sessionKeys)
Converts an arbitrary number ofsessionKeys
to OAuth access tokens.FacebookClient
createClientWithAccessToken(String accessToken)
creates a newFacebookClient
from an old one.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
.JsonMapper
getJsonMapper()
Gets theJsonMapper
used to convert Facebook JSON to Java objects.String
getLoginDialogUrl(String appId, String redirectUri, ScopeBuilder scope, Parameter... additionalParameters)
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.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 invokesobtainExtendedAccessToken(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.<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
.
-
-
-
Method Detail
-
fetchObject
<T> T fetchObject(String object, Class<T> objectType, Parameter... parameters)
Fetches a single Graph API object, mapping the result to an instance ofobjectType
.- 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. - Throws:
FacebookException
- If an error occurs while performing the API call.
-
createClientWithAccessToken
FacebookClient createClientWithAccessToken(String accessToken)
creates a newFacebookClient
from an old one.App secret and api version are taken from the original client.
- Parameters:
accessToken
- this accesstoken is used for the new client- Returns:
- a new Facebookclient
-
fetchObjects
<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
.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.- 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. - Throws:
FacebookException
- If an error occurs while performing the API call.
-
fetchConnection
<T> Connection<T> fetchConnection(String connection, Class<T> connectionType, Parameter... parameters)
Fetches a Graph APIConnection
type, mapping the result to an instance ofconnectionType
.- 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. - Throws:
FacebookException
- If an error occurs while performing the API call.
-
fetchConnectionPage
<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
.- 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. - Throws:
FacebookException
- If an error occurs while performing the API call.
-
executeBatch
List<BatchResponse> executeBatch(BatchRequest... batchRequests)
Executes operations as a batch using the Batch API.- Parameters:
batchRequests
- The operations to execute.- Returns:
- The execution results in the order in which the requests were specified.
-
executeBatch
List<BatchResponse> executeBatch(List<BatchRequest> batchRequests)
Executes operations as a batch using the Batch API.- Parameters:
batchRequests
- The operations to execute.- Returns:
- The execution results in the order in which the requests were specified.
-
executeBatch
List<BatchResponse> executeBatch(List<BatchRequest> batchRequests, List<BinaryAttachment> binaryAttachments)
Executes operations as a batch with binary attachments using the Batch API.- 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.
- Since:
- 1.6.5
-
publish
<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
.- 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. - Throws:
FacebookException
- If an error occurs while performing the API call.
-
publish
<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
.- 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. - Throws:
FacebookException
- If an error occurs while performing the API call.
-
publish
<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
.- 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. - Throws:
FacebookException
- If an error occurs while performing the API call.
-
publish
<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
.- 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. - Throws:
FacebookException
- If an error occurs while performing the API call.
-
deleteObject
boolean deleteObject(String object, Parameter... parameters)
Performs a Graph API delete operation on the givenobject
.- 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.- Throws:
FacebookException
- If an error occurred while attempting to delete the object.
-
convertSessionKeysToAccessTokens
List<AccessToken> convertSessionKeysToAccessTokens(String appId, String secretKey, String... sessionKeys)
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.
- 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. - Throws:
FacebookException
- If an error occurs while attempting to convert the session keys to API keys.- Since:
- 1.6
-
obtainUserAccessToken
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.See Access Tokens.
- 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
. - Throws:
FacebookException
- If an error occurs while attempting to obtain an access token.- Since:
- 1.8.0
-
obtainAppAccessToken
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.- 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
. - Throws:
FacebookException
- If an error occurs while attempting to obtain an access token.- Since:
- 1.6.10
-
obtainExtendedAccessToken
AccessToken obtainExtendedAccessToken(String appId, String appSecret, String accessToken)
Obtains an extended access token for the given existing, non-expired, short-lived access_token.- 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
. - Throws:
FacebookException
- If an error occurs while attempting to obtain an extended access token.- Since:
- 1.6.10
-
obtainAppSecretProof
String obtainAppSecretProof(String accessToken, String appSecret)
Generates anappsecret_proof
value.See Facebook's 'securing requests' documentation for more info.
- 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
. - Throws:
IllegalStateException
- If creating theappsecret_proof
fails.- Since:
- 1.6.13
-
obtainExtendedAccessToken
AccessToken obtainExtendedAccessToken(String appId, String appSecret)
Convenience method which invokesobtainExtendedAccessToken(String, String, String)
with the current access token.- 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
. - Throws:
FacebookException
- If an error occurs while attempting to obtain an extended access token.IllegalStateException
- If this instance was not constructed with an access token.- Since:
- 1.6.10
-
obtainRefreshedExtendedAccessToken
AccessToken obtainRefreshedExtendedAccessToken()
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.
- Returns:
- A new
AccessToken
object containing the refreshed access token, expiration time, and token type. - Throws:
FacebookResponseContentException
- If the response from the Facebook API cannot be parsed or if the access token cannot be extracted from the response.
-
parseSignedRequest
<T> T parseSignedRequest(String signedRequest, String appSecret, Class<T> objectType)
Parses a signed request and verifies it against your App Secret.- 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
. - Throws:
FacebookSignedRequestParsingException
- If an error occurs while trying to processsignedRequest
.FacebookSignedRequestVerificationException
- IfsignedRequest
fails verification againstappSecret
.- Since:
- 1.6.13
-
fetchDeviceCode
DeviceCode fetchDeviceCode(ScopeBuilder scope)
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 theobtainDeviceAccessToken(java.lang.String)
method.- 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
AccessToken obtainDeviceAccessToken(String code) throws FacebookDeviceTokenCodeExpiredException, FacebookDeviceTokenPendingException, FacebookDeviceTokenDeclinedException, FacebookDeviceTokenSlowdownException
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.- 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- Since:
- 1.12.0
-
debugToken
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. 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.- Parameters:
inputToken
- The Access Token to debug.- Returns:
- A JsonObject containing the debug information for the accessToken.
- Since:
- 1.6.13
-
getJsonMapper
JsonMapper getJsonMapper()
Gets theJsonMapper
used to convert Facebook JSON to Java objects.- Returns:
- The
JsonMapper
used to convert Facebook JSON to Java objects. - Since:
- 1.6.7
-
getWebRequestor
WebRequestor getWebRequestor()
Gets theWebRequestor
used to talk to the Facebook API endpoints.- Returns:
- The
WebRequestor
used to talk to the Facebook API endpoints. - Since:
- 1.6.7
-
getLogoutUrl
String getLogoutUrl(String next)
generates an logout url- Parameters:
next
- may be null, url the webpage should redirect after logout- Returns:
- the logout url
- Since:
- 1.9.0
-
getLoginDialogUrl
String getLoginDialogUrl(String appId, String redirectUri, ScopeBuilder scope, String state, Parameter... parameters)
generates the login dialog url- 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
- Since:
- 1.9.0
-
getLoginDialogUrl
String getLoginDialogUrl(String appId, String redirectUri, ScopeBuilder scope, Parameter... additionalParameters)
generates the login dialog url- 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.additionalParameters
- List of additional parameters- Returns:
- the login dialog url
- Since:
- 1.9.0
-
-