0

Hi I am making a Google app in which I am sending request to Google API. I am trying to implement 2legged OAuth1.0 for getting request token(I am following this link)

In this link I am sending aPOST request to /accounts/oauth/GetRequesttoken.below is my servlet code for sending post request .It is well commented please read it :

   package org.ritesh;
    //import classes
    import java.io.BufferedInputStream;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.net.URLEncoder;

    import javax.servlet.http.*;

    import com.google.gdata.client.authn.oauth.GoogleOAuthParameters;

    @SuppressWarnings("serial")
    public class HelloWorldServlet extends HttpServlet {
method for handling get request    
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws IOException {

            resp.setContentType("text/plain");
            resp.getWriter().println("Hello, world");

//setting oauth_version parameter to 1.0      

        String datastring=URLEncoder.encode("oauth_version", "UTF-8")+"="+URLEncoder.encode("1.0", "UTF-8");

 //setting oauth_nonce a unique value so assigned a time stamp     

      datastring+="&"+URLEncoder.encode("oauth_nonce", "UTF-8")+"="+URLEncoder.encode(System.currentTimeMillis()+"","UTF-8");
        //setting parameter oauth_timestamp to current time in setting

      datastring+="&"+URLEncoder.encode("oauth_timestamp", "UTF-8")+"="+URLEncoder.encode(System.currentTimeMillis()/1000+"","UTF-8");

//its my domain name i am hosting my app on domain iriteshmehandiratta.appspot.com

        datastring+="&"+URLEncoder.encode("oauth_consumer_key","UTF-8")+"="+URLEncoder.encode("iriteshmehandiratta.appspot.com","UTF-8");
  //my .pem file contain RSA fingerprints so i include RSA-SHA1 for //oauth_signature_method    

        datastring+="&"+URLEncoder.encode("oauth_signature_method","UTF-8")+"="+URLEncoder.encode("RSA-SHA1", "UTF-8"); 
  //consumer_secret that i got when i successfully registered my app  on google Manage domain link

        datastring+="&"+URLEncoder.encode("oauth_signature", "UTF-8")+"="+URLEncoder.encode("eOcmMdWaO2O14JyK1TbYPS09", "UTF-8");
    //setting oauth_callback parameter

    datastring+="&"+URLEncoder.encode("oauth_callback","UTF-8")+"="+URLEncoder.encode("https://www.iriteshmehandiratta.appspot.com/authsub","UTF-8");

    datastring+="&"+URLEncoder.encode("scope","UTF-8")+"="+URLEncoder.encode("http://www.google.com/calendar/feeds","UTF-8");


    URL url=new URL("https://www.google.com/accounts/OAuthGetRequestToken?"+datastring); 

            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

            urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

            urlConnection.setRequestProperty("Authorization", "OAuth");

            urlConnection.setRequestMethod("POST");

            urlConnection.setDoOutput(true);

            BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

              resp.getWriter().println( urlConnection.getResponseCode());

               String xx="";

               String xx1="";

               while((xx1=in.readLine()) != null)

               {
                   xx+=xx1;


               }
               resp.getWriter().println(xx);

        }
    }

When I run this I am expecting response should be like below!!

oauth_token=ab3cd9j4ks73hf7g&oauth_token_secret=ZXhhbXBsZS5jb20&oauth_callback_confirmed=true

But the response I am getting is:

signature_invalidbase_string:POST&https%3A%2F%2Fwww.google.com%2Faccounts%2FOAuthGetRequestToken&oauth_callback%3Dhttps%253A%252F%252Fwww.iriteshmehandiratta.appspot.com%252Fauthsub%26oauth_consumer_key%3Diriteshmehandiratta.appspot.com%26oauth_nonce%3D1357576190878%26oauth_signature_method%3DRSA-SHA1%26oauth_timestamp%3D1357576190%26oauth_version%3D1.0%26scope%3Dhttp%253A%252F%252Fwww.google.com%252Fcalendar%252Ffeeds 

Where I am wrong please point me how to rectify this problem:

Sachin Prasad
  • 5,365
  • 12
  • 54
  • 101
mathlearner
  • 7,509
  • 31
  • 126
  • 189

1 Answers1

0

I suggest you try using the google-oauth-java-client library. It's tested to work on AppEngine.

Peter Knego
  • 79,991
  • 11
  • 123
  • 154