0

I have a function that calls an apps script that has been deployed as an executable script.

I setup oAuth with the correct necessary scopes in a react app and then I call my API request to get data from the app script.

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

The error I get is 401 UNAUTHENTICATED.

{
  "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
}

I'm logging my access token out before I call the api so I know I am logged in and have an access token.

How do I explicitly attach the access token to the window.gapi.client.script.scripts.run() request?

TheMaster
  • 45,448
  • 6
  • 62
  • 85
Josh Pittman
  • 7,024
  • 7
  • 38
  • 66
  • Have you tried to use `UrlFetchApp` method? Please check this [SO post](https://stackoverflow.com/questions/49069853/check-if-user-has-run-it) for more details. – Jessica Rodriguez Apr 04 '19 at 17:08
  • UrlFetchApp is for getting data from an API inside an apps script. I want to get data from the app, which I have published as an API. The link you shared is in the context of publishing a script as a web app, not an executable API. Thanks though. – Josh Pittman Apr 05 '19 at 03:01
  • No, I take that back. I didn't go through Edit 3.7.18 on the accepted answer. This solved my problem. You are an absolute lifesaver. Thank you so much. – Josh Pittman Apr 05 '19 at 08:31
  • You're welcome. I'm glad it works well. – Jessica Rodriguez Apr 05 '19 at 08:58
  • 1
    Possible duplicate of [Check if user has run it](https://stackoverflow.com/questions/49069853/check-if-user-has-run-it) – tehhowch Apr 05 '19 at 21:13
  • 1
    @tehhowch Not a duplicate, these are different problems with the same solution. Also, 'Check if user has run it' is a terribly unhelpful title for people searching for a solution. I've been wracking my brain for days now and I never came across this answer till it was shared with me explicitly. Please remove duplication. – Josh Pittman Apr 06 '19 at 02:37
  • @Josh If this needs to be a separate question, it needs a good answer. Please add a answer in your own words explaining what you did wrong and what you did to solve the issue. You may link to that answer as a reference. – TheMaster Apr 07 '19 at 21:05

0 Answers0