Google+ CreateActivity为某些帐户返回“401 Unauthorized”

时间:2014-01-13 18:10:49

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

我有一个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);
        });
},

1 个答案:

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