0

I am using GET https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId} to get some data from my Google Sheets by using fetch API + Sheet v4 API

let fetch_url = "https://sheets.googleapis.com/v4/spreadsheets/1UOaYEl8i33Bco5PaIXAljWkPFmt3j3i1tohXwNl3vvs?key=" + apiKey
    let fetch_option = {
        method: 'GET',
        headers: {
            Authorization: `Bearer ${accessToken}`,
            'Content-Type': 'application/json'
        }
    }
    fetch(fetch_url,fetch_option)
    .then(res => function(res){
        console.log(res.JSON())
    })

But I have received 401 when I am running this fetch function. May I know what am I missing?

"permissions":[
    "activeTab",
    "identity",
    "http://*/"
],
"background":{
    "scripts":["src/script/background.js","src/script/jsrsasign-all-min.js"],
    "persistent": false
},
"oauth2": {
    "client_id": "Client ID",
    "scopes":["https://www.googleapis.com/auth/spreadsheets",
        "https://sheets.googleapis.com/v4/spreadsheets/*"]
}
new guy
  • 21
  • 3
  • I do this in my background.js, and I think I have declared the permission – new guy Sep 19 '21 at 10:17
  • @wOxxOm I have updated this post, you can take a look – new guy Sep 19 '21 at 10:19
  • Replace `http` with `*` in permissions. – wOxxOm Sep 19 '21 at 10:23
  • @wOxxOm still the same, will you think I am missing out something? – new guy Sep 19 '21 at 10:29
  • I am not familiar with the sheets API, but recently having used the reCaptcha API I realised they weren't taking json requests, and I also see you assign the `Content-Type` header to `application/json` while not sending anything on the request body. Perhaps try removing this header? – Menma Sep 19 '21 at 10:33
  • If you reloaded the extension after editing manifest I have no more ideas. – wOxxOm Sep 19 '21 at 10:46
  • @Menma not works, @wOxxOm yep I have reloaded before I test. May I know authentication credentials means "access token" or the "id-token" that inside the redirected_url? Because I am using `chrome.identity.launchWebAuthFlow` to do the login and get the token from the redirected url. – new guy Sep 19 '21 at 10:57
  • From the documentation, 401 error means `The HTTP 401 Unauthorized client error status response code indicates that the request has not been applied because it lacks valid authentication credentials for the target resource.` Maybe I am lacking the authentication credentials? – new guy Sep 19 '21 at 11:01
  • try using "ContentType" as header. Perhaps you may be missing the `https://www.googleapis.com/auth/script.external_request` scope which is required for `UrlFetchApp` See https://stackoverflow.com/a/46938768/4243927 – Aerials Sep 22 '21 at 07:24

0 Answers0