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

Implement OAuth 2.0 refresh access token #253

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/main/java/org/scribe/builder/api/DefaultApi20.java
Expand Up @@ -67,4 +67,11 @@ public OAuthService createService(OAuthConfig config)
return new OAuth20ServiceImpl(this, config);
}

/**
* @return the parameter needed to refresh a access token.
*/
public String getRefreshTokenParameterName()
{
throw new UnsupportedOperationException("Refresh token is not implemented for "+getClass().getSimpleName());
}
}
6 changes: 6 additions & 0 deletions src/main/java/org/scribe/builder/api/FacebookApi.java
Expand Up @@ -30,4 +30,10 @@ public String getAuthorizationUrl(OAuthConfig config)
return String.format(AUTHORIZE_URL, config.getApiKey(), OAuthEncoder.encode(config.getCallback()));
}
}

@Override
public String getRefreshTokenParameterName()
{
return "fb_exchange_token";
}
}
6 changes: 6 additions & 0 deletions src/main/java/org/scribe/builder/api/LiveApi.java
Expand Up @@ -37,4 +37,10 @@ public AccessTokenExtractor getAccessTokenExtractor()
{
return new JsonTokenExtractor();
}

@Override
public String getRefreshTokenParameterName()
{
return "refresh_token";
}
}
1 change: 1 addition & 0 deletions src/main/java/org/scribe/model/OAuthConstants.java
Expand Up @@ -45,5 +45,6 @@ public class OAuthConstants
public static final String CLIENT_SECRET = "client_secret";
public static final String REDIRECT_URI = "redirect_uri";
public static final String CODE = "code";
public static final String GRANT_TYPE = "grant_type";

}
8 changes: 8 additions & 0 deletions src/main/java/org/scribe/oauth/OAuth10aServiceImpl.java
Expand Up @@ -82,6 +82,14 @@ public Token getAccessToken(Token requestToken, Verifier verifier)
return api.getAccessTokenExtractor().extract(response.getBody());
}

/**
* {@inheritDoc}
*/
public Token refreshAccessToken(Token accessToken)
{
throw new UnsupportedOperationException("Refresh token is not supported in Scribe OAuth 1.0");
}

/**
* {@inheritDoc}
*/
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/org/scribe/oauth/OAuth20ServiceImpl.java
Expand Up @@ -37,6 +37,28 @@ public Token getAccessToken(Token requestToken, Verifier verifier)
return api.getAccessTokenExtractor().extract(response.getBody());
}

/**
* {@inheritDoc}
*/
public Token refreshAccessToken(Token accessToken)
{

String accessTokenEndpoint = api.getAccessTokenEndpoint();
if (accessTokenEndpoint.contains("?grant_type="))
{
// handle the ugly case where the grant_type parameter is already hardcoded in the constant url
accessTokenEndpoint = accessTokenEndpoint.substring(0, accessTokenEndpoint.indexOf("?"));
}
OAuthRequest request = new OAuthRequest(api.getAccessTokenVerb(), accessTokenEndpoint);
request.addQuerystringParameter(OAuthConstants.CLIENT_ID, config.getApiKey());
request.addQuerystringParameter(OAuthConstants.CLIENT_SECRET, config.getApiSecret());
request.addQuerystringParameter(OAuthConstants.REDIRECT_URI, config.getCallback());
request.addQuerystringParameter(OAuthConstants.GRANT_TYPE, api.getRefreshTokenParameterName());
request.addQuerystringParameter(api.getRefreshTokenParameterName(), accessToken.getToken());
Response response = request.send();
return api.getAccessTokenExtractor().extract(response.getBody());
}

/**
* {@inheritDoc}
*/
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/org/scribe/oauth/OAuthService.java
Expand Up @@ -27,6 +27,19 @@ public interface OAuthService
*/
public Token getAccessToken(Token requestToken, Verifier verifier);

/**
* Refresh the access token to extend its expiration date.
* <p/>
* For the token in parameter, Facebook needs the access_token, while Live
* needs the refresh_token (which can be found only in the
* {@link org.scribe.model.Token#getRawResponse()} returned by
* {@link #getAccessToken(org.scribe.model.Token, org.scribe.model.Verifier)})
*
* @param accessToken access or refresh token, depending on the OAuth provider
* @return fresh access token
*/
public Token refreshAccessToken(Token accessToken);

/**
* Signs am OAuth request
*
Expand Down