Как передать токен доступа в API-интерфейс оставшегося скрипта приложений Google из браузера? - PullRequest
0 голосов
/ 04 апреля 2019

У меня есть функция, которая вызывает скрипт приложения, который был развернут как исполняемый скрипт.

Я устанавливаю oAuth с правильными необходимыми областями в приложении реагирования, а затем вызываю запрос API для получения данных из сценария приложения.

const connectToDrive = async () => {

    const authToken = await window.gapi.auth2
      .getAuthInstance()
      .currentUser.get()
      .getAuthResponse().id_token;

    console.log('token', authToken);

    window.gapi.client.script.scripts
      .run({
        scriptId,
        resource: {
          function: 'setUpOrReadRoot',
        },
      })
      .then(resp => console.log('result', resp.result))
      .catch(err => console.log('err', err));
  };

Я получаю сообщение об ошибке: 401 НЕАУТЕНТИФИЦИРОВАНО.

{
  "result": {
    "error": {
      "code": 401,
      "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
      "status": "UNAUTHENTICATED"
    }
  },
  "body": "{\n  \"error\": {\n    \"code\": 401,\n    \"message\": \"Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.\",\n    \"status\": \"UNAUTHENTICATED\"\n  }\n}\n",
  "headers": {
    "date": "Thu, 04 Apr 2019 06:50:59 GMT",
    "content-encoding": "gzip",
    "server": "ESF",
    "content-type": "application/json; charset=UTF-8",
    "vary": "Origin, X-Origin, Referer",
    "cache-control": "private",
    "content-length": "228"
  },
  "status": 401,
  "statusText": null
}

Я записываю свой токен доступа перед тем, как вызвать API, поэтому я знаю, что я вошел в систему и имею токен доступа.

Как явно прикрепить токен доступа к запросу window.gapi.client.script.scripts.run()?

...