我正在尝试使用以下代码生成请求令牌:
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, FileNotFoundException, IOException, Base64DecoderException, InvalidKeySpecException, NoSuchAlgorithmException, OAuthException {
// response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
String CONSUMER_KEY = "www.google.com";
GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters();
oauthParameters.setOAuthConsumerKey(CONSUMER_KEY);
oauthParameters.setScope("https://spreadsheets.google.com/feeds/spreadsheets/private/full");
oauthParameters.setOAuthCallback("callbackUrl.jsp");
BASE64Encoder encoder = new BASE64Encoder();
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
keyGen.initialize(1024, random);
KeyPair pair = keyGen.generateKeyPair();
PrivateKey priv = (RSAPrivateKey) pair.getPrivate();
//PublicKey pub = pair.getPublic();
GoogleOAuthHelper oauthHelper = new GoogleOAuthHelper(new OAuthRsaSha1Signer(priv));
oauthHelper.getUnauthorizedRequestToken(oauthParameters);
String approvalPageUrl = oauthHelper.createUserAuthorizationUrl(oauthParameters);
System.out.println("Callback URL : " + approvalPageUrl);
/* TODO output your page here
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet requestServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet requestServlet at " + request.getContextPath () + "</h1>");
out.println("</body>");
out.println("</html>");
*/
} finally {
out.close();
}
}
当然,这里没有编译时错误。但是,出现运行时错误,即:
com.google.gdata.client.authn.oauth.OAuthException: Error getting HTTP response
at com.google.gdata.client.authn.oauth.OAuthHttpClient.getResponse(OAuthHttpClient.java:76)
at com.google.gdata.client.authn.oauth.OAuthHttpClient.getResponse(OAuthHttpClient.java:44)
at com.google.gdata.client.authn.oauth.OAuthHelper.getUnauthorizedRequestToken(OAuthHelper.java:335)
at Oauth.requestServlet.processRequest(requestServlet.java:56)
at Oauth.requestServlet.doGet(requestServlet.java:88)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:734)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:325)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:226)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
at java.lang.Thread.run(Thread.java:662)
Caused by: java.io.IOException: Server returned HTTP response code: 400 for URL: https://www.google.com/accounts/OAuthGetRequestToken?oauth_callback=callbackUrl.jsp&scope=https%3A%2F%2Fdocs.google.com%2Ffeeds%2F&oauth_nonce=6077341098309&oauth_signature_method=RSA-SHA1&oauth_consumer_key=www.google.com&oauth_timestamp=1296198416&oauth_signature=h%2F0IlDlsMYy2HqXv%2FksrOX3XCm51IwwpoRMjgLwGOJXTwSbZkTP%2FguIH7ZRXfL16DtHAlULhyyvqapCR3pSA%2BKqZdcr5kdHMHWlnRGglHQb54X%2BT8EOWPr9xTQOd0DDbQrLvau6RJhhSFxov95mU2UDQa2vI%2FRoyVMpAGqNUi4c%3D
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:234)
at com.google.gdata.client.authn.oauth.OAuthHttpClient.getResponse(OAuthHttpClient.java:66)
... 30 more
我的错误是什么?在这里,范围 url 肯定是正确的。您也可以为您的 google Url 尝试它。如果错误仍然与 requestUrl 本身有关,请帮助它可能是什么?
此外,当我抛出请求 URL 的请求时,"https://www.google.com/accounts/OAuthGetRequestToken?oauth_callback=callbackUrl.jsp&scope=https%3A%2F%2Fspreadsheets.google.com%2Ffeeds%2Fspreadsheets%2Fprivate%2Ffull&oauth_nonce=7977789106925&oauth_signature_method=RSA-SHA1&oauth_consumer_key=www.google.com&oauth_timestamp=1296200317&oauth_signature=FFNlR4vB%2FOAs3006VGG4wKQkB3TbGDmyg4KN04YvicZfQ6Y9%2FHp5ZVinyhTI5YB%2BQmxFu%2BPXGL3iQcVCr2Ap2Y4abwMnX%2Bi4q3%2FvNMr8g1IGJvJJ%2FPu9x5rIQHiJPNLmCdGCjlXOZLIM5%2BuLv0zDWc%2B6cDFCess0Iuzhh%2BdqP6I%3D",
它给了我错误签名的错误!:(