嗨,我正在尝试实现OAuth1.0以下本教程在本教程中,有一个标题OAuthGetRequestToken
在中,为了获得请求令牌,我们必须向URL
发送post请求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上托管我的应用程序,我想可能我把我所有的参数都正确了。可能在教程链接签名是一个非常小的字符串,但在我的情况下,它是一个非常大的字符串,我把我的。pk8文件的内容,它是由证书。pem文件生成的。我希望我的输入像这样
oauth_token=ab3cd9j4ks73hf7g&oauth_token_secret=ZXhhbXBsZS5jb20&oauth_callback_confirmed=true
但是在这个地方,我得到了发送post请求的响应
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
my oauth_callback is https://imehandirattaritesh.appspot.com/authsub
我正试图删除这个错误,但每次都失败了,请任何人帮助如何删除这个错误或资源或链接,我将得到任何帮助。请不要建议我使用java客户端OAuth1.0。我必须使用谷歌预测api,然后从我的应用程序和java客户端库中调用,没有与调用谷歌预测api相关的功能。请帮助我如何消除这个错误,并得到正确的输出
很明显签名无效。您的RSA密钥不是您的签名。请参考同一教程的签名请求来创建有效的签名。https://developers.google.com/accounts/docs/OAuth_ref SigningOAuth .
和http://oauth.net/core/1.0/signing_process