|
32 | 32 | - [Status code from Web Apps](#statuscodefromwebapps) |
33 | 33 | - [Concurrent access to Web Apps](#concurrentaccesstowebapps) |
34 | 34 | - [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> |
35 | 36 | - [Workarounds](#workarounds) |
36 | 37 | - [1. Reflecting Latest Script to Deployed Web Apps without Redeploying](#workaround1) <sup><font color="Red">Added at December 7, 2022</font></sup> |
37 | 38 | - [Applications](#applications) |
@@ -1125,6 +1126,38 @@ In Google Apps Script, there is the Web Apps. When Web Apps is used, the users c |
1125 | 1126 |
|
1126 | 1127 | **You can see the detail of this report at [https://gist.github.com/tanaikech/7a15164b1227e2ec2231fce24ae9daf2](https://gist.github.com/tanaikech/7a15164b1227e2ec2231fce24ae9daf2).** |
1127 | 1128 |
|
| 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 | +
|
1128 | 1161 | <a name="workarounds"></a> |
1129 | 1162 |
|
1130 | 1163 | ## Workarounds |
|
0 commit comments