'UrlFetchApp.Fetch returns status 200 but Google script web app does not work

I have made two Google spreadsheets. One is parent sheet and another is a child sheet that imports data from parent sheet. This data is imported using a standalone Google app script which is published as web app to run as "Me", the only person who has access to parent sheet. (This is project requirement to not share the parent sheet with other users. Also, I have child sheet protected except one column which other user can edit. And if I do not run the script as me, it will run as that user who then gets " cannot edit protected cells" error as the script runs as him otherwise)

Now the problem is that when I run this Web app by visiting its url (exec) with Chrome, it runs fine and gets data on child sheet. But when I do the same using UrlFetchApp.Fetch in my child sheet bound script, it gives responsecode 200 but doesnt import any data!

Why? What am I missing? Please help.



Solution 1:[1]

I found the solution myself fortunately. The missing link is the OAuth bearer header to be passed along with fetch request. Also, I was earlier using doGet in my web app, but now using doPost instead and it works fine!

So, the summary is to pass OAuth as bearer in your header and dont forget to publish your web app again first for changes (unless you are using dev mode for development).

PS - Check the marked duplicate link I have posted for my question. It has the code for you.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 J J