Question

I am running into a Access-Control-Allow-Origin error when I am trying to build an OpenSocial Gadget with IBM Social Business ToolKit for IBM Connections.

I have 3 servers participating in this gadget:

  • CONNECTIONS: The IBM Connections 4.0 Server that will be hosting the gadget
  • IBMSBT: A server hosting the Social Business Toolkit scripts and app
  • JESSE_API: My application server hosting the API that the gadget will be using

The gadget xml is loaded from JESSE_API by CONNECTIONS. The view for the gadget loads scripts and makes calls to JESSE_API. I would like to use the Social Business Toolkit for accessing parts of Connections so the gadget view is also loading those components from IBMSBT.

I am currently just prototyping this - I was able to make this work just using the Connections 4.0 API but would rather use the SBT libraries.

For getting started I just dropped in the "Get My Communities - Main Window" snippet into my gadget's view and included the following scripts:

<script type="text/javascript">
    var djConfig = {
        parseOnLoad: true
    };
</script>

<script src="//IBMSBT/sbt.dojo180/dojo/dojo.js"></script>
<script src="//IBMSBT/sbt.sample.web/library?ver=1.8.0"></script>

Reloading the gadget gives the following error in the console:

XMLHttpRequest cannot load http://IBMSBT/sbt.sample.web/service/proxy/connections/http/CONNECTIONS/communities/service/atom/communities/my?ps=5

Since my gadget was running on the CONNECTIONS server I should not need the proxy. I did not see an immediate way to disable the proxy for this endpoint so I just set a breakpoint in Endpoint.js before line 160 where the following code is executed:

if(this.proxy) {
    args.url = this.proxy.rewriteUrl(args.url,this.proxyPath);
}

When the breakpoint hits, I set this.proxy = null which causes the proxy to not be used and the community information to return correctly.

My question is should I be doing this differently or should a way be added to bypass the use of a proxy given the structure I am currently using?

Was it helpful?

Solution 2

Based on the information from Mark Wallace, I looked a bit closer at what the /library/ endpoint was doing.

I was not able to make it quite work exactly with that code but the following works nicely:

<script data-dojo-config="parseOnLoad:true"
        src="//IBMSBT/sbt.dojo180/dojo/dojo.js.uncompressed.js"></script>

<script>
if(typeof _sbt=='undefined' || window._sbt_bridge_compat){
    _sbt=0;
    dojo.registerModulePath('sbt','http://IBMSBT/sbt/js/sdk/sbt');
    dojo.registerModulePath('sbt/_bridge','http://IBMSBT/sbt/js/sdk/_bridges/dojo-amd');
    dojo.registerModulePath('sbt/dojo','http://IBMSBT/sbt/js/sdk/dojo');
    define('sbt/config',['sbt/Proxy','sbt/_bridge/Transport','sbt/authenticator/Basic','sbt/Endpoint'],function(Proxy,Transport,Basic,Endpoint){
        window.sbt = {};
        sbt.Properties={
            "sbtUrl":"http:\/\/IBMSBT\/sbt\/js\/sdk"
        };
        sbt.Endpoints={
            'connections':new Endpoint({
                "baseUrl":"http:\/\/connectionsww.demos.ibm.com",
                "transport":new Transport({}),
                "authType":"basic",
                "authenticator":new Basic({}),
                "proxyPath":"connections"})
        };
        return sbt;
    });
}
</script>

The contents of the script tag was basically the output from the /library/ endpoint. The sbt.Endpoints.connections definition originally included a defined proxy attribute which I removed.

OTHER TIPS

The SDK Proxy does not need to be used in this environment. We've made some changes in this area recently as part of the work to support OAuth. What you need to do is configure the SDK library initialization so it knows it's running in a Gadget context.

Take a look at the acme.social.sample.webapp:

  1. In faces-config.xml you will see an environment for use with OpenSocial

    <!--  OpenSocial Environment -->
    <managed-bean>
        <managed-bean-name>openSocial</managed-bean-name>
        <managed-bean-class>com.ibm.sbt.jslibrary.SBTEnvironment</managed-bean-class>
        <managed-bean-scope>application</managed-bean-scope>
        <managed-property>
            <property-name>endpoints</property-name>
            <value>acmeAirOS:acmeAir</value>
        </managed-property>
    </managed-bean>
    
  2. The endpoint definition used a gadget endpoint (further down in the faces-config.xml)

    <managed-bean>
        <managed-bean-name>acmeAirOS</managed-bean-name>
        <managed-bean-class>com.ibm.sbt.services.endpoints.GadgetOAuthEndpoint</managed-bean-class>
        <managed-bean-scope>session</managed-bean-scope>
        <managed-property>
            <property-name>url</property-name>
            <value>%{acme.url}</value>
        </managed-property>
    </managed-bean>
    
  3. In the gadget xml (or imported html) when loading the library pass a parameter to indicate that the OpenSocial environment should be used

    <script type="text/javascript" src="../../library?ver=1.8.0&context=gadget&env=openSocial"></script>
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top