如何针对Google Api撤消身份验证令牌客户端

时间:2012-10-09 22:29:09

标签: javascript jquery oauth google-api google-analytics-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起源   Access-Control-Allow-Origin不允许http://balblabla.com

2 个答案:

答案 0 :(得分: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 :(得分:0)

现在jsonp不起作用。他们已将内容类型更改为“ application / x-www-form-urlencoded”,

    $.ajax({
        type: 'GET',
        url: "https://accounts.google.com/o/oauth2/revoke?token=dsfgdfsg.98sdfgsdfg9sd8fgsdfgs.sdfg89dsfg",
        async: false,
        contentType: "application/x-www-form-urlencoded",
        success: function(nullResponse) {
            // Do something now that user is disconnected
            // The response is always undefined.
        },
        error: function(e) {
            console.log(e)
        }
    });