0
我想試試salesforce.com上的oauth
servlet示例。無法在服務器上運行應用程序
但問題是,我不能在服務器上運行的應用程序,因爲它總是失敗,同樣的錯誤
我創建dynamic web project
和我的項目結構看起來像這樣
的Servlet我完全複製
@WebServlet(name = "oauth", urlPatterns = { "/oauth/*", "/oauth" }, initParams = {
@WebInitParam(name = "clientId", value = "xxxx"),
@WebInitParam(name = "clientSecret", value = "xxxx"),
@WebInitParam(name = "redirectUri", value = "https://localhost:8443/DemoREST/oauth/_callback"),
@WebInitParam(name = "environment", value = "https://login.salesforce.com"), })
public class OAuthServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String ACCESS_TOKEN = "ACCESS_TOKEN";
private static final String INSTANCE_URL = "INSTANCE_URL";
private String clientId = null;
private String clientSecret = null;
private String redirectUri = null;
private String environment = null;
private String authUrl = null;
private String tokenUrl = null;
public void init() throws ServletException {
clientId = this.getInitParameter("clientId");
clientSecret = this.getInitParameter("clientSecret");
redirectUri = this.getInitParameter("redirectUri");
environment = this.getInitParameter("environment");
try {
authUrl = environment
+ "/services/oauth2/authorize?response_type=code&client_id="
+ clientId + "&redirect_uri="
+ URLEncoder.encode(redirectUri, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new ServletException(e);
}
tokenUrl = environment + "/services/oauth2/token";
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String accessToken = (String) request.getSession().getAttribute(
ACCESS_TOKEN);
if (accessToken == null) {
String instanceUrl = null;
if (request.getRequestURI().endsWith("oauth")) {
// we need to send the user to authorize
response.sendRedirect(authUrl);
return;
} else {
System.out.println("Auth successful - got callback");
String code = request.getParameter("code");
HttpClient httpclient = new HttpClient();
PostMethod post = new PostMethod(tokenUrl);
post.addParameter("code", code);
post.addParameter("grant_type", "authorization_code");
post.addParameter("client_id", clientId);
post.addParameter("client_secret", clientSecret);
post.addParameter("redirect_uri", redirectUri);
try {
httpclient.executeMethod(post);
try {
JSONObject authResponse = new JSONObject(
new JSONTokener(new InputStreamReader(
post.getResponseBodyAsStream())));
System.out.println("Auth response: "
+ authResponse.toString(2));
accessToken = authResponse.getString("access_token");
instanceUrl = authResponse.getString("instance_url");
System.out.println("Got access token: " + accessToken);
} catch (JSONException e) {
e.printStackTrace();
throw new ServletException(e);
}
} finally {
post.releaseConnection();
}
}
// Set a session attribute so that other servlets can get the access
// token
request.getSession().setAttribute(ACCESS_TOKEN, accessToken);
// We also get the instance URL from the OAuth response, so set it
// in the session too
request.getSession().setAttribute(INSTANCE_URL, instanceUrl);
}
response.sendRedirect(request.getContextPath() + "/DemoREST");
}
}
但是,當我嘗試它在服務器上運行我得到了錯誤。
Sep 19, 2014 2:41:48 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre7\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\TortoiseSVN\bin;C:\Program Files\TortoiseGit\bin;D:\installed\Git1.8\cmd;D:\installed\cURL\bin;C:\Program Files\Java\jdk1.7.0_21\bin;D:\installed\apache-maven-3.2.1\bin;D:\installed\apache-ant-1.9.3\bin;D:\installed\luna\eclipse;D:\installed\apache-tomcat-7.0.53\bin;D:\installed\Git1.7.7.1\cmd;D:\installed\gradle-1.7\bin;C:\Program Files\PostgreSQL\9.3\bin;D:\installed\cURL\bin;D:\installed\apache-activemq-5.8.0\bin;;.
Sep 19, 2014 2:41:48 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:Salesforce' did not find a matching property.
Sep 19, 2014 2:41:49 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Sep 19, 2014 2:41:49 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Sep 19, 2014 2:41:49 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
Sep 19, 2014 2:41:49 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Sep 19, 2014 2:41:49 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 1223 ms
Sep 19, 2014 2:41:49 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Sep 19, 2014 2:41:49 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.0.5
Sep 19, 2014 2:41:49 PM org.apache.catalina.core.ContainerBase startInternal
SEVERE: A child container failed during start
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/Salesforce]]
at java.util.concurrent.FutureTask$Sync.innerGet(Unknown Source)
at java.util.concurrent.FutureTask.get(Unknown Source)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:918)
at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:851)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1388)
at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/Salesforce]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
... 7 more
Caused by: java.lang.NoClassDefFoundError: org/apache/commons/httpclient/HttpMethod
at java.lang.Class.getDeclaredFields0(Native Method)
at java.lang.Class.privateGetDeclaredFields(Unknown Source)
at java.lang.Class.getDeclaredFields(Unknown Source)
at org.apache.catalina.util.Introspection.getDeclaredFields(Introspection.java:106)
at org.apache.catalina.startup.WebAnnotationSet.loadFieldsAnnotation(WebAnnotationSet.java:261)
at org.apache.catalina.startup.WebAnnotationSet.loadApplicationServletAnnotations(WebAnnotationSet.java:140)
at org.apache.catalina.startup.WebAnnotationSet.loadApplicationAnnotations(WebAnnotationSet.java:66)
at org.apache.catalina.startup.ContextConfig.applicationAnnotationsConfig(ContextConfig.java:332)
at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:771)
at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:303)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5058)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 7 more
Caused by: java.lang.ClassNotFoundException: org.apache.commons.httpclient.HttpMethod
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1328)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1175)
... 21 more
Sep 19, 2014 2:41:49 PM org.apache.catalina.core.ContainerBase startInternal
SEVERE: A child container failed during start
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost]]
at java.util.concurrent.FutureTask$Sync.innerGet(Unknown Source)
at java.util.concurrent.FutureTask.get(Unknown Source)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:918)
at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:262)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.StandardService.startInternal(StandardService.java:439)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:744)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.startup.Catalina.start(Catalina.java:656)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:351)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:485)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1388)
at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: org.apache.catalina.LifecycleException: A child container failed during start
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:926)
at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:851)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 7 more
Sep 19, 2014 2:41:49 PM org.apache.catalina.startup.Catalina start
SEVERE: The required Server component failed to start so Tomcat is unable to start.
org.apache.catalina.LifecycleException: Failed to start component [StandardServer[8005]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
at org.apache.catalina.startup.Catalina.start(Catalina.java:656)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:351)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:485)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardService[Catalina]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:744)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 7 more
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
at org.apache.catalina.core.StandardService.startInternal(StandardService.java:439)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 9 more
Caused by: org.apache.catalina.LifecycleException: A child container failed during start
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:926)
at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:262)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 11 more
Sep 19, 2014 2:41:49 PM org.apache.coyote.AbstractProtocol pause
INFO: Pausing ProtocolHandler ["http-nio-8080"]
Sep 19, 2014 2:41:49 PM org.apache.coyote.AbstractProtocol pause
INFO: Pausing ProtocolHandler ["ajp-nio-8009"]
Sep 19, 2014 2:41:49 PM org.apache.catalina.core.StandardService stopInternal
INFO: Stopping service Catalina
Sep 19, 2014 2:41:49 PM org.apache.coyote.AbstractProtocol destroy
INFO: Destroying ProtocolHandler ["http-nio-8080"]
Sep 19, 2014 2:41:49 PM org.apache.coyote.AbstractProtocol destroy
INFO: Destroying ProtocolHandler ["ajp-nio-8009"]
我用tomcat7