Hi,
i'm doing an application that collects updates from users and then sends these updates as their Yahoo! statuses.
the problem is the access token expires every 1 hour and whenever i try to run the code from the YOS java api that refreshes the access token, it always says "token_rejected"
so my question is:
is there a way to get a permanent access token for a certain user ? if there isn't a way, what am i doing wrong in the refresh procedure ? (code snippet is below, the only thing worth mentioning, is that the session handle isn't retrieved correctly for some reason so it is saved as a blank string)
private static void refreshAccessToken(AccessToken accessToken, int userID, OAuthConsumer consumer, OAuthClient client, OAuthServiceProvider provider) {
try {
OAuthAccessor accessor = new OAuthAccessor(consumer);
RequestToken requestToken = new RequestToken();
requestToken.setKey(accessToken.getKey());
requestToken.setSessionHandle(accessToken.getSessionHandle());
accessor.tokenSecret = accessToken.getSecret();
AccessToken at = fetchAccessToken(accessor, requestToken, null, client, provider);
if (at != null) {
UserHandler.addYahooCredential(userID, at.getKey(), at.getSecret(), at.getOwner(), at.getGuid(), at.getSessionHandle());
} else {
log.error("YahooGateway Error refreshing the YahooAccessToken at " + SystemUtils.now());
YahooCredential yc = new YahooCredential(new User(userID));
YahooCredential.deleteYahooCredential(yc);
}
} catch (Exception e) {
log.error("YahooGateway Error refreshing the YahooAccessToken at " + SystemUtils.now(), e);
}
}
private static AccessToken fetchAccessToken(OAuthAccessor accessor, RequestToken requestToken, String verifier, OAuthClient client, OAuthServiceProvider provider) throws IOException, URISyntaxException, OAuthException {
try {
List<OAuth.Parameter> params;
if (requestToken.getSessionHandle() != null) {
params = OAuth.newList("oauth_token", requestToken.getKey(), "oauth_session_handle", requestToken.getSessionHandle());
} else {
params = OAuth.newList("oauth_token", requestToken.getKey());
}
// Add the verifier which is required for OAuth1.0a
if (verifier != null) {
params.addAll(OAuth.newList("oauth_verifier", verifier));
}
OAuthMessage getTokenMsg = new OAuthMessage("GET", provider.accessTokenURL, params);
getTokenMsg.addRequiredParameters(accessor);
OAuthMessage msg = client.invoke(getTokenMsg, ParameterStyle.QUERY_STRING);
Map<String, String> map = OAuth.newMap(msg.getParameters());
AccessToken at = new AccessToken();
at.setKey(map.get("oauth_token"));
at.setSecret(map.get("oauth_token_secret"));
at.setGuid(map.get("xoauth_yahoo_guid"));
at.setConsumer(accessor.consumer.consumerKey);
at.setSessionHandle(map.get("oauth_session_handle"));
long now = System.currentTimeMillis() / 1000;
if (map.containsKey("oauth_expires_in")) {
at.setTokenExpires(now + Long.parseLong(msg.getParameter("oauth_expires_in")));
} else {
at.setTokenExpires(-1);
}
if (map.containsKey("oauth_authorization_expires_in")) {
at.setHandleExpires(now + Long.parseLong(map.get("oauth_authorization_expires_in")));
} else {
at.setHandleExpires(-1);
}
return at;
} catch (Exception e) {
log.error("(fetchAccessToken) Error getting the new accessToken at " + SystemUtils.now(), e);
return null;
}
your help is much appreciated
by
0 Replies