Search code examples
javaapache-axiswso2-identity-server

302 Error while creating service provider in WSO2 Identity Server


I am trying to create a service provider in WSO2 Identity Server through Java program. The code block creating a service provider goes as follows.

public static OAuthKeySecret createOAuthServiceProvider(OAuthAppDetails oauthApp, String authType) {
        OAuthKeySecret oauthKeySecret = new OAuthKeySecret();
        try {               
            IdentityApplicationManagementServiceStub IAMStub = new IdentityApplicationManagementServiceStub(
                    null, oauthApp.getSERVER_URL() + "IdentityApplicationManagementService");               
            ServiceClient IAMClient = IAMStub._getServiceClient();              
            Authenticate.authenticate(IAMClient);               
            ServiceProvider serviceProvider = new ServiceProvider();
            serviceProvider.setApplicationName(oauthApp.getAppName());
            serviceProvider.setDescription(oauthApp.getAppDescription());
            IAMStub.createApplication(serviceProvider);                 
            System.out.println("Service Provider created");    
        } catch (Exception e) {
            e.printStackTrace();
        }
        return oauthKeySecret;    
    }

When running the program I am getting the following error traced back to following line of code

IAMStub.createApplication(serviceProvider);

Complete trace

org.apache.axis2.AxisFault: Transport error: 302 Error: Found
        at org.apache.axis2.transport.http.HTTPSender.handleResponse(HTTPSender.java:311)
        at org.apache.axis2.transport.http.HTTPSender.sendViaPost(HTTPSender.java:194)
        at org.apache.axis2.transport.http.HTTPSender.send(HTTPSender.java:75)
        at org.apache.axis2.transport.http.CommonsHTTPTransportSender.writeMessageWithCommons(CommonsHTTPTransportSender.java:451)
        at org.apache.axis2.transport.http.CommonsHTTPTransportSender.invoke(CommonsHTTPTransportSender.java:278)
        at org.apache.axis2.engine.AxisEngine.send(AxisEngine.java:442)
        at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:430)
        at org.apache.axis2.description.OutInAxisOperationClient.executeImpl(OutInAxisOperation.java:225)
        at org.apache.axis2.client.OperationClient.execute(OperationClient.java:149)
        at org.wso2.carbon.identity.application.mgt.stub.IdentityApplicationManagementServiceStub.createApplication(IdentityApplicationManagement
ServiceStub.java:601)
        at com.xxxxx.identity.wso2.IdentityServerAdapter.IDManagementClient.createOAuthServiceProvider(IDManagementClient.java:52)
        at com.xxxxx.identity.wso2.IdentityServerAdapter.IdentityServerRest$1.handle(IdentityServerRest.java:37)
        at com.xxxxx.identity.wso2.IdentityServerAdapter.IdentityServerRest$1.handle(IdentityServerRest.java:19)
        at spark.webserver.MatcherFilter.doFilter(MatcherFilter.java:138)
        at spark.webserver.JettyHandler.doHandle(JettyHandler.java:54)
        at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:179)
        at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:136)
        at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
        at org.eclipse.jetty.server.Server.handle(Server.java:451)
        at org.eclipse.jetty.server.HttpChannel.run(HttpChannel.java:252)
        at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:266)
        at org.eclipse.jetty.io.AbstractConnection$ReadCallback.run(AbstractConnection.java:240)
        at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:596)
        at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:527)
        at java.lang.Thread.run(Unknown Source)

Whats going on here? Please help me.


Solution

  • In Identity Server 5.1.0, "createApplication" operation works without any issue. Can you please post the full sample source code and Implementation of Authenticate.authenticate? Log the values of "oauthApp.getAppName()". Below code works for me.

    try {
                String authCookie = null;
                ConfigurationContext ctx = ConfigurationContextFactory.createConfigurationContextFromFileSystem(null, null);
                AuthenticationAdminStub authstub = new AuthenticationAdminStub(ctx, "https://localhost:9443/services`enter code here`/AuthenticationAdmin");
                ServiceClient client = authstub._getServiceClient();
                Options options = client.getOptions();`enter code here`
                options.setManageSession(true);
                options.setProperty(org.apache.axis2.transport.http.HTTPConstants.COOKIE_STRING, authCookie);
                //set trust store properties required in SSL communication.
                System.setProperty("javax.net.ssl.trustStore", RemoteUMSampleConstants.TRUST_STORE_PATH);
                System.setProperty("javax.net.ssl.trustStorePassword", RemoteUMSampleConstants.TRUST_STORE_PASSWORD);
                authstub.login("admin", "admin", "localhost");
                authCookie = (String) authstub._getServiceClient().getServiceContext().getProperty(
                        HTTPConstants.COOKIE_STRING);
    
    
                IdentityApplicationManagementServiceStub stub = new IdentityApplicationManagementServiceStub(
                        "https://localhost:9443/services/IdentityApplicationManagementService");
                ServiceClient e = stub._getServiceClient();
                Options option = e.getOptions();
                option.setManageSession(true);
                option.setProperty("Cookie", authCookie);
    
                ServiceProvider serviceProvider = new ServiceProvider();
                serviceProvider.setApplicationName("testName");
                serviceProvider.setDescription("testDescription");
    
                stub.createApplication(serviceProvider);
    }catch (Exception e){
                System.out.print(e);
    }