开发者

Error getting HTTP response

I am trying to generate Request Token using following code :

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();
        }
    }

For sure, there is no compile time error here. But, getting runtime error i.e. :

   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开发者_StackOverflow社区.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

What is my error here ? Here, the scope url is correct for sure. You can try it for your google Url as well. If still the error has been with the requestUrl itself, please help what it can be ?

Also, when I throw the request to request Url that is "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", it gives me error of Wrong Signature !!! :(


It's because the URL which is causing an HTTP 400 error. See:

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 

Verify your URL is correct. More info about HTTP 400 Error, click here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜