嗨,我正在制作一个谷歌应用程序,我正在向谷歌API发送请求。我正试图实现2legs OAuth1.0获取请求令牌(我正在跟踪此链接)
在这个链接中,我发送aPOST请求到/accounts/oauth/GetRequesttoken。下面是我发送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
我错了,请告诉我如何纠正这个问题:
我建议您尝试使用google-oauth-java-client
库。