Search code examples
c#.netgoogle-classroom

Official Google Classroom API example returns error on request C#


Good time everyone I stucked with the problem with Google Classroom API and C#. I want to be able to create course from discord bot but since I had problems I decided to try with a simple example from the source

There are some additional libraries connected but this is just a scratch. The problem is that this code that I pasted from the official google api website is returning error. I've searched the net searching the solution and found some cases. Someone asked to turn on API in Google Cloud Console and I turned on Google Classroom API on my project. But it didn't help. But I can read information from the google account's classrooms, like courses' names and etc.

This is a new (and first) experience with Google API and I can don't know some basic things for example? What should I do?

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);

        }
    }
}

Error

Google.GoogleApiException: "Google.Apis.Requests.RequestError
Request had insufficient authentication scopes. [403]
Errors [
    Message[Insufficient Permission] Location[ - ] Reason[insufficientPermissions] Domain[global]

Solution

  • Issue:

    ClassroomCoursesReadonly cannot be used to create courses, just to retrieve them.

    Solution:

    So I found the solution:

    1. Change the Scopes from Readonly to just ClassroomCourses
    2. Recreate credentials
    3. Upload the new file in project folder

    Reference: