Search code examples
oauthibm-sbt

Error trying to access SmartCloud using OAuth


I'm trying to use the Social Business Toolkit to access SmartCloud using OAuth, but I'm getting this error. Using SmartCloudBasicEndpoint everything works fine.

[4/4/14 12:01:56:236 CEST] 00000038 SBTProxy      I   URL computed from SBTProxy is https://apps.na.collabserv.com/communities/service/atom/oauth/communities/all
[4/4/14 12:01:56:556 CEST] 00000038               E com.ibm.sbt.security.authentication.oauth.consumer.OAuth1Handler _performOAuth1Dance Failed to get request token. requestUrl:https://apps.na.collabserv.com/manage/oauth/getRequestToken, authorizeUrl: https://apps.na.collabserv.com/manage/oauth/authorizeToken, accessUrl: https://apps.na.collabserv.com/manage/oauth/getAccessToken, callback: null, truncated key:a919....1b25, truncated secret:9054....687d. OAuth callback is empty, please check with your application vendor to ensure a callback is not required.

                                 com.ibm.sbt.security.authentication.oauth.OAuthException: Internal error - getRequestToken failed Exception: <br>
    at com.ibm.sbt.security.authentication.oauth.consumer.OAuth1Handler.getRequestTokenFromServer(OAuth1Handler.java:169)
    at com.ibm.sbt.security.authentication.oauth.consumer.OAuth1Handler._performOAuth1Dance(OAuth1Handler.java:656)
    at com.ibm.sbt.security.authentication.oauth.consumer.OAuth1Handler.performOAuth1Dance(OAuth1Handler.java:649)
    at com.ibm.sbt.security.authentication.oauth.consumer.OAuth1Handler._acquireToken(OAuth1Handler.java:625)
    at com.ibm.sbt.security.authentication.oauth.consumer.OAuth1Handler.acquireToken(OAuth1Handler.java:580)
    at com.ibm.sbt.services.endpoints.OAuthEndpoint.authenticate(OAuthEndpoint.java:234)
    at com.ibm.sbt.services.client.ClientService.forceAuthentication(ClientService.java:281)
    at com.ibm.sbt.services.client.ClientService.processResponse(ClientService.java:1123)
    at com.ibm.sbt.services.client.ClientService._xhr(ClientService.java:1041)
    at com.ibm.sbt.services.client.ClientService.execRequest(ClientService.java:1006)
    at com.ibm.sbt.services.client.ClientService.xhr(ClientService.java:966)
    at com.ibm.sbt.services.client.ClientService.get(ClientService.java:842)
    at com.ibm.sbt.services.client.ClientService.get(ClientService.java:838)
    at com.ibm.sbt.services.client.base.BaseService.retrieveData(BaseService.java:352)
    at com.ibm.sbt.services.client.base.BaseService.retrieveData(BaseService.java:376)
    at com.ibm.sbt.services.client.base.BaseService.retrieveData(BaseService.java:327)
    at com.ibm.sbt.services.client.base.BaseService.getEntities(BaseService.java:187)
    at com.ibm.sbt.services.client.connections.communities.CommunityService.getPublicCommunities(CommunityService.java:164)
    at com.ibm.sbt.services.client.connections.communities.CommunityService.getPublicCommunities(CommunityService.java:146)
    at servlets.SmartCloudTestServlet.doGet(SmartCloudTestServlet.java:82)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:575)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1224)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:774)
    at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:456)
    at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:178)
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.invokeTarget(WebAppFilterChain.java:136)
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:97)
    at com.ibm.sbt.util.SBTFilter.doFilter(SBTFilter.java:53)
    at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:195)
    at com.ibm.ws.webcontainer.filter.WebAppFilterChain.doFilter(WebAppFilterChain.java:91)
    at com.ibm.ws.webcontainer.filter.WebAppFilterManager.doFilter(WebAppFilterManager.java:928)
    at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1025)
    at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3751)
    at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:304)
    at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:962)
    at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1662)
    at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:195)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:452)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:511)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:305)
    at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:276)
    at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.sendToDiscriminators(NewConnectionInitialReadCallback.java:214)
    at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.complete(NewConnectionInitialReadCallback.java:113)
    at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)
    at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
    at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
    at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
    at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
    at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
    at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
    at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1690)
Caused by: java.lang.NullPointerException
    at java.net.URLEncoder.encode(URLEncoder.java:197)
    at com.ibm.sbt.security.authentication.oauth.consumer.OAuth1Handler.getRequestTokenFromServer(OAuth1Handler.java:156)
    ... 51 more

[4/4/14 12:01:56:560 CEST] 00000038 SystemOut     O Problem Occurred while fetching public communities: Problem occurred while fetching public communities

Here's what I've done so far:

  • Create a new Servlet application.
  • I'm using RuntimeFactoryStandalone.
  • Added managed-beans.xml and sbt.properties (both from samples app) to /WEB-INF/.
  • I'm running the application on WebSphere Application Server. (I also tried the same code in a Portlet on WebSphere Portal)
  • Added the SmartCloud key, secret and appId to the properties file.

I'm using this code:

try {
    CommunityService svc = new CommunityService("smartcloud");
    CommunityList communities = svc.getPublicCommunities();

    System.out.println("Listing public communities , Total communities found : " + communities.getTotalResults());
} catch (Throwable e) {
    System.out.println("Problem Occurred while fetching public communities: " + e.getMessage());
    //e.printStackTrace();
}

I'm getting the same error if I manually use a SmartCloudOAuthEndpoint.

Am I forgetting something here?


Update:

My manual endpoint:

SmartCloudOAuthEndpoint endpoint = new SmartCloudOAuthEndpoint();
endpoint.setUrl("https://apps.na.collabserv.com");
endpoint.setConsumerKey("a91..----REMOVED FOR STACKOVERFLOW----..");
endpoint.setConsumerSecret("905..----REMOVED FOR STACKOVERFLOW----..");
endpoint.setRequestTokenURL("https://apps.na.collabserv.com/manage/oauth/getRequestToken");
endpoint.setAuthorizationURL("https://apps.na.collabserv.com/manage/oauth/authorizeToken");
endpoint.setAccessTokenURL("https://apps.na.collabserv.com/manage/oauth/getAccessToken");
endpoint.setAppId("RonnieTest");
endpoint.setApiVersion("apiVersion");
//endpoint.setHttpProxy("localhost:8888");
endpoint.setForceTrustSSLCertificate(true);
endpoint.setSignatureMethod("PLAINTEXT");
endpoint.setCredentialStore("SmartCloudStore");
endpoint.setAuthenticationService("communities/service/atom/communities/my");

I'm using this Internal App in SmartCloud: enter image description here


Solution

  • I solved it. I wasn't passing ServletRequest and ServletResponse to my Context. Turns out that getCallbackUrl(Context context) in OAuthHandler is the first method that actually needs the request to build the callback URL.