trying to get RequestAccess to work with Java

157 views
Skip to first unread message

Ezra

unread,
Mar 1, 2012, 3:08:10 PM3/1/12
to ChannelAdvisor Developer Network
Hello,
I've extracted the Admin Service WSLD and it unpacked all the Java
classes but I wasn't able to figure out a way to get the RequestAccess
to work. I found a "requestAccess(int localID)" method available but
there was no way to set a developer key and password. After searching
around in the ChannelAdvisor Group, I could not find any good
resources for Java regarding RequestAccess.

I did however built my own SOAP calls but this returns the following
error:

javax.xml.soap.SOAPException: Server did not recognize the value of
HTTP Header SOAPAction: urn:anonOutInOp.

Here is my code:


import javax.xml.soap.*;
import javax.xml.namespace.QName;
import java.net.URL;
import java.util.Iterator;

public class RequestAccessTest {
public static void main(String[] args) throws Exception {

SOAPConnectionFactory soapConnectionFactory =
SOAPConnectionFactory.newInstance();
SOAPConnection connection =
soapConnectionFactory.createConnection();
SOAPFactory soapFactory = SOAPFactory.newInstance();

MessageFactory factory = MessageFactory.newInstance();
SOAPMessage message = factory.createMessage();

MimeHeaders hd = message.getMimeHeaders();
hd.addHeader("SOAPAction", "http://api.channeladvisor.com/
webservices/RequestAccess");

SOAPPart sp = message.getSOAPPart();
SOAPEnvelope envelope = sp.getEnvelope();
SOAPHeader header = envelope.getHeader();
SOAPBody body = envelope.getBody();

//Envelope
envelope.addNamespaceDeclaration("web","http://
api.channeladvisor.com/webservices/");

//Header
Name headerName =
soapFactory.createName("APICredentials","web", "http://
api.channeladvisor.com/webservices/");
SOAPHeaderElement headerElement =
header.addHeaderElement(headerName);

Name devKeyName = soapFactory.createName("DeveloperKey",
"web", "http://api.channeladvisor.com/webservices/");
SOAPElement devKey =
headerElement.addChildElement(devKeyName);
devKey.addTextNode("<put developer key here>");

Name passwordName = soapFactory.createName("Password", "web",
"http://api.channeladvisor.com/webservices/");
SOAPElement password =
headerElement.addChildElement(passwordName);
password.addTextNode("<put password here>");

//Body
Name bodyName =
soapFactory.createName("RequestAccess","web","http://
api.channeladvisor.com/webservices/");
SOAPBodyElement bodyElement = body.addBodyElement(bodyName);

Name localIDName = soapFactory.createName("localID", "web",
"http://api.channeladvisor.com/webservices/");
SOAPElement localID =
bodyElement.addChildElement(localIDName);
localID.addTextNode("<put profile Id Here>");

URL endpoint = new URL("https://api.channeladvisor.com/
ChannelAdvisorAPI/v6/AdminService.asmx");
SOAPMessage response = connection.call(message, endpoint);
connection.close();



}
}


Can someone take a look at the code? My SOAP Action is set to:
"http://api.channeladvisor.com/webservices/RequestAccess" yet i'm
getting the error that the server did not recognize the header
"urn:anonOutInOp"

Thanks,
Ezra


Ezra

unread,
Mar 2, 2012, 3:37:48 PM3/2/12
to ChannelAdvisor Developer Network
Nevermind...

I figured out how to use the Channel Advisor Admin Service API with
Java

For others that are on Java, here is a demonstration on how to
manipulate the admin service:


First create an AuthenticationHandler.java file:

//Set the package to wherever you've extracted the AdminService wsdl.
package com.channeladvisor.adminservice;

import javax.xml.namespace.QName;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import org.apache.axis.*;
import org.apache.axis.handlers.BasicHandler;
import org.apache.axis.message.SOAPHeaderElement;
import org.apache.log4j.Logger;
/**
* User: Ezra Oh
* Date: Mar 2, 2012
* Time: 10:10:55 AM
*/

public class AuthenticationHandler extends BasicHandler {
public void invoke(MessageContext context) throws AxisFault {
try {
MessageContext smc = (MessageContext) context;
SOAPMessage message = smc.getMessage();
QName APICredentialsName = new QName("http://
api.channeladvisor.com/webservices/","APICredentials");
QName DeveloperKeyName = new QName("http://
api.channeladvisor.com/webservices/","DeveloperKey");
QName PasswordName = new QName("http://api.channeladvisor.com/
webservices/", "Password");

SOAPHeader header = message.getSOAPHeader();
SOAPHeaderElement cred = new
SOAPHeaderElement(APICredentialsName);
SOAPHeaderElement key = new SOAPHeaderElement(DeveloperKeyName);
key.setObjectValue("<put your dev key here>");
SOAPHeaderElement pw = new SOAPHeaderElement(PasswordName);
pw.setObjectValue("<put your password here>");
cred.addChildElement(key);
cred.addChildElement(pw);
header.appendChild(cred);
} catch (Exception e) {
throw new AxisFault("Error during authentication", e);
}
}
}


Once you've created the AuthenticationHandler.java file, create a new
file called AdminServiceTestExample.java. Complie then run the
example file:

Note: This is just a demonstration. I'd run each test one at a time.
-Test #1 to verify you have connectivity
-Test #2 to request your Dev key access to a Profile ID
-Test #3 to view what's currently authorized for a given Profile ID


//Set the package to wherever you've extracted the AdminService wsdl.
package com.channeladvisor.adminservice;

import org.apache.axis.Handler;
import org.apache.axis.SimpleChain;
import org.apache.axis.SimpleTargetedChain;
import org.apache.axis.client.AxisClient;
import org.apache.axis.configuration.SimpleProvider;
import org.apache.axis.transport.http.HTTPSender;
import org.apache.axis.transport.http.HTTPTransport;


/**
* User: Ezra Oh
* Date: Mar 2, 2012
* Time: 12:06:48 PM
*/
public class AdminServiceTestExample {
public static void main(String[] args) throws Exception {

SimpleProvider provider = new SimpleProvider();
Handler handler = new AuthenticationHandler();
SimpleChain requestChain = new SimpleChain();
requestChain.addHandler(handler);
SimpleChain responseChain = new SimpleChain();
Handler sender = new HTTPSender();
Handler transport = new SimpleTargetedChain(requestChain,
sender, responseChain);


provider.deployTransport(HTTPTransport.DEFAULT_TRANSPORT_NAME,transport);

AdminServiceLocator locator = new AdminServiceLocator();
locator.setEngineConfiguration(provider);
locator.setEngine(new AxisClient(provider));


//Test #1: Ping the Admin Service and print the response to
the console
APIResultOfString pingResponse =
locator.getAdminServiceSoap().ping();
System.out.println(pingResponse.getResultData());


//Test #2: Request Access to a Profile ID
APIResultOfBoolean raResponse =
locator.getAdminServiceSoap().requestAccess(<put your Profile ID
Here>);
if(raResponse.isResultData()){
System.out.println("Request Access was successfully
submitted: " + raResponse.getStatus());
System.out.println("Now go into the Channel Advisor
Message Center and enable this Dev Key!");
}else{
System.out.println("There was a problem Requesting Access:
" + raResponse.getStatus());
}


//Test #3: With a given Profile ID, obtain a list of account
authorizations and print to the console
java.math.BigInteger bi = new java.math.BigInteger("<put your
Pofile ID Here>");
APIResultOfArrayOfAuthorizationResponse arrAuthResponse =
locator.getAdminServiceSoap().getAuthorizationList(bi);
AuthorizationResponse[] authResponse =
arrAuthResponse.getResultData().getAuthorizationResponse();
for(int i=0; i < authResponse.length; i++){
System.out.println("Account ID:" +
authResponse[i].getAccountID());
System.out.println("Account Name:" +
authResponse[i].getAccountName());
System.out.println("Account Type:" +
authResponse[i].getAccountType());
System.out.println("Local ID:" +
authResponse[i].getLocalID());
System.out.println("Resource Name:" +
authResponse[i].getResourceName());
System.out.println("Status:" +
authResponse[i].getStatus());
}


}
}

SandeepM

unread,
Sep 25, 2012, 12:07:24 PM9/25/12
to ca...@googlegroups.com

Hi Ezra,

 Thank you for posting your source code here. Could you please throw some light on the reason why I am getting null for the SOAPHeader.
SOAPHeader header = message.getSOAPHeader();
I am seeing header as null in the message.

Thanks,
Reply all
Reply to author
Forward
0 new messages