【问题标题】:Google+ CreateActivity returning "401 Unauthorized" for certain accountsGoogle+ CreateActivity 为某些帐户返回“401 Unauthorized”
【发布时间】:2014-02-01 14:45:27
【问题描述】:

我有一个 Google+ 应用正在为某些测试帐户成功编写应用活动,但其他应用返回 401 Unauthorized 错误:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "unauthorized",
    "message": "Unauthorized"
   }
  ],
  "code": 401,
  "message": "Unauthorized"
 }
}

我也在响应头中注意到了这一点:

WWW-AuthenticateBearer realm="https://www.google.com/accounts/AuthSubRequest", error=invalid_token

这似乎表明令牌无效,但我不确定我是如何错误地使用 gapi.auth.authorize 的……尤其是因为该脚本使用某些测试帐户可以完美运行,并且可以毫无问题地向 G+ 写入时刻。如果有人可以提出某些测试帐户可能无法验证编写应用程序活动的任何原因(或以下代码有任何问题),请告诉我!

// first call gapi.auth.authorize with immediate:true:
 _checkAuth = function _checkAuth(){
        gapi.auth.authorize({
            client_id : 'XXXXXXXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com',
            scope : 'https://www.googleapis.com/auth/plus.login',
            request_visible_actions : 'http://schemas.google.com/CreateActivity',
            immediate : true
        }, function(authResult){
            if(!authResult || authResult.error){
               _signIn();
            }else{
                _performAction();
            }
        });
    },

// if not logged in, call gapi.auth.authorize with immediate:false:
_signIn = function _signIn(){
        gapi.auth.authorize({
            client_id : 'XXXXXXXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com',
            scope : 'https://www.googleapis.com/auth/plus.login',
            request_visible_actions : 'http://schemas.google.com/CreateActivity',
            immediate : false
        }, function(token){
            gapi.auth.setToken(token);
           _performAction();
        });
    },

// create activity
_performAction = function _performAction(){
        gapi.client.load('plus','v1', function(){
            gapi.client.setApiKey('XXXXXXXXXXXXXXXXXXXXXXX');
            var payload = {
                "type" : 'http://schemas.google.com/CreateActivity'
            };
            payload.target = {
                "id" : "myappid",
                "image" : "http://www.example.com/xxxxxxxxxxx.jpg",
                "type" : 'http://schema.org/CreativeWork',
                "description" : "description of activity",
                "name" : "name of activity"
            };
            var args = {
                'path' : '/plus/v1/people/me/moments/vault',
                'method' : 'POST',
                'body' : JSON.stringify(payload),
                'callback' : function(response) {
                    console.log(response); // error
                }
            };
            // triggers 401 error for some accounts
            gapi.client.request(args);
        });
},

【问题讨论】:

    标签: javascript google-api google-plus google-authentication google-api-js-client


    【解决方案1】:

    一些关于原因的想法:

    • 是否有一些帐户升级到了 Google+ 而其他没有?这可能是 Google+ 方面的错误,但如果帐户尚未升级,则连接应用等的概念可能会被解释为错误。
    • 您是否可能正在尝试编写不同类型的应用活动,或者这是编写应用活动的唯一代码?
    • 您是否存储访问令牌?这些在 3600 秒后过期。你可以verify your tokens here
    • 为什么要调用 gapi.client.setApiKey?这应该用于未经身份验证的 API 调用,而编写应用程序活动需要授权凭据。客户端库将使用用户授权您的应用后捕获的凭据。
    • 您为什么不使用 Google+ API 客户端库?我会给你一个使用客户端库编写应用程序活动的示例,希望对你有所帮助。

    请求对演示中使用的应用程序活动的写入权限的示例按钮:

    <button class="g-signin"
        data-scope="https://www.googleapis.com/auth/plus.login"
        data-requestvisibleactions=
        "http://schemas.google.com/AddActivity http://schemas.google.com/ListenActivity"
        data-clientId="268858962829.apps.googleusercontent.com"
        data-callback="onSignInCallback"
        data-theme="dark"
        data-cookiepolicy="single_host_origin">
    </button>
    

    使用客户端库编写应用活动的示例。

        writeAddActivity: function(url){
          var payload = {
            "type":"http:\/\/schemas.google.com\/AddActivity",
            "startDate": "2012-10-31T23:59:59.999Z"
          };
          if (url != undefined){
            payload.target = {
              'url' : 'https://developers.google.com/+/plugins/snippet/examples/thing'
            };
          }else{
            payload.target = {
              "id" : "replacewithuniqueidforaddtarget",
              "image" : "http:\/\/www.google.com\/s2\/static\/images\/GoogleyEyes.png",
              "type" : "http:\/\/schema.org\/CreativeWork",
              "description" : "The description for the activity",
              "name":"An example of AddActivity"
            };
          }
          this.writeAppActivity(payload);
        },
        writeAppActivity: function(payload){
    
          gapi.client.plus.moments.insert(
              {  'userId' : 'me',
                 'collection' : 'vault',
                 'resource' : payload
              }).execute(function(result){
                  console.log(result);
              });
        }
    

    工作演示:

    http://wheresgus.com/appactivitiesdemo/

    【讨论】:

    • 非常感谢您的回复。您的演示适用于我遇到问题的测试帐户,它编写应用程序活动没有任何问题......但是,接下来我将其源代码复制到我的环境并更改了 client_id 和有效负载目标 id,再次得到 401 错误。我正在查看开发人员控制台,一切似乎都设置正确(Javascript 起源等),但我想知道是否有需要在某处调整的设置。
    • 您正在使用的所有操作是否都设置在 request_visible_actions 中?我在答案中提供了一个示例按钮 - 您将知道您正确设置它们,因为活动类型将出现在同意对话框中。
    • @class 你用什么来重装“replacewithuniqueidforaddtarget”。您从哪里获得这些信息?
    猜你喜欢
    • 1970-01-01
    • 2018-08-22
    • 1970-01-01
    • 2015-03-13
    • 2021-09-02
    • 2021-10-19
    • 2013-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多