我正在使用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="));
    }
}


非常感谢你的帮助。

最佳答案

Grant类型应设置为post参数:

List<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("grant_type", "client_credentials"));
post.setEntity(new UrlEncodedFormEntity(parameters));

关于java - Twitter API:有人可以建议如何在HttpPost请求的正文中发送参数吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17097828/

10-09 04:51