您好我从服务器动态获取access_token值以获取servlet请求:现在我想将access_taken值重新检入我的servlet程序。
{
"access_token":"AQWP_EVkqdc7E0wD09J6msmjnUlvilhP304gUIDzl6KKgNxwnHyz_W9nOxS0IroDJwEfVr3n3O-IC9YKc3bjyuwYRm6qbKRiP3A2AzuDjo8ohZERZFRCMyfjjqqDjNJ5J5ReCQDhkFJam51eiqsOeXDg4U_c9XJzc1dUx7Qxck0p9RNE0",
"expires_in": 5183999
}
我的代码如下:
public class Demo extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String PROTECTED_RESOURCE_URL = "http://api.linkedin.com/v1/people/~:(first-name,last-name,email-address)";
/**
* @see HttpServlet#HttpServlet()
*/
public Demo() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
PrintWriter out=response.getWriter();
String authCode = request.getParameter("code");
request.setAttribute("authCode",authCode);
out.println(authCode);
doPost(request,response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
PrintWriter out=response.getWriter();
Object url=request.getAttribute("authCode");
response.sendRedirect("https://www.linkedin.com/uas/oauth2/accessToken?grant_type=authorization_code&code="+url+"&redirect_uri=http://localhost:8080/LinkedinMails/dem&client_id=xxxxxxxxxxxxx&client_secret=xxxxxxxxxxxxxxx");
}
}
谢谢。
答案 0 :(得分:0)
public String doGet(String url) throws Exception {
URL urlObj = new URL(url);
HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
conn.setDoOutput(true);
String line;
StringBuffer buffer = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
reader.close();
conn.disconnect();
return buffer.toString();
}
现在,您可以从servlet调用此方法以在servlet中获取服务器响应。希望它对你有用。
答案 1 :(得分:0)
您可以使用Gson或Jackson库来传达您的JSON响应。 使用杰克逊:
new ObjectMapper().readValue(result, LinkedIn.class);
其中,结果是您的JSON包含access_Token
和expires
_ in
LinkedIn.java
@JsonIgnoreProperties(ignoreUnknown=true)
public class LinkedIn
{
@JsonProperty("access_token")
private String accessToken;
@JsonProperty("expires_in")
private long expiresIn;
// Getters and setters for expiresOn and accessToken
}
在Servlet / Controller中
LinkedIn l= new ObjectMapper().readValue(result, LinkedIn.class); // Result is your JSON response which has access token and expires from LinkedIn
l.getAccessToken() ; // Returns you access token
l.getExpiresIn() // Returns you the expires time frame
}
使用JSONObject
String s="{\n" +
" \"access_token\":\"AQWP_EVkqdc7E0wD09J6msmjnUlvilhP304gUIDzl6KKgNxwnHyz_W9nOxS0IroDJwEfVr3n3O-IC9YKc3bjyuwYRm6qbKRiP3A2AzuDjo8ohZERZFRCMyfjjqqDjNJ5J5ReCQDhkFJam51eiqsOeXDg4U_c9XJzc1dUx7Qxck0p9RNE0\",\n" +
" \"expires_in\": 5183999\n" +
"}"; // This contains your access token JSON
JSONParser parser = new JSONParser();
JSONObject o = (JSONObject) parser.parse(s);
System.out.println("Access Token: "+o.get("access_token")); //returns your access token
System.out.println("Expires: "+o.get("expires_in"));
获取访问令牌JSON:
在你的Demo
Servlet中,doPost方法代替sendRedirect做一个
http Post(使用Apache Http Client或java.net)调用url。
完成HTTP Post后,您将获得一个Http响应,该响应将是application / json内容。所以得到这个String内容(包含application / json)。此字符串内容是LinkedIn发送给您的access_token。收到回复后,请执行以下操作:
JSONParser parser = new JSONParser();
JSONObject o = (JSONObject) parser.parse(responseString); // responseString is the JSON that you got from LinkedIn.
System.out.println("Access Token: "+o.get("access_token")); //returns your access token
System.out.println("Expires: "+o.get("expires_in"));