我正在使用 Twitter api 1.1 下面是从 api 1.1 获取不记名令牌的代码。我需要设置请求的内容类型并在请求正文中发送 grant type=client_credentials 。我已经在标头中发送了编码的 consumer_key 和 consumer_secret。
有人可以指导我如何在 HttpPost 请求的正文中设置内容和发送参数。
----------------------代码从这里开始-------------- -------------------------------------------
package com.memorial.twitter;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import javax.net.ssl.HttpsURLConnection;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.http.HttpEntity;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.client.HttpClient;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
//import java.io.IOException;
import java.io.InputStreamReader;
//import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
//import java.net.MalformedURLException;
//import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder;
import java.security.GeneralSecurityException;
import java.util.Calendar;
//import java.util.Date;
import java.util.UUID;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import quicktime.streaming.Stream;
import com.sun.tools.javac.util.Convert;
@WebServlet("/twitter_final")
public class Twitter_Final extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("I am post");
String handle=request.getParameter("TwitterID");
System.out.println(handle);
HttpClient httpclient = new DefaultHttpClient();
String consumer_key="consumer_key";
String consumer_secret="consumer_secret";
String authorization_header_string=consumer_key+":"+consumer_secret;
authorization_header_string=URLEncoder.encode(authorization_header_string, "UTF-8");
System.out.println(authorization_header_string);
HttpPost httppost = new HttpPost("https://api.twitter.com/oauth/token?grant_type=client_credentials");
httppost.setHeader("Authorization",authorization_header_string);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(httppost, responseHandler);
System.out.println(responseBody);
// oauth_token = responseBody.substring(responseBody.indexOf("oauth_token=") + 12, responseBody.indexOf("&oauth_token_secret="));
}
}
非常感谢你的帮助。