2013-01-09 105 views
0

嗨,我想實現這個之後tutorial 在本教程OAuth1.0有一個標題OAuthGetRequestTokenoauth_signature在OAuth的谷歌API

,其中,用於獲取請求令牌我們發送POST請求URL

www.google.com/accounts/OAuthGetRequestToken

我發送我的谷歌應用程序引擎代碼POST請求 我的代碼是:

package org.ritesh; 

    import java.io.BufferedInputStream; 
    import java.io.BufferedReader; 
    import java.io.DataInputStream; 
    import java.io.File; 
    import java.io.FileInputStream; 
    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 java.security.KeyFactory; 
    import java.security.NoSuchAlgorithmException; 
    import java.security.PrivateKey; 
    import java.security.spec.EncodedKeySpec; 
    import java.security.spec.InvalidKeySpecException; 
    import java.security.spec.PKCS8EncodedKeySpec; 
    import java.util.Iterator; 
    import java.util.Map; 
    import java.util.Set; 
    import java.util.TreeMap; 

    import com.google.gdata.client.authn.oauth.*; 
    import com.google.gdata.util.common.util.Base64; 
    import com.google.gdata.util.common.util.Base64DecoderException; 
    import com.sun.org.apache.xerces.internal.util.URI; 

    import javax.servlet.ServletContext; 
    import javax.servlet.http.*; 


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

     String myrsakey= "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBALwVoQ3Ksd9gwZY3" 
      +"a7Flz5bf1oCiYe8XSn6vlkaPiA0jBcPJAmACjI023/Z+8KgDbyhlRumTtcateXNZ" 
      +"FVb7q/BKTQWgxK1Fj2XGUkWQz6Nsp/sk54M+R4n3XkTp6W7HhFERE81Iobgy+KtM" 
      +"vr5f/tJbCtLspKSaq2totveKMvMDAgMBAAECgYBIeTke3FzfyyOtI1vO9oEgDM5V" 
      +"sLx16Y6d9EC+na36CeW9xGWy4yiPfXadP9qxkukxMp05gd5IWS30QX5UjxN4ER9j" 
      +"tEDCfDePnrOoi6aUpLXuHoOQbhVIBMTEsFzw9v837a2GOnU0YMgUnGTzC8Ql/3Aw" 
      +"kFPNFqVEG57ItzZYGQJBAO8K8qXiDfUeMUjGLwqbRk5NgM7GVlPI80f3/V2o7EtP" 
      +"T6kr6nvob7ZfgQ9R1STuIPjF+0GartfHZ5x+7tdcZ7cCQQDJbUV6Y41zzQ/Pg/cl" 
      +"VIbZ8Lx9GdtYBaDFeIhGHXDq7Q0I17ztMILJfvx5kKQWGix8ktb0COGX7LxKIwFu" 
      +"GxcVAkEAhoDWf9humhnfCV/aYFF2geDCNZcMRCCyIzC689R1APsji8EWM5paIXgj" 
      +"moclM556FwDvm7552xhsiHYz1iI8iQJANcCMRvHkIJ/7dSRBQtwAtI4yrqvExgOS" 
      +"eMAGlbdrl7W0wcRYrW9Bp6XUmFhKAX/wmTnSVQM9uH47bQlUa16dVQJBAKnnjcgW" 
      +"AmoCYM+YYmi6+fytPYn9W61RNdl1f9rtccDBhsWomgS6O204qJoLX+U/aCkjpPxK" 
      +"IyilkfsZBNupdzA="; 


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

     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"); 
     tree.put("oauth_signature", myrsakey); 
     tree.put("oauth_callback", "https://imehandirattaritesh.appspot.com/authsub"); 
     tree.put("scope", "http://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()); 
      } 


    URL url=new URL("https://www.google.com/accounts/OAuthGetRequestToken?"+datastring); 
        resp.getWriter().println(""+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); 
       resp.getWriter().println("</body></html>"); 




     } 



} 

我正在我的託管域imehandirattaritesh.appspot.com應用程序,我想可能是我把我所有的參數correct.may在教程鏈接簽名是一個非常小的字符串,但在我而言這是一個非常大的字符串我我把它從證書的.pem文件生成的。我是希望我的輸入作爲這樣

oauth_token=ab3cd9j4ks73hf7g&oauth_token_secret=ZXhhbXBsZS5jb20&oauth_callback_confirmed=true 

但作爲代替我收到發送POST請求的響應,

的我.pk8文件的內容
signature_invalidbase_string:POST&https%3A%2F%2Fwww.google.com%2Faccounts%2FOAuthGetRequestToken&oauth_callback%3Dhttps%253A%252F%252Fimehandirattaritesh.appspot.com%252Fauthsub%26oauth_consumer_key%3Dimehandirattaritesh.appspot.com%26oauth_nonce%3D1357733037248%26oauth_signature_method%3DRSA-SHA1%26oauth_timestamp%3D1357733037%26oauth_version%3D1.0%26scope%3Dhttp%253A%252F%252Fwww.google.com%252Fcalendar%252Ffeeds 

我oauth_callback是https://imehandirattaritesh.appspot.com/authsub

我試圖刪除像地獄這樣的錯誤,但每次都失敗可以任何人請幫助如何刪除此錯誤或資源或鏈接從中我將得到任何help.please不建議我使用Java客戶端OAuth1.0 .i必須使用谷歌預測api,然後從我的應用程序調用,並在java客戶端庫中沒有相關的功能調用谷歌預測api.please幫助我如何消除此錯誤並得到正確的輸出

回答

1

它清楚地表示無效簽名。你的RSA密鑰不是你的簽名。請參考同一教程的簽名請求以創建有效的簽名。 https://developers.google.com/accounts/docs/OAuth_ref#SigningOAuth。的 http://oauth.net/core/1.0/#signing_process

+0

那麼我應該到位oauth_signature什麼價值

而第9.3?我無法理解,請簡化它! –

+0

像這樣的東西可以工作 /** *用給定的鍵和提供的算法標記數據。 */ 私有靜態字節[]符號(專用密鑰鍵, 字符串數據) 拋出GeneralSecurityException { 簽名簽名= Signature.getInstance( 「SHA1withRSA」); signature.initSign(key); signature.update(data.getBytes()); return signature.sign(); } –

+0

我試過很多的組合,但沒有什麼工作out.canü請閱讀並accrately告訴我應該怎樣oauth_signature –