【问题标题】:How to revoke an authentication token client side against the Google Api如何针对 Google Api 撤销身份验证令牌客户端
【发布时间】:2012-10-09 22:29:08
【问题描述】:

我正在尝试使用 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

【问题讨论】:

  • 根据错误,您似乎无法在此客户端上执行此操作。也许您需要一个服务器端脚本来处理来自您的域内的请求。您还可以探索this 解决方案。这是使用该解决方案的jsFiddle 示例。

标签: javascript jquery oauth google-api google-analytics-api


【解决方案1】:

经过一些研究,我发现您可以通过在 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
  }
});

【讨论】:

  • 这很好用,但是有没有办法在没有 jquery 的情况下做到这一点?
【解决方案2】:

继续@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");
     },
});

哪个有效。

【讨论】:

    【解决方案3】:

    现在 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)
            }
        });
    

    【讨论】:

      猜你喜欢
      • 2023-03-12
      • 1970-01-01
      • 2013-08-19
      • 2016-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-29
      • 2020-11-03
      相关资源
      最近更新 更多