Julienh / Sharrre

Make your sharing widget! Sharrre is a jQuery plugin that allows you to create nice widgets sharing for Facebook, Twitter, Google Plus (with PHP script) and more.
sharrre.com
MIT License
1.04k stars 411 forks source link

Running G+ button without PHP? #162

Open OB80 opened 11 years ago

OB80 commented 11 years ago

I'm building a site that will not have PHP support but would like the G+ button. Is there an alternative?

Great plugin btw :)

tmorehouse commented 10 years ago

You could make a modification to the javascript to get the counts. You can get google plus counts via a CORS request, but you need to have a google API key (and restrict it to your website domain, so other people don't steal it and use up your daily quota) so that they will enable the CORS headers on the JSON response.

There would be a need to add a new parameter for the google+ key. But the gist of the code to get the counts looks like this:

function getSharesGoogleplus(el,href) {
    // el => element to update
    // href => url to check
    $.ajax({
        url: 'https://clients6.google.com/rpc',
        type: 'post',
        dataType: 'json',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify([ // request data must be a JSON ARRAY of HASHES
            {
                key: "<your google api key>", // replace with your API Key
                method: "pos.plusones.get",
                id: "p",
                params:{
                    id: href, // URL to get +1's for
                    nolog: true,
                    source: "widget",
                    userId: "@viewer",
                    groupId: "@self"
                },
                jsonrpc:"2.0",
                apiVersion:"v1"
            }
        ])
    }).done(function(data) {
        if(data && data.length && data[0].metadata & data[0].metadata.globalCounts) {
            $(el).text(data[0].metadata.globalCounts.count);
        }
    });
}