英文:
App Script: write a CSV file to a Google Cloud Storage Bucket
问题
我已经编写了一个从项目A的Big Query表中提取数据的App脚本,然后应该将这些数据作为CSV写入项目B的Google Cloud Storage存储桶。前者正常工作,但后者失败了。我在项目B中具有Cloud Storage对象管理员权限,但仍然在运行脚本时收到以下错误:
"error": {
"code": 403,
"message": "权限不足",
"errors": [
{
"message": "权限不足",
"domain": "global",
"reason": "insufficientPermissions"
}
]
}
我的脚本目前如下所示:
function pushCSV2Bucket(sheet_data){
var bucketName = "some_name";
var fileName = "wooqer_employee_details.csv";
var csv_payload = sheet_data.map(row=>row.join(',')).join('\n');
var url = "https://www.googleapis.com/upload/storage/v1/b/" + bucketName + "/o?uploadType=media&name=" + fileName;
var options = {
method: "post",
contentType: "text/csv",
payload: csv_payload,
headers: {
Authorization: "Bearer " + ScriptApp.getOAuthToken(),
"Content-Type": "text/csv"
}
};
try {
var response = UrlFetchApp.fetch(url, options); // 如果失败,会引发错误
Logger.log('成功将CSV推送到GCS存储桶:'+response.getContentText());
return true;
} catch (error) {
Logger.log("将CSV推送到GCS存储桶失败:" + error);
return false;
}
}
也许您知道哪里有问题?我不认为问题出在脚本上。也许在使用App脚本时我不能同时访问这两个项目。
英文:
I have written an App Script that pulls data from a Big Query table in project A and is then supposed to write this data as a CSV to a Google Cloud Storage Bucket in project B. The former works but the latter fails. I have Cloud Storage Object Admin rights in project B but still get the following error when I run my script:
"error": {
"code": 403,
"message": "Insufficient Permission",
"errors": [
{
"message": "Insufficient Permission",
"domain": "global",
"reason": "insufficientPermissions"
}
]
}
My script currently looks like this:
function pushCSV2Bucket(sheet_data){
var bucketName = "some_name";
var fileName = "wooqer_employee_details.csv";
var csv_payload = sheet_data.map(row=>row.join(',')).join('\n');
var url = "https://www.googleapis.com/upload/storage/v1/b/" + bucketName + "/o?uploadType=media&name=" + fileName;
var options = {
method: "post",
contentType: "text/csv",
payload: csv_payload,
headers: {
Authorization: "Bearer " + ScriptApp.getOAuthToken(),
"Content-Type": "text/csv"
}
};
try {
var response = UrlFetchApp.fetch(url, options); // This will raise error if failed
Logger.log('Successfully pushed CSV to GCS bucket : '+response.getContentText());
return true;
} catch (error) {
Logger.log("Failed to push CSV to GCS bucket: " + error);
return false;
}
}
Do you perhaps know what is incorrect? I do not think the issue lies with the script. Perhaps, I cannot access to projects at a time when using App Script.
答案1
得分: 1
以下是翻译好的部分:
回答这个问题作为社区维基。由于您通过将必要的范围添加到 appsscript.json
文件中解决了问题。
在使用Google API在Apps Script中时,要授予对特定服务和资源的访问权限,必须在脚本清单(appsscript.json)中明确声明相关范围。
如您所分享的文档中所述 设置明确的范围
Apps Script 通过扫描其代码中需要这些范围的函数调用来自动确定脚本需要什么范围。对于大多数脚本来说,这已经足够并节省了您的时间,但对于已发布的附加组件和Web应用程序,您必须更直接地控制范围。
英文:
Answering this as community wiki .As you were able to resolve the issue by adding the necessary scopes to the appsscript.json
file.
To grant access to particular services and resources when using Google APIs in Apps Script, you must explicitly declare the relevant scopes in the script manifest (appsscript.json).
As mentioned in the document you shared Setting explicit scopes
>Apps Script automatically determines what scopes a script needs by scanning its code for function calls that require them. For most scripts this is sufficient and saves you time, but for published add-ons and web apps you must exercise more direct control of the scopes.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论