How to get Google +1 pluses count in Java

Here is a simple way to manually get the Google +1 pluses count for your web pages using Java. This is based on a PHP implementation from this blog post.

Google +1 API pluses count is available through the following endpoint:

https://clients6.google.com/rpc?
	key=AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ

The service is a JSON RPC setup which requires a POST request using JSON data with the following format. Just supply the URL of the target page replacing the PAGE_URL in the sample below.

[
{"method":
        "pos.plusones.get",
	"id":"p",
	"params":{"nolog":true,"id":"PAGE_URL",
            "source":"widget",
            "userId":"@viewer",
            "groupId":"@self"},
	"jsonrpc":"2.0","key":"p","apiVersion":"v1"
}
]

The response should look like the following:

[
{"result": 
    { "kind": "pos#plusones", 
        "id": "PAGE_URL", 
        "isSetByViewer": false, 
        "metadata": 
            {"type": "URL", 
		"globalCounts": {"count": 51.0} 
            } 
    } "id": "p"
}
]

The Java Code

Below is how we implemented the request and extract the count from JSON data in Java.

public String plusCount(String pageUrl) {

    String plusCount = "-1";

    StringBuilder postData =
            new StringBuilder("{\"method\":\"pos.plusones.get\",");
    postData.append("\"id\":\"p\",");
    postData.append("\"params\":");
    postData.append("{\"nolog\":true,");
    postData.append("\"id\":\"");
    postData.append(pageUrl);
    postData.append("\",");
    postData.append("\"source\":\"widget\",");
    postData.append("\"userId\":\"@viewer\",");
    postData.append("\"groupId":\"@self\"");
    postData.append("},");
    postData.append("\"jsonrpc\":\"2.0\",");
    postData.append("\"key\":\"p\",");
    postData.append("\"apiVersion\":\"v1\"}");

    StringBuilder response = new StringBuilder();

    try {
        URL url = new URL("https://clients6.google.com/rpc?"
                + "key=AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ");

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.addRequestProperty("User-Agent", "Mozilla");
        connection.addRequestProperty("Content-Type", "application/json");
        connection.setRequestMethod("POST");

        connection.addRequestProperty("Content-Length", "" + postData.length());
        connection.setDoOutput(true);
        connection.setDoInput(true);

        DataOutputStream outputStream = new DataOutputStream(
                connection.getOutputStream());
        outputStream.writeBytes(postData.toString());

        outputStream.flush();
        outputStream.close();

        InputStream input = connection.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(input));

        String line;

        while ((line = reader.readLine()) != null) {
            response.append(line);
        }

        reader.close();

    } catch (IOException e) {
        // Do something about the error
        return plusCount;
    }

    try {

        Gson gs = new Gson();
        Map<String, Map<String, Object>> map = new HashMap();
        map = gs.fromJson(response.toString(), map.getClass());
        
        if (map != null) {
            Map<String, Object> result = (Map<String, Object>) map.get("result");
            Map<String, Object> metaData = (Map<String, Object>) result.get("metadata");
            if (metaData != null) {
                Map<String, Object> globalCounts =
                        (Map<String, Object>) metaData.get("globalCounts");
                if (globalCounts != null) {
                    Double C = (Double) globalCounts.get("count");
                    plusCount = "" + C.intValue();
                }
            }
        }

    } catch (Exception e) {
        // return "-1";
        // Do something about the error
    }

    return plusCount;
}

That's it, good luck.

If you like the article, please share.
(Site URL pattern has changed as a result social actions counter was reset.)



Comment icon   Comments (Newest first)