package com.viadeo.servlets; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.json.JSONException; import org.json.JSONObject; public class Oauth2SampleCodeServlet extends HttpServlet { private static final long serialVersionUID = 1L; private static final String clientId = "[your client id]"; private static final String clientSecret = "[your client secret]"; private static final String oauthBase = "https://secure.viadeo.com/oauth-provider"; private static final String authorizeURL = oauthBase + "/authorize2"; private static final String accessTokenURL = oauthBase + "/access_token2"; private static final String apiBase = "https://api.viadeo.com"; @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String code = request.getParameter("code"); String redirectUri = request.getRequestURL().toString(); if (code == null || "".equals(code)) { // Step 1 - Redirect user to provider for authorization String url = authorizeURL + "?response_type=code&display=popup&lang=en&client_id=" + clientId + "&redirect_uri=" + redirectUri; response.sendRedirect(url); } else { // Step 2 - Exchange for access grant String urlParameters = "grant_type=authorization_code&client_id=" + clientId + "&client_secret=" + clientSecret + "&redirect_uri=" + redirectUri + "&code=" + code; String resp = executePost(accessTokenURL, urlParameters); String accessToken = ""; try { JSONObject json = new JSONObject(resp); accessToken = json.getString("access_token"); } catch (JSONException e) { } // Step 3 - Create connection response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY); response.setHeader("Location", apiBase + "/me.xml?access_token=" + accessToken); } } // POST request helper private String executePost(String targetURL, String urlParameters) { URL url; HttpURLConnection connection = null; try { // Create connection url = new URL(targetURL); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length)); connection.setRequestProperty("Content-Language", "en-US"); connection.setUseCaches(false); connection.setDoInput(true); connection.setDoOutput(true); // Send request DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); wr.writeBytes(urlParameters); wr.flush(); wr.close(); // Get Response InputStream is = connection.getInputStream(); BufferedReader rd = new BufferedReader(new InputStreamReader(is)); String line; StringBuffer response = new StringBuffer(); while ((line = rd.readLine()) != null) { response.append(line); response.append('\r'); } rd.close(); return response.toString(); } catch (Exception e) { e.printStackTrace(); return null; } finally { if (connection != null) { connection.disconnect(); } } } }
import java.io.*; import java.net.*; public class Lookup { public static void main(String[] args) throws Exception { System.out.println("Hello World!"); String client = "api"; String apikey = "ABQIAAAAdGnGSqPru2GZ5ZCwFZsLDhQM9mDvw_EbadXXtLQ5SlzX_s0ijA"; String appver = "1.5.2"; String pver = "3.0"; String url = "http://www.yahoo.com"; String uri = "https://sb-ssl.google.com/safebrowsing/api/lookup?client="+client+"&apikey="+apikey+"&appver="+appver+"&pver="+pver;//+"&url="+url; URL lookUrl = new URL(uri); HttpURLConnection htCon = (HttpURLConnection) lookUrl.openConnection(); htCon.setDoInput(true); htCon.setDoOutput(true); //OutputStream os = htCon.getOutputStream(); PrintStream pos = new PrintStream(htCon.getOutputStream()); pos.println(2); pos.println("http://www.asdrediff.com"); pos.println("http://www.yahooabds.com"); pos.close(); System.out.println("respose code:" + htCon.getResponseCode()); BufferedReader br = new BufferedReader(new InputStreamReader(htCon.getInputStream())); String str = ""; while((str = br.readLine()) != null) { System.out.println(str); } br.close(); htCon.disconnect(); System.out.println("end"); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)