*/
/* if there is any request token in session, get it */
requestToken = getRequestTokenFromSession();
OAuthService service = null;
Verifier verifier = null;
Token accessToken = null;
/* Get Access Token */
if (authProvider != ClientUtils.DEFAULT)
{
service = getOAuthService(authProvider);
verifier = new Verifier(credential.getVerifier());
logger.info("Requesting access token with requestToken: " + requestToken);
logger.info("verifier=" + verifier);
try
{
accessToken = service.getAccessToken(requestToken,verifier);
if (accessToken == null)
{
logger.error("Could not get Access Token for " + authProviderName);
throw new OurException("Could not get Access Token");
}
}
catch (Exception e)
{
logger.info("Exception received gettign Access Token: " + e);
throw new OurException("Exception received getting Access Token: " + e);
}
logger.info("Got the access token: " + accessToken);
logger.info(" Token: " + accessToken.getToken());
logger.info(" Secret: " + accessToken.getSecret());
logger.info(" Raw: " + accessToken.getRawResponse());
}
else
{
/*
** Default provider.
** The info will probably come from database. Password will
** probably some kind of salted hash. We're just hard coding
** "test" and "secret" for the demo.
*/
logger.info("Handing default loign..");
String username = credential.getLoginName();
String password = credential.getPassword();
if (username == null)
{
throw new OurException("Default Username can not be empty");
}
if (password == null)
{
throw new OurException("Default Password not be empty");
}
if (username.equals(getDefaultUsername()) && password.equals(getDefaultPassword()))
{
}
else
{
throw new OurException("Please use " + getDefaultUsername() + " and " + getDefaultPassword() + " as Default Credential!");
}
}
if (authProvider == ClientUtils.INSTAGRAM)
{
try
{
instragramToken = InstragramToken.parse(accessToken.getRawResponse());
} catch (ParseException e)
{
throw new OurException("Could not parse " + authProviderName + " Json AccessToken");
}
logger.info("Getting Instragram Access Token");
logger.info(" access token" + instragramToken.getAcessToken());
logger.info(" userId: " + instragramToken.getUserId());
logger.info(" full name: " + instragramToken.getFullName());
logger.info(" username: " + instragramToken.getFullName());
logger.info(" raw: " + instragramToken.getRawResponse());
// replace userId and access token in protected resource url
protectedResourceUrl = ClientUtils.getProctedResourceUrl(authProvider);
logger.info("Instragram protected resource url: " + protectedResourceUrl);
protectedResourceUrl = String.format(protectedResourceUrl, instragramToken.getUserId(),instragramToken.getAcessToken());
logger.info("Instragram protected resource url: " + protectedResourceUrl);
}
if (authProvider == ClientUtils.GITHUB ||
authProvider == ClientUtils.FOURSQUARE)
{
protectedResourceUrl = String.format(protectedResourceUrl,accessToken.getToken());
}
if (authProvider == ClientUtils.YAHOO)
{
//throw new OurException("Not implemented for yahoo yet!)");
/* we need to replace <GUID> */
yahooGuid = getQueryStringValue(accessToken.getRawResponse(),"xoauth_yahoo_guid");
if (yahooGuid == null)
{
throw new OurException("Could not get Yahoo GUID from Query String");
}
// must save it to session. we'll use to get the user profile
saveYahooGuidToSession(yahooGuid);
protectedResourceUrl = ClientUtils.getProctedResourceUrl(authProvider);
protectedResourceUrl = String.format(protectedResourceUrl,yahooGuid);
logger.info("Yahoo protected resource url: " + protectedResourceUrl);
}
// make session id
String sessionId = makeRandomString();
// must save session id to session
saveSessionIdToSession(sessionId);
// must save authProvider to session
saveAuthProviderToSession(authProvider);
SocialUser socialUser = null;
if (authProvider != ClientUtils.DEFAULT)
{
// must save acess token to session
saveAccessTokenToSession(accessToken);
// must save the protected resource url to session
saveProtectedResourceUrlToSession(protectedResourceUrl);
// now request protected resource
logger.info("Getting protected resource");
logger.info("Protected resource url: " + protectedResourceUrl);
try
{
OAuthRequest request = new OAuthRequest(Verb.GET,protectedResourceUrl);
service.signRequest(accessToken,request);
Response response = request.send();
logger.info("Status code: " + response.getCode());
logger.info("Body: " + response.getBody());