Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add /oauth/access_token_info endpoint #85

Merged
merged 1 commit into from
Nov 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,14 @@ public AccessToken extendTokenExpiration() throws FacebookException {
return extendTokenExpiration(getOAuth().getOAuthAccessToken().getToken());
}

public AccessToken getOAuthAccessTokenInfo(String accessToken) throws FacebookException {
return getOAuth().getOAuthAccessTokenInfo(accessToken);
}

public AccessToken getOAuthAccessTokenInfo() throws FacebookException {
return getOAuth().getOAuthAccessTokenInfo();
}

private OAuthSupport getOAuth() {
if (!(auth instanceof OAuthSupport)) {
throw new IllegalStateException("OAuth app id/secret combination not supplied");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,25 @@ protected String getExtendTokenURL(String shortLivedToken) {
"&fb_exchange_token=" + shortLivedToken;
}

public AccessToken getOAuthAccessTokenInfo(String accessToken) throws FacebookException {
String url = getAccessTokenInfoURL(accessToken);
HttpResponse response = http.get(url);
if (response.getStatusCode() != 200) {
throw new FacebookException("authorization failed.");
}
return new AccessToken(response);
}

public AccessToken getOAuthAccessTokenInfo() throws FacebookException {
return getOAuthAccessTokenInfo(this.oauthToken.getToken());
}

protected String getAccessTokenInfoURL(String accessToken) {
return conf.getOAuthAccessTokenInfoURL() +
"?client_id=" + this.appId +
"&access_token=" + accessToken;
}


public void setAppSecretProofEnabled(boolean enabled) {
this.appSecretProofEnabled = enabled;
Expand Down
14 changes: 14 additions & 0 deletions facebook4j-core/src/main/java/facebook4j/auth/OAuthSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,18 @@ public interface OAuthSupport {
*/
AccessToken extendTokenExpiration(String shortLivedToken) throws FacebookException;

/**
* Returns the access token information.
* @param accessToken access token
* @return access token information
* @throws FacebookException when Facebook service or network is unavailable, or the user has not authorized
*/
AccessToken getOAuthAccessTokenInfo(String accessToken) throws FacebookException;

/**
* Returns the access token information.
* @return access token information
* @throws FacebookException when Facebook service or network is unavailable, or the user has not authorized
*/
AccessToken getOAuthAccessTokenInfo() throws FacebookException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public interface Configuration extends HttpClientConfiguration

String getOAuthAccessTokenURL();

String getOAuthAccessTokenInfoURL();

String getOAuthDeviceTokenURL();

boolean isJSONStoreEnabled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public class ConfigurationBase implements Configuration, Serializable {

private String oAuthAuthorizationURL;
private String oAuthAccessTokenURL;
private String oAuthAccessTokenInfoURL;
private String oAuthDeviceTokenURL;

private String restBaseURL;
Expand All @@ -84,6 +85,7 @@ public class ConfigurationBase implements Configuration, Serializable {

private static final String DEFAULT_OAUTH_AUTHORIZATION_URL = "http://www.facebook.com/dialog/oauth";
private static final String DEFAULT_OAUTH_ACCESS_TOKEN_URL = "http://graph.facebook.com/oauth/access_token";
private static final String DEFAULT_OAUTH_ACCESS_TOKEN_INFO_URL = "http://graph.facebook.com/oauth/access_token_info";
private static final String DEFAULT_OAUTH_DEVICE_TOKEN_URL = "http://graph.facebook.com/oauth/device";

private static final String DEFAULT_REST_BASE_URL = "http://graph.facebook.com/";
Expand Down Expand Up @@ -147,6 +149,7 @@ protected ConfigurationBase() {

setOAuthAuthorizationURL(DEFAULT_OAUTH_AUTHORIZATION_URL);
setOAuthAccessTokenURL(DEFAULT_OAUTH_ACCESS_TOKEN_URL);
setOAuthAccessTokenInfoURL(DEFAULT_OAUTH_ACCESS_TOKEN_INFO_URL);
setOAuthDeviceTokenURL(DEFAULT_OAUTH_DEVICE_TOKEN_URL);

setRestBaseURL(DEFAULT_REST_BASE_URL);
Expand Down Expand Up @@ -395,6 +398,9 @@ private void fixRestBaseURL() {
if (DEFAULT_OAUTH_ACCESS_TOKEN_URL.equals(fixURL(false, oAuthAccessTokenURL))) {
this.oAuthAccessTokenURL = fixURL(useSSL, oAuthAccessTokenURL);
}
if (DEFAULT_OAUTH_ACCESS_TOKEN_INFO_URL.equals(fixURL(false, oAuthAccessTokenInfoURL))) {
this.oAuthAccessTokenInfoURL = fixURL(useSSL, oAuthAccessTokenInfoURL);
}
if (DEFAULT_OAUTH_DEVICE_TOKEN_URL.equals(fixURL(false, oAuthDeviceTokenURL))) {
this.oAuthDeviceTokenURL = fixURL(useSSL, oAuthDeviceTokenURL);
}
Expand Down Expand Up @@ -432,6 +438,15 @@ protected final void setOAuthAccessTokenURL(String oAuthAccessTokenURL) {
fixRestBaseURL();
}

public String getOAuthAccessTokenInfoURL() {
return oAuthAccessTokenInfoURL;
}

protected final void setOAuthAccessTokenInfoURL(String oAuthAccessTokenInfoURL) {
this.oAuthAccessTokenInfoURL = oAuthAccessTokenInfoURL;
fixRestBaseURL();
}

public String getOAuthDeviceTokenURL() {
return oAuthDeviceTokenURL;
}
Expand Down Expand Up @@ -532,6 +547,7 @@ public int hashCode() {
result = 31 * result + appSecretProofCacheSize;
result = 31 * result + (oAuthAuthorizationURL != null ? oAuthAuthorizationURL.hashCode() : 0);
result = 31 * result + (oAuthAccessTokenURL != null ? oAuthAccessTokenURL.hashCode() : 0);
result = 31 * result + (oAuthAccessTokenInfoURL != null ? oAuthAccessTokenInfoURL.hashCode() : 0);
result = 31 * result + (oAuthDeviceTokenURL != null ? oAuthDeviceTokenURL.hashCode() : 0);
result = 31 * result + (restBaseURL != null ? restBaseURL.hashCode() : 0);
result = 31 * result + (videoBaseURL != null ? videoBaseURL.hashCode() : 0);
Expand Down Expand Up @@ -581,6 +597,8 @@ public boolean equals(Object o) {
return false;
if (oAuthAccessTokenURL != null ? !oAuthAccessTokenURL.equals(that.oAuthAccessTokenURL) : that.oAuthAccessTokenURL != null)
return false;
if (oAuthAccessTokenInfoURL != null ? !oAuthAccessTokenInfoURL.equals(that.oAuthAccessTokenInfoURL) : that.oAuthAccessTokenInfoURL != null)
return false;
if (oAuthDeviceTokenURL != null ? !oAuthDeviceTokenURL.equals(that.oAuthDeviceTokenURL) : that.oAuthDeviceTokenURL != null)
return false;
if (oAuthAppId != null ? !oAuthAppId.equals(that.oAuthAppId) : that.oAuthAppId != null) return false;
Expand Down Expand Up @@ -632,6 +650,7 @@ public String toString() {
", appSecretProofCacheSize=" + appSecretProofCacheSize +
", oAuthAuthorizationURL='" + oAuthAuthorizationURL + '\'' +
", oAuthAccessTokenURL='" + oAuthAccessTokenURL + '\'' +
", oAuthAccessTokenInfoURL='" + oAuthAccessTokenInfoURL + '\'' +
", oAuthDeviceTokenURL='" + oAuthDeviceTokenURL + '\'' +
", restBaseURL='" + restBaseURL + '\'' +
", videoBaseURL='" + videoBaseURL + '\'' +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* </ul>
*/
public final class PropertyConfiguration extends ConfigurationBase implements Serializable {
private static final long serialVersionUID = -2795427346026618896L;
private static final long serialVersionUID = -6424033561218975631L;

public static final String DEBUG = "debug";
public static final String HTTP_USER_AGENT = "http.userAgent";
Expand Down Expand Up @@ -71,6 +71,7 @@ public final class PropertyConfiguration extends ConfigurationBase implements Se

public static final String OAUTH_AUTHORIZATION_URL = "oauth.authorizationURL";
public static final String OAUTH_ACCESS_TOKEN_URL = "oauth.accessTokenURL";
public static final String OAUTH_ACCESS_TOKEN_INFO_URL = "oauth.accessTokenInfoURL";
public static final String OAUTH_DEVICE_TOKEN_URL = "oauth.deviceTokenURL";

public static final String REST_BASE_URL = "restBaseURL";
Expand Down Expand Up @@ -306,6 +307,10 @@ private void setFieldsWithPrefix(Properties props, String prefix) {
setOAuthAccessTokenURL(getString(props, prefix, OAUTH_ACCESS_TOKEN_URL));
}

if (notNull(props, prefix, OAUTH_ACCESS_TOKEN_INFO_URL)) {
setOAuthAccessTokenInfoURL(getString(props, prefix, OAUTH_ACCESS_TOKEN_INFO_URL));
}

if (notNull(props, prefix, OAUTH_DEVICE_TOKEN_URL)) {
setOAuthDeviceTokenURL(getString(props, prefix, OAUTH_DEVICE_TOKEN_URL));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ public void shortToLong() throws Exception {
}
}

public static class AccessTokenInfo extends FacebookTestBase {
@Test
@Category(RealAPITests.class)
public void string() throws Exception {
Facebook facebook = FacebookFactory.getSingleton();
String oAuthAppId = p.getProperty("oauth.appId");
String oAuthAppSecret = p.getProperty("oauth.appSecret");
facebook.setOAuthAppId(oAuthAppId, oAuthAppSecret);

String accessToken = "your-access-token";
AccessToken accessTokenInfo = facebook.getOAuthAccessTokenInfo(accessToken);
assertThat(accessTokenInfo.getToken(), is(accessToken));
assertThat(accessTokenInfo.getExpires(), is(notNullValue()));
assertThat(accessTokenInfo.getType(), is(notNullValue()));
}
}

public static class AppSecretProof {
@Test
public void defaultCacheSize() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ public void configuration() throws Exception {
conf = new PropertyConfiguration();
assertThat(conf.getOAuthCallbackURL(), is("http://localhost:8080/callback"));

writeFile("./facebook4j.properties", "oauth.accessTokenInfoURL=https://graph.facebook.com/oauth/access_token_info2");
conf = new PropertyConfiguration();
assertThat(conf.getOAuthAccessTokenInfoURL(), is("https://graph.facebook.com/oauth/access_token_info2"));

writeFile("./facebook4j.properties", "oauth.deviceTokenURL=https://graph.facebook.com/oauth/deviceA");
conf = new PropertyConfiguration();
assertThat(conf.getOAuthDeviceTokenURL(), is("https://graph.facebook.com/oauth/deviceA"));
Expand Down