代码之家  ›  专栏  ›  技术社区  ›  Viatorus

谷歌API批量返回401

  •  -1
  • Viatorus  · 技术社区  · 7 年前

    一次在一批中处理两个或多个请求会导致每个请求的401。

    const batch = gapi.client.newBatch();
    batch.add(gapi.client.drive.files.list());
    batch.add(gapi.client.drive.files.list());
    batch.then((e) => {
      console.log(e);
    });
    

    错误是:

    {
     "error": {
      "errors": [
       {
        "domain": "global",
        "reason": "authError",
        "message": "Invalid Credentials",
        "locationType": "header",
        "location": "Authorization"
       }
      ],
      "code": 401,
      "message": "Invalid Credentials"
     }
    }
    

    只用一个 一批添加 很好用。这对我来说毫无意义。

    为什么?我做错了什么?

    1 回复  |  直到 7 年前
        1
  •  -1
  •   Linda Lawton - DaImTo    7 年前

    直接从文档中获取 handle errors

    401:无效凭据 无效的授权标头。您正在使用的访问令牌已过期或无效。

    {
      "error": {
        "errors": [
          {
            "domain": "global",
            "reason": "authError",
            "message": "Invalid Credentials",
            "locationType": "header",
            "location": "Authorization",
          }
        ],
        "code": 401,
        "message": "Invalid Credentials"
      }
    }
    

    建议的操作:使用长寿命刷新令牌刷新访问令牌。如果失败,请引导用户通过OAuth流,如中所述 Authorizing Your App with Google Drive .

    Batch 文档

    var searchRequest = function(name) {
      return gapi.client.request({
        'path': 'plus/v1/people',
        'params': {'query': name}
       });
    };
    var searchAlvin = searchRequest('Alvin');
    var searchSimon = searchRequest('Simon');
    
    // Adding just the request
    batch.add(searchAlvin);
    // Adding the request with an ID
    batch.add(searchSimon, {'id': 'searchSimon'});