2012-10-09 47 views
1

我正试图使用​​Google Api客户端代码撤销令牌。如何撤销对Google API的身份验证令牌客户端

我的代码看起来是这样的:

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

而且我收到以下错误?

的XMLHttpRequest不能加载 https://accounts.google.com/o/oauth2/revoke?token=tokenishere产地 http://balblabla.com不受访问控制允许来源允许的。

+2

根据错误,它看起来像你不能在这个客户端上做到这一点。也许你需要一个服务器端脚本来处理你的域内的请求。您还可以探索[this](http://stackoverflow.com/questions/3506208/jquery-ajax-cross-domain)解决方案。这是使用该解决方案的[jsFiddle](http://jsfiddle.net/hwjPS/)示例。 – krg

回答

0

从@ KRG的评论继:基于它看起来就像你不能这样做此客户端上的错误

。也许你需要一个服务器端脚本来处理你的域内的请求。您还可以探索this解决方案。以下是使用该解决方案的jsFiddle示例。

我这样做在服务器端,使用相同的代码:

$.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"); 
    }, 
}); 

其中工程。

5

经过一番研究,我发现你可以通过在jquery ajax请求中指定一个dataType:'jsonp'选项来绕过cors限制。相关信息在这里:https://developers.google.com/+/web/signin/disconnect

$.ajax({ 
    type: 'GET', 
    url: revokeUrl, 
    async: false, 
    contentType: "application/json", 
    dataType: 'jsonp', 
    success: function(nullResponse) { 
    // Do something now that user is disconnected 
    // The response is always undefined. 
    }, 
    error: function(e) { 
    // Handle the error 
    // console.log(e); 
    // You could point users to manually disconnect if unsuccessful 
    // https://plus.google.com/apps 
    } 
}); 
+1

这很好,但有没有办法做到这一点没有jQuery? – mhatch