Skip to content

Commit 3220501

Browse files
committed
Added a section of Request Web Apps using Fetch API of Javascript with access token
1 parent bc2f5d5 commit 3220501

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
- [Status code from Web Apps](#statuscodefromwebapps)
3333
- [Concurrent access to Web Apps](#concurrentaccesstowebapps)
3434
- [Implementing Pseudo 2FA for Web Apps](#2fatowebapps) <sup><font color="Red">Added at October 26, 2022</font></sup>
35+
- [Request Web Apps using Fetch API of Javascript with access token](#fetchapiwithaccesstoken) <sup><font color="Red">Added at December 20, 2022</font></sup>
3536
- [Workarounds](#workarounds)
3637
- [1. Reflecting Latest Script to Deployed Web Apps without Redeploying](#workaround1) <sup><font color="Red">Added at December 7, 2022</font></sup>
3738
- [Applications](#applications)
@@ -1125,6 +1126,38 @@ In Google Apps Script, there is the Web Apps. When Web Apps is used, the users c
11251126
11261127
**You can see the detail of this report at [https://gist.github.com/tanaikech/7a15164b1227e2ec2231fce24ae9daf2](https://gist.github.com/tanaikech/7a15164b1227e2ec2231fce24ae9daf2).**
11271128
1129+
<a name="fetchapiwithaccesstoken"></a>
1130+
1131+
## Request Web Apps using Fetch API of Javascript with the access token
1132+
1133+
In the current stage, when you want to request Web Apps with the access token, when the access token is including the request header, an error related to CORS occurs. Please be careful about this.
1134+
1135+
So, in this case, please include the access token in the query parameter. By this, the request can be worked. The sample script is as follows.
1136+
1137+
```javascript
1138+
const accessToken = "###"; // Please set your access token.
1139+
const url = "https://script.google.com/macros/s/###/exec?access_token=" + accessToken;
1140+
fetch(url)
1141+
.then((res) => res.text())
1142+
.then((res) => console.log(res));
1143+
```
1144+
1145+
By this modification, you can access Web Apps with the access token.
1146+
1147+
By the way, for example, when the curl command is used, the access token can be included in the request header as follows.
1148+
1149+
```bash
1150+
$ curl -L \
1151+
-H "Authorization: Bearer ###" \
1152+
"https://script.google.com/macros/s/###/exec"
1153+
```
1154+
1155+
And also, the following curl command can be used.
1156+
1157+
```bash
1158+
$ curl -L "https://script.google.com/macros/s/###/exec?access_token=###"
1159+
```
1160+
11281161
<a name="workarounds"></a>
11291162
11301163
## Workarounds

0 commit comments

Comments
 (0)