Google app engine 在OAuth中对基本字符串进行签名

Google app engine 在OAuth中对基本字符串进行签名,google-app-engine,oauth,2-legged,Google App Engine,Oauth,2 Legged,您好,我正在尝试实现OAuth1.0。在本教程之后,在本教程中有一个标题OAuthGetRequestToken 其中,为了获取请求令牌,我们必须向URL发送post请求 www.google.com/accounts/OAuthGetRequestToken 我在google app engine的代码中发送post请求我的代码是: public class HelloWorldServlet extends HttpServlet { @SuppressWarnings({ "unc

您好,我正在尝试实现OAuth1.0。在本教程之后,在本教程中有一个标题OAuthGetRequestToken

其中,为了获取请求令牌,我们必须向URL发送post请求

www.google.com/accounts/OAuthGetRequestToken

我在google app engine的代码中发送post请求我的代码是:

public class HelloWorldServlet extends HttpServlet {
    @SuppressWarnings({ "unchecked", "unchecked" })
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {



        resp.setContentType("text/html");
         resp.getWriter().println("<html><head> <meta name=\"google-site-verification\" content=\"OBFeK6hFEbTkNdcYc-SQNH9tCTpcht-HkUdj6IgCaLg\" </head>");

        resp.getWriter().println("<body>Hello, world");
        //String post="key=AIzaSyBgmwbZaW3-1uaVOQ9UqlyHAUxvQtHe7X0&oauth_consumer_key=iriteshmehandiratta.appspot.com";
        //String param= "&oauth_callback=\"https://www.iriteshmehandiratta.appspot.com\"&scope=\"http://www.google.com/calendar/feeds\""; 


        //URL url=new URL("https://www.googleapis.com/prediction/v1.5/trainedmodels/10/predict?");
  TreeMap<String,String> tree=new TreeMap<String,String>();
  tree.put("oauth_version","1.0");
  tree.put("oauth_nonce", System.currentTimeMillis()+"");
  tree.put("oauth_timestamp",System.currentTimeMillis()/1000+"");
  tree.put("oauth_consumer_key", "imehandirattaritesh.appspot.com");
  tree.put("oauth_signature_method", "RSA-SHA1");

  ServletContext context = getServletContext();
  PrivateKey privKey = getPrivateKey(context,"/myrsakey11.pk8");




  tree.put("oauth_callback", "https://imehandirattaritesh.appspot.com/authsub");
  tree.put("scope", "https://www.google.com/calendar/feeds");
  Set set = tree.entrySet(); 

  Iterator<Map.Entry<String, String>> i = set.iterator(); 
  String datastring="";
  Map.Entry me=(Map.Entry)i.next();
 datastring=me.getKey()+"=";
 datastring+=me.getValue();

 while(i.hasNext()) { 
      me = (Map.Entry)i.next(); 
      datastring+="&"+me.getKey()+"="; 
      datastring+=(me.getValue()); 
      } 
  String data_string="GET&https://www.google.com/accounts/OAuthGetRequestToken&"+datastring;
 byte[] xx11;
 String str = null;
try {
    xx11 = sign(privKey,data_string);
    str=new String(xx11);
resp.getWriter().println(str);

} catch (GeneralSecurityException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}



URL url=new URL("https://www.google.com/accounts/OAuthGetRequestToken?"+str); 
            //  resp.getWriter().println(""+datastring);
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

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

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

        urlConnection.setRequestMethod("GET");

        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("response");
           resp.getWriter().println(xx);
           resp.getWriter().println("</body></html>");




    }


    public static PrivateKey getPrivateKey(ServletContext context,String privKeyFileName) throws IOException {

        InputStream resourceContent = context.getResourceAsStream("/WEB-INF/myrsakey11.pk8");
         // FileInputStream fis = new FileInputStream(privKeyFile);
          DataInputStream dis  = new DataInputStream(resourceContent);

          @SuppressWarnings("deprecation")
        String str="";
          String str1="";
          while((str=dis.readLine())!=null)
          {
              str1+=str;

          }



          String BEGIN = "-----BEGIN PRIVATE KEY-----";
          String END = "-----END PRIVATE KEY-----";
         // String str = new String(privKeyBytes);

          if (str1.contains(BEGIN) && str1.contains(END)) {
            str1 = str1.substring(BEGIN.length(), str1.lastIndexOf(END));
          }

          KeyFactory fac;
        try {
            fac = KeyFactory.getInstance("RSA");

              EncodedKeySpec privKeySpec= new PKCS8EncodedKeySpec(Base64.decode(str1));
              return fac.generatePrivate(privKeySpec);

        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Base64DecoderException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvalidKeySpecException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         return null;

          }


    byte[] sign(PrivateKey key, String data) throws GeneralSecurityException {
        Signature signature = Signature.getInstance("SHA1withRSA"); 
        signature.initSign(key);

        signature.update(data.getBytes());
        return signature.sign(); 
        }
}
公共类HelloWorldServlet扩展了HttpServlet{
@SuppressWarnings({“未选中”、“未选中”})
公共无效数据集(HttpServletRequest请求、HttpServletResponse响应)
抛出IOException{
分别为setContentType(“文本/html”);

resp.getWriter().println(“我建议您使用一个用于执行OAuth的接口。从长远来看,这将更容易,并且您不必担心调试协议

 F????T???&??$????????l:v????x???}??U-'?"?????U?[?kr^?G?(? ???qT0??]??j???5??`??$??AD??T??@<t?,#:`V????????????