gpt4 book ai didi

google-apps-script - "The caller does not have permission - google-apps-script"

转载 作者:行者123 更新时间:2023-12-02 00:14:05 24 4
gpt4 key购买 nike

我正在尝试使用 google-apps-script 创建谷歌教室的扩展,但我似乎遇到了权限问题 - “调用者没有权限”。有人可以告诉我为什么会发生这种情况以及我需要做什么

我尝试访问谷歌云平台提供的所有库,希望其中一个提供所需的权限,但没有成功。

function listCourses() {
var optionalArgs = {
pageSize: 10
};
var response = Classroom.Courses.list(optionalArgs);
var courses = response.courses;
if (courses && courses.length > 0) {
for (i = 0; i < courses.length; i++) {
var course = courses[i];
Logger.log('%s (%s)', course.name, course.id);
var submissions = Classroom.Courses.CourseWork.list(course.id, optionalArgs);
for (i = 0; i < submissions.length; i++) {
var submission = submissions[i];
Logger.log('%s', submission);
}
}
} else {
Logger.log('No courses found.');
}
}

该代码旨在列出特定类(class)中类(class)的类(class) ID

最佳答案

此解决方案涉及做两件事 -

  1. 更新 list 文件 (appsscript.json)
  2. 更新 Code.gs 文件

任务 1

更新 list 文件 (appsscript.json)

适合此任务的 Classroom API 引用是 here .

看起来即使在启用高级 Google 服务...后,您也只能添加以下 OAuth 范围 -

您可以通过导航到文件 > 项目属性 > 范围来查看这些内容。

但是,当您从文档链接尝试使用 API 时,在凭据 > Google OAuth 2.0 选项卡下,它会显示另外 4 个完全不同的 OAuth 范围;这些如下 -

您需要在 Apps 脚本 list 文件中手动添加所有 8 个内容。为此,请导航至查看并选中显示 list 文件。您需要在此处添加此代码,可能位于依赖项下方 -

"oauthScopes": [
"https://www.googleapis.com/auth/classroom.courses",
"https://www.googleapis.com/auth/classroom.coursework.me.readonly",
"https://www.googleapis.com/auth/classroom.profile.emails",
"https://www.googleapis.com/auth/classroom.profile.photos",
"https://www.googleapis.com/auth/classroom.rosters",

"https://www.googleapis.com/auth/classroom.coursework.me",
"https://www.googleapis.com/auth/classroom.coursework.me.readonly",
"https://www.googleapis.com/auth/classroom.coursework.students",
"https://www.googleapis.com/auth/classroom.coursework.students.readonly"
],

Note1: Only adding the newer 4 will not do the trick as the script would assume only these and not the original 5 that were auto-populated when your script ran for the first time.

Note2: The blank line is simply to differentiate between the scopes that get generated automatically vs. the ones you need to add manually (its redundant).

我的 appsscript.json 文件如下所示;你的可能会有所不同 -

{
"timeZone": "Asia/Kolkata",
"dependencies": {
"enabledAdvancedServices": [{
"userSymbol": "Classroom",
"serviceId": "classroom",
"version": "v1"
}]
},
"oauthScopes": [
"https://www.googleapis.com/auth/classroom.courses",
"https://www.googleapis.com/auth/classroom.coursework.me.readonly",
"https://www.googleapis.com/auth/classroom.profile.emails",
"https://www.googleapis.com/auth/classroom.profile.photos",
"https://www.googleapis.com/auth/classroom.rosters",

"https://www.googleapis.com/auth/classroom.coursework.me",
"https://www.googleapis.com/auth/classroom.coursework.me.readonly",
"https://www.googleapis.com/auth/classroom.coursework.students",
"https://www.googleapis.com/auth/classroom.coursework.students.readonly"
],
"exceptionLogging": "STACKDRIVER"
}

任务 2

更新 Code.gs 文件

一旦您拥有正确的权限,您就可以自由地使用实际代码 - 您最初共享的代码缺少一些组件,我已经对其进行了修改以使其在此处正常工作 -

function listCourses() {
var optionalArgs = {
pageSize: 10
};
var response = Classroom.Courses.list(optionalArgs);
var courses = response.courses;
if (courses && courses.length > 0) {
for (var i = 0; i < courses.length; i++) {
var course = courses[i];
Logger.log('%s (%s)', course.name, course.id);
var submissions = Classroom.Courses.CourseWork.list(course.id, optionalArgs);
for (var j = 0; j < submissions.courseWork.length; j++) {
var submission = submissions.courseWork[j];
Logger.log('%s', submission);
}
}
} else {
Logger.log('No courses found.');
}
}

希望这有帮助:)但我实际上不知道为什么这些范围没有自动添加,也许会向 Google 提出一个问题。

编辑注释:纠正语法错误

关于google-apps-script - "The caller does not have permission - google-apps-script",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58364530/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com