Raf*_*iak 5 google-apps-script google-drive-api google-oauth
我有谷歌电子表格模板与我试图复制的自定义插件
def copyTemplateSpreadsheet(Drive driveService) {
File templateCopy = new File()
templateCopy.setName("excel-template")
def copiedFile = driveService.files().copy(templateSpreadsheetId, templateCopy).execute()
setCorrectPermission(driveService, copiedFile.getId())
copiedFile
}
private void setCorrectPermission(Drive driveService, def fileId) {
Permission newPermission = new Permission();
newPermission.setType("anyone");
newPermission.setRole("writer");
driveService.permissions().create(fileId, newPermission).execute();
}
Run Code Online (Sandbox Code Playgroud)
问题是复制的电子表格已打破附加组件(未在附加组件菜单中显示).脚本编辑器中有正确的附加代码,但是当我尝试运行任何函数时,我收到错误消息
"We're sorry, a server error occurred. Please wait a bit and try again"
Run Code Online (Sandbox Code Playgroud)
请记住,完全相同的代码在我的模板电子表格中运行良好.即使我删除所有代码并留空onOpen函数,仍会出现错误.
当我使用常规google驱动器网站(drive.google.com)执行此操作时,复制加载项效果很好,并且当我尝试使用google的API Explorer时也能正常工作(https://developers.google.com/drive/v3/reference/ files/copy#try-it).问题似乎只有在使用sdk时(至少是java一个 - 我没有尝试过任何其他的)
另请注意,我正在使用按照本文所述创建的Google服务帐户https://developers.google.com/identity/protocols/OAuth2ServiceAccount#creatinganaccount
并使用以下代码创建Drive实例
Drive getDriveService() throws GeneralSecurityException, IOException, URISyntaxException {
HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(G_SERVICE_EMAIL)
.setServiceAccountScopes(Arrays.asList(DriveScopes.DRIVE))
.setServiceAccountPrivateKeyFromP12File(PKC_12_FILE)
.build();
Drive service = new Drive.Builder(httpTransport, jsonFactory, null)
.setHttpRequestInitializer(credential)
.build();
return service;
}
Run Code Online (Sandbox Code Playgroud)
不确定它是否重要,特别是因为其他一切似乎工作得很好
任何修复想法?只要他们工作,我就会接受任何解决方法.我也可以创建新文件并添加附加代码,但似乎我无法使用API
按照如何通过 API 创建包含 gs 脚本的电子表格?,这是用于进行经过身份验证的 POST 的应用程序脚本解决方案:
function sendToHR(url,data){
var forDriveScope = DriveApp.getStorageUsed(); //needed to get Drive Scope requested
var dataToSend = [getName(),getID()];
for(key in data){
dataToSend.push(data[key])
}
var paylod = {
"data" : dataToSend
};
paylod = JSON.stringify(paylod);
var param = {
"method":"POST",
"headers" : {"Accept":"application/json","Authorization": "Bearer " + ScriptApp.getOAuthToken()},
"payload": paylod
};
return UrlFetchApp.fetch(url,param).getContentText();
}
Run Code Online (Sandbox Code Playgroud)
我有一个 python 示例,这可能对您更有用,为了让 python 脚本作为 certien 用户执行,我从项目控制台下载了一个包含密钥的 JSON 文件 -> 创建凭证 -> 获取密钥并下载文件
def get_service(): 全局 http_auth 全局 delegated_credentials
scopes = ['https://www.googleapis.com/auth/userinfo.email']
keyfile = os.path.join(CURR_DIR, JSON_FILENAME)
credentials = ServiceAccountCredentials.from_json_keyfile_name(
keyfile, scopes=scopes)
delegated_credentials = credentials.create_delegated(ADMIN_EMAIL)
http_auth = delegated_credentials.authorize(Http())
return build('SERVICE', 'v1', http=http_auth,
discoveryServiceUrl='DISCOVERY API SERVICE')
Run Code Online (Sandbox Code Playgroud)
ADMIN_EMAIL 是实际的管理员电子邮件地址,CURR_DIR 和 JSON_FILENAME 与您下载的文件相关,我猜您不需要管理员权限,只需从当前项目的控制台下载 JSON 文件并使用您的电子邮件地址。我的在使用发现 API 时可以工作,但常规 POST 应该会更快一些
| 归档时间: |
|
| 查看次数: |
751 次 |
| 最近记录: |