0

嗨,我正在制作一个 Google 应用程序,我在其中向 Google API 发送请求。我正在尝试实现 2legged OAuth1.0 以获取请求令牌(我正在关注此链接

在此链接中,我正在向 /accounts/oauth/GetRequesttoken 发送 POST 请求。下面是我用于发送 post 请求的 servlet 代码。评论很好,请阅读:

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

        }
    }

当我运行它时,我期望响应应该如下所示!

oauth_token=ab3cd9j4ks73hf7g&oauth_token_secret=ZXhhbXBsZS5jb20&oauth_callback_confirmed=true

但我得到的回应是:

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 

如果我错了,请指出如何纠正这个问题:

4

1 回答 1

0

我建议您尝试使用该google-oauth-java-client库。它经过测试可以在 AppEngine 上运行。

于 2013-01-07T15:58:09.580 回答