Search code examples
javascriptjqueryoauthgoogle-apigoogle-analytics-api

How to revoke an authentication token client side against the Google Api


I am trying to revoke a token using the Google Api client side code.

My code looks something like this:

$.get("https://accounts.google.com/o/oauth2/revoke?token=" + accessToken, function () {
        window.location.reload();
    }); 

And I am getting the following error?

XMLHttpRequest cannot load https://accounts.google.com/o/oauth2/revoke?token=tokenishere Origin http://balblabla.com is not allowed by Access-Control-Allow-Origin.


Solution

  • Following on from @krg's comment:

    Based on the error it looks like you cannot do this on this client. Perhaps you'll need a server-side script to handle the request from within your domain. You can also explore this solution. Here's a jsFiddle example using the solution.

    I have done this on the server side, using the same code:

    $.ajax({
         url:"https://accounts.google.com/o/oauth2/revoke?token=10100101",
         dataType: 'jsonp', // Notice! JSONP <-- P (lowercase)
         success:function(json){
             console.log(arguments);
             // do stuff with json (in this case an array)
             alert("Success");
         },
         error:function(){
             alert("Error");
         },
    });
    

    which works.