public class DefaultLegacyFacebookClient extends Object implements LegacyFacebookClient
Modifier and Type | Field and Description |
---|---|
protected static String |
ACCESS_TOKEN_PARAM_NAME
Reserved access token parameter name.
|
protected String |
accessToken
OAuth Access token.
|
protected static String |
API_KEY_PARAM_NAME |
protected String |
apiKey
Facebook API key.
|
protected static String |
CALL_ID_PARAM_NAME |
protected static String |
FACEBOOK_REST_ENDPOINT_URL
API endpoint URL.
|
protected static String |
FACEBOOK_REST_OAUTH_ENDPOINT_URL
OAuth API endpoint URL.
|
protected static String |
FORMAT_PARAM_NAME |
protected static String |
FORMAT_PARAM_VALUE |
protected Set<String> |
illegalParamNames
Set of parameter names that user must not specify themselves, since we use
these parameters internally.
|
protected JsonMapper |
jsonMapper
Handles mapping Facebook response JSON to Java objects.
|
protected static String |
LEGACY_ERROR_CODE_ATTRIBUTE_NAME
Legacy API error response 'error_code' attribute name.
|
protected static String |
LEGACY_ERROR_MSG_ATTRIBUTE_NAME
Legacy API error response 'error_msg' attribute name.
|
protected Logger |
logger
Logger.
|
protected static String |
METHOD_PARAM_NAME |
protected String |
secretKey
Facebook application secret key.
|
protected static String |
SESSION_KEY_PARAM_NAME |
protected static String |
SIG_PARAM_NAME |
protected static String |
VERSION_PARAM_NAME |
protected static String |
VERSION_PARAM_VALUE |
protected WebRequestor |
webRequestor
Handles
GET s and POST s to the Facebook API endpoint. |
Constructor and Description |
---|
DefaultLegacyFacebookClient(String accessToken)
Creates a Facebook API client with the given OAuth
access token.
|
DefaultLegacyFacebookClient(String apiKey,
String secretKey)
Deprecated.
You should use
DefaultLegacyFacebookClient(String)
instead. Facebook is moving to OAuth and will stop supporting
the old authentication scheme soon. |
DefaultLegacyFacebookClient(String apiKey,
String secretKey,
WebRequestor webRequestor,
JsonMapper jsonMapper)
Deprecated.
You should use
DefaultLegacyFacebookClient(String, WebRequestor, JsonMapper)
instead. Facebook is moving to OAuth and will stop supporting
the old authentication scheme soon. |
DefaultLegacyFacebookClient(String accessToken,
WebRequestor webRequestor,
JsonMapper jsonMapper)
Creates a Facebook API client with the given OAuth
access token.
|
Modifier and Type | Method and Description |
---|---|
<T> T |
execute(String method,
Class<T> resultType,
Parameter... parameters)
Executes a Facebook API method with the given
parameters , mapping
the API response to a single instance of type resultType . |
void |
execute(String method,
Parameter... parameters)
Executes a Facebook API method with the given
parameters , ignoring
the response. |
<T> T |
execute(String method,
String sessionKey,
Class<T> resultType,
Parameter... parameters)
Executes a Facebook API method with the given
parameters , mapping
the API response to a single instance of type resultType . |
void |
execute(String method,
String sessionKey,
Parameter... parameters)
Executes a Facebook API method with the given
parameters , ignoring
the response. |
<T> List<T> |
executeForList(String method,
Class<T> resultType,
Parameter... parameters)
Executes a Facebook API method with the given
parameters , mapping
the API response to a List of instances of type resultType . |
<T> List<T> |
executeForList(String method,
String sessionKey,
Class<T> resultType,
Parameter... parameters)
Executes a Facebook API method with the given
parameters , mapping
the API response to a List of instances of type resultType . |
<T> T |
executeMultiquery(Map<String,String> queries,
Class<T> resultType,
Parameter... additionalParameters)
Executes the
fql.multiquery API call, mapping the API response to a single
instance of type resultType . |
<T> T |
executeMultiquery(Map<String,String> queries,
String sessionKey,
Class<T> resultType,
Parameter... additionalParameters)
Executes the
fql.multiquery API call, mapping the API response to a single
instance of type resultType . |
protected String |
generateMd5(String string)
Generate an MD5 hash of the given
string . |
protected String |
generateSignature(Map<String,String> sortedParameters)
Given a sorted map of parameter names to values, calculate and return the
Facebook API signature as defined by
http://wiki.developers.facebook.com/index.php/Verifying_The_Signature.
|
protected String |
getFacebookRestEndpointUrl()
Returns the base endpoint URL for the Old REST API.
|
protected String |
getFacebookRestOauthEndpointUrl()
Returns the base endpoint URL for the Old REST OAuth API.
|
protected void |
initializeIllegalParamNames()
Initializes the set of illegal URL parameter names.
|
protected String |
makeRequest(String method,
String sessionKey,
Parameter... parameters)
Coordinates the process of verifying and transforming API parameters,
executing the API POST, and processing the response we receive from the
endpoint.
|
protected Parameter[] |
parametersWithAdditionalParameter(Parameter parameter,
Parameter... parameters)
Appends the given
parameter to the given parameters array. |
protected String |
queriesToJson(Map<String,String> queries)
Given a map of query names to queries, verify that it contains valid data
and convert it to a JSON object string.
|
protected void |
throwLegacyFacebookResponseStatusExceptionIfNecessary(String json)
If the
error_code JSON field is present, we've got a response
status error for this API call. |
protected String |
toParameterString(String method,
String sessionKey,
Parameter... parameters)
Given basic request information, generate the parameter string to be
included in the Facebook API POST.
|
protected String |
urlEncodedValueForParameterName(String name,
String value)
Gets the URL-encoded version of the given
value for the parameter
named name . |
protected boolean |
usesAccessTokenAuthentication()
Are we using OAuth access token authentication?
|
protected void |
verifyParameterLegality(Parameter... parameters)
Verifies that the provided parameter names don't collide with the ones we
internally pass along to Facebook.
|
protected void |
verifyParameterPresence(String parameterName,
Object parameter)
Ensures that
parameter isn't null . |
protected void |
verifyParameterPresence(String parameterName,
String parameter)
Ensures that
parameter isn't null or an empty string. |
protected String apiKey
protected String secretKey
protected String accessToken
protected static final String FACEBOOK_REST_ENDPOINT_URL
protected static final String FACEBOOK_REST_OAUTH_ENDPOINT_URL
protected static final String METHOD_PARAM_NAME
protected static final String FORMAT_PARAM_NAME
protected static final String FORMAT_PARAM_VALUE
protected static final String API_KEY_PARAM_NAME
protected static final String CALL_ID_PARAM_NAME
protected static final String SIG_PARAM_NAME
protected static final String SESSION_KEY_PARAM_NAME
protected static final String VERSION_PARAM_NAME
protected static final String VERSION_PARAM_VALUE
protected WebRequestor webRequestor
GET
s and POST
s to the Facebook API endpoint.protected JsonMapper jsonMapper
protected final Set<String> illegalParamNames
protected static final String LEGACY_ERROR_CODE_ATTRIBUTE_NAME
protected static final String LEGACY_ERROR_MSG_ATTRIBUTE_NAME
protected static final String ACCESS_TOKEN_PARAM_NAME
protected final Logger logger
public DefaultLegacyFacebookClient(String accessToken)
accessToken
- An OAuth access token.NullPointerException
- If accessToken
is null
.IllegalArgumentException
- If accessToken
is a blank string.@Deprecated public DefaultLegacyFacebookClient(String apiKey, String secretKey)
DefaultLegacyFacebookClient(String)
instead. Facebook is moving to OAuth and will stop supporting
the old authentication scheme soon.apiKey
- A Facebook API key.secretKey
- A Facebook application secret key.NullPointerException
- If either parameter is null
.IllegalArgumentException
- If either parameter is a blank string.@Deprecated public DefaultLegacyFacebookClient(String apiKey, String secretKey, WebRequestor webRequestor, JsonMapper jsonMapper)
DefaultLegacyFacebookClient(String, WebRequestor, JsonMapper)
instead. Facebook is moving to OAuth and will stop supporting
the old authentication scheme soon.webRequestor
, and jsonMapper
(Legacy authentication).apiKey
- A Facebook API key.secretKey
- A Facebook application secret key.webRequestor
- The WebRequestor
implementation to use for POST
ing
to the API endpoint.jsonMapper
- The JsonMapper
implementation to use for mapping API
response JSON to Java objects.NullPointerException
- If any parameter is null
.IllegalArgumentException
- If either apiKey
or secretKey
is a blank string.public DefaultLegacyFacebookClient(String accessToken, WebRequestor webRequestor, JsonMapper jsonMapper)
accessToken
- An OAuth access token.webRequestor
- The WebRequestor
implementation to use for POST
ing
to the API endpoint.jsonMapper
- The JsonMapper
implementation to use for mapping API
response JSON to Java objects.NullPointerException
- If any parameter is null
.IllegalArgumentException
- If accessToken
is a blank string.public void execute(String method, Parameter... parameters)
LegacyFacebookClient
parameters
, ignoring
the response.execute
in interface LegacyFacebookClient
method
- The Facebook API method to call, e.g. fql.query
.parameters
- Parameters to include in the API call.LegacyFacebookClient.execute(String,
com.restfb.Parameter[])
public void execute(String method, String sessionKey, Parameter... parameters)
LegacyFacebookClient
parameters
, ignoring
the response.execute
in interface LegacyFacebookClient
method
- The Facebook API method to call, e.g. fql.query
.sessionKey
- A Facebook API session key if you're using the legacy API
key/Secret key authentication scheme. Must be null
if
using OAuth access token authentication.parameters
- Parameters to include in the API call.LegacyFacebookClient.execute(String,
String, com.restfb.Parameter[])
public <T> T execute(String method, Class<T> resultType, Parameter... parameters)
LegacyFacebookClient
parameters
, mapping
the API response to a single instance of type resultType
.execute
in interface LegacyFacebookClient
T
- Java type to map to.method
- The Facebook API method to call, e.g. fql.query
.resultType
- Result type token.parameters
- Parameters to include in the API call.resultType
which contains API response
data.LegacyFacebookClient.execute(String,
Class, com.restfb.Parameter[])
public <T> T execute(String method, String sessionKey, Class<T> resultType, Parameter... parameters)
LegacyFacebookClient
parameters
, mapping
the API response to a single instance of type resultType
.execute
in interface LegacyFacebookClient
T
- Java type to map to.method
- The Facebook API method to call, e.g. fql.query
.sessionKey
- A Facebook API session key if you're using the legacy API
key/Secret key authentication scheme. Must be null
if
using OAuth access token authentication.resultType
- Result type token.parameters
- Parameters to include in the API call.resultType
which contains API response
data.LegacyFacebookClient.execute(String,
String, Class, com.restfb.Parameter[])
public <T> List<T> executeForList(String method, Class<T> resultType, Parameter... parameters)
LegacyFacebookClient
parameters
, mapping
the API response to a List
of instances of type resultType
.executeForList
in interface LegacyFacebookClient
T
- Java type to map to.method
- The Facebook API method to call, e.g. fql.query
.resultType
- Result type token.parameters
- Parameters to include in the API call.List
of instances of type resultType
which
contain API response data.LegacyFacebookClient.executeForList(String,
Class, com.restfb.Parameter[])
public <T> List<T> executeForList(String method, String sessionKey, Class<T> resultType, Parameter... parameters)
LegacyFacebookClient
parameters
, mapping
the API response to a List
of instances of type resultType
.executeForList
in interface LegacyFacebookClient
T
- Java type to map to.method
- The Facebook API method to call, e.g. fql.query
.sessionKey
- A Facebook API session key if you're using the legacy API
key/Secret key authentication scheme. Must be null
if
using OAuth access token authentication.resultType
- Result type token.parameters
- Parameters to include in the API call.List
of instances of type resultType
which
contain API response data.LegacyFacebookClient.executeForList(String,
String, Class, com.restfb.Parameter[])
public <T> T executeMultiquery(Map<String,String> queries, Class<T> resultType, Parameter... additionalParameters)
LegacyFacebookClient
fql.multiquery
API call, mapping the API response to a single
instance of type resultType
.
This method exists because the standard
LegacyFacebookClient.execute(String, Parameter...)
and
LegacyFacebookClient.executeForList(String, Class, Parameter...)
family of methods are
not expressive enough to handle fql.multiquery
in a non-verbose
way.executeMultiquery
in interface LegacyFacebookClient
T
- Java type to map to.queries
- A mapping of query names to queries. This is marshaled to JSON and
sent over the wire to the Facebook API endpoint as the
queries
parameter.resultType
- Result type token.additionalParameters
- Additional parameters to include in the API call.resultType
which contains API response
data.LegacyFacebookClient.executeMultiquery(java.util.Map,
Class, com.restfb.Parameter[])
public <T> T executeMultiquery(Map<String,String> queries, String sessionKey, Class<T> resultType, Parameter... additionalParameters)
LegacyFacebookClient
fql.multiquery
API call, mapping the API response to a single
instance of type resultType
.
This method exists because the standard
LegacyFacebookClient.execute(String, Parameter...)
and
LegacyFacebookClient.executeForList(String, Class, Parameter...)
family of methods are
not expressive enough to handle fql.multiquery
in a non-verbose
way.executeMultiquery
in interface LegacyFacebookClient
T
- Java type to map to.queries
- A mapping of query names to queries. This is marshaled to JSON and
sent over the wire to the Facebook API endpoint as the
queries
parameter.sessionKey
- A Facebook API session key if you're using the legacy API
key/Secret key authentication scheme. Must be null
if
using OAuth access token authentication.resultType
- Result type token.additionalParameters
- Additional parameters to include in the API call.resultType
which contains API response
data.LegacyFacebookClient.executeMultiquery(java.util.Map,
String, Class, com.restfb.Parameter[])
protected String makeRequest(String method, String sessionKey, Parameter... parameters)
method
- Facebook API method name.sessionKey
- Facebook API session key (can be null
or a blank string).parameters
- Arbitrary number of parameters to send along to Facebook as part
of the API call.FacebookException
- If an error occurs while making the Facebook API POST or
processing the response.protected String toParameterString(String method, String sessionKey, Parameter... parameters)
method
- Facebook API method name.sessionKey
- Facebook API session key (can be null
or a blank string).parameters
- Arbitrary number of extra parameters to include in the request.IllegalArgumentException
- If a session key is provided but we're using OAuth authentication
instead.protected String generateSignature(Map<String,String> sortedParameters)
sortedParameters
- Parameter name/value mappings, sorted alphabetically.protected String generateMd5(String string)
string
.string
- The string for which an MD5 hash is calculated.string
.IllegalStateException
- If MD5 hashing isn't supported on this platform (should never
occur).protected boolean usesAccessTokenAuthentication()
true
if we are, false
if we're using the legacy
authentication scheme.protected void initializeIllegalParamNames()
protected String getFacebookRestEndpointUrl()
protected String getFacebookRestOauthEndpointUrl()
protected void throwLegacyFacebookResponseStatusExceptionIfNecessary(String json)
error_code
JSON field is present, we've got a response
status error for this API call. Extracts relevant information from the JSON
and throws an exception which encapsulates it for end-user consumption.json
- The JSON returned by Facebook in response to an API call.FacebookResponseStatusException
- If the JSON contains an error code.FacebookJsonMappingException
- If an error occurs while processing the JSON.protected Parameter[] parametersWithAdditionalParameter(Parameter parameter, Parameter... parameters)
parameter
to the given parameters
array.parameter
- The parameter value to append.parameters
- The parameters to which the given parameter
is appended.parameter
and
parameters
.protected String queriesToJson(Map<String,String> queries)
queries
- The query map to convert.queries
in JSON string format.IllegalArgumentException
- If the provided queries
are invalid.protected String urlEncodedValueForParameterName(String name, String value)
value
for the parameter
named name
.
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.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.value
.protected void verifyParameterLegality(Parameter... parameters)
parameters
- The parameters to check.IllegalArgumentException
- If there's a parameter name collision.protected void verifyParameterPresence(String parameterName, String parameter)
parameter
isn't null
or an empty string.parameterName
- The name of the parameter (to be used in exception message).parameter
- The parameter to check.IllegalArgumentException
- If parameter
is null
or an empty string.protected void verifyParameterPresence(String parameterName, Object parameter)
parameter
isn't null
.parameterName
- The name of the parameter (to be used in exception message).parameter
- The parameter to check.IllegalArgumentException
- If parameter
is null
.Copyright © 2014. All rights reserved.