官方 Google 课堂 API 示例 returns 请求 C# 时出错
Official Google Classroom API example returns error on request C#
大家好我坚持使用 Google 课堂 API 和 C# 的问题。我希望能够从 discord bot 创建课程,但由于遇到问题,我决定尝试使用 source
中的一个简单示例
连接了一些额外的库,但这只是一个开始。问题是我从 google api 官方网站粘贴的这段代码返回错误。我在网上搜索了解决方案,发现了一些案例。有人要求在 Google Cloud Console 中打开 API,而我在我的项目中打开了 Google 课堂 API。但这没有帮助。但我可以从 google 帐户的教室中读取信息,例如课程名称等。
这是 Google API 的全新(也是第一次)体验,例如,我可能不知道一些基本知识?我该怎么办?
using Google.Apis.Auth.OAuth2;
using Google.Apis.Classroom.v1;
using Google.Apis.Classroom.v1.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ClassroomQuickstart
{
class Program
{
static string[] Scopes = { ClassroomService.Scope.ClassroomCoursesReadonly };
static string ApplicationName = "Classroom API .NET Quickstart";
static void Main(string[] args)
{
UserCredential credential;
using (var stream =
new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
{
// The file token.json stores the user's access and refresh tokens, and is created
// automatically when the authorization flow completes for the first time.
string credPath = "token.json";
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
// Create Classroom API service.
var service = new ClassroomService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
Console.WriteLine("Service");
// Define request parameters.
CoursesResource.ListRequest request = service.Courses.List();
request.PageSize = 10;
var course = new Course
{
Name = "10th Grade Biology",
Section = "Period 2",
DescriptionHeading = "Welcome to 10th Grade Biology",
Description = "We'll be learning about about the structure of living creatures "
+ "from a combination of textbooks, guest lectures, and lab work. Expect "
+ "to be excited!",
Room = "301",
OwnerId = "me",
CourseState = "PROVISIONED"
};
Console.WriteLine("Course");
course = service.Courses.Create(course).Execute();
Console.WriteLine("Execute");
Console.WriteLine("Course created: {0} ({1})", course.Name, course.Id);
}
}
}
错误
Google.GoogleApiException: "Google.Apis.Requests.RequestError
Request had insufficient authentication scopes. [403]
Errors [
Message[Insufficient Permission] Location[ - ] Reason[insufficientPermissions] Domain[global]
问题:
ClassroomCoursesReadonly
不能用于创建课程,只能用于检索它们。
解决方案:
所以我找到了解决方案:
- 将范围从只读更改为仅课堂课程
- 重新创建凭据
- 上传项目文件夹中的新文件
参考:
大家好我坚持使用 Google 课堂 API 和 C# 的问题。我希望能够从 discord bot 创建课程,但由于遇到问题,我决定尝试使用 source
中的一个简单示例连接了一些额外的库,但这只是一个开始。问题是我从 google api 官方网站粘贴的这段代码返回错误。我在网上搜索了解决方案,发现了一些案例。有人要求在 Google Cloud Console 中打开 API,而我在我的项目中打开了 Google 课堂 API。但这没有帮助。但我可以从 google 帐户的教室中读取信息,例如课程名称等。
这是 Google API 的全新(也是第一次)体验,例如,我可能不知道一些基本知识?我该怎么办?
using Google.Apis.Auth.OAuth2;
using Google.Apis.Classroom.v1;
using Google.Apis.Classroom.v1.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ClassroomQuickstart
{
class Program
{
static string[] Scopes = { ClassroomService.Scope.ClassroomCoursesReadonly };
static string ApplicationName = "Classroom API .NET Quickstart";
static void Main(string[] args)
{
UserCredential credential;
using (var stream =
new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
{
// The file token.json stores the user's access and refresh tokens, and is created
// automatically when the authorization flow completes for the first time.
string credPath = "token.json";
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
// Create Classroom API service.
var service = new ClassroomService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
Console.WriteLine("Service");
// Define request parameters.
CoursesResource.ListRequest request = service.Courses.List();
request.PageSize = 10;
var course = new Course
{
Name = "10th Grade Biology",
Section = "Period 2",
DescriptionHeading = "Welcome to 10th Grade Biology",
Description = "We'll be learning about about the structure of living creatures "
+ "from a combination of textbooks, guest lectures, and lab work. Expect "
+ "to be excited!",
Room = "301",
OwnerId = "me",
CourseState = "PROVISIONED"
};
Console.WriteLine("Course");
course = service.Courses.Create(course).Execute();
Console.WriteLine("Execute");
Console.WriteLine("Course created: {0} ({1})", course.Name, course.Id);
}
}
}
错误
Google.GoogleApiException: "Google.Apis.Requests.RequestError
Request had insufficient authentication scopes. [403]
Errors [
Message[Insufficient Permission] Location[ - ] Reason[insufficientPermissions] Domain[global]
问题:
ClassroomCoursesReadonly
不能用于创建课程,只能用于检索它们。
解决方案:
所以我找到了解决方案:
- 将范围从只读更改为仅课堂课程
- 重新创建凭据
- 上传项目文件夹中的新文件