Пример #1
0
        private void Write()
        {
            string[] Scopes = { DocsService.Scope.Documents, DocsService.Scope.DriveFile, DocsService.Scope.Drive };

            UserCredential credential;

            using (var stream = new FileStream($"/Users/mathiasgammelgaard/credentials/credentials.json", FileMode.Open, FileAccess.Read)) {
                string credPath = "/Users/mathiasgammelgaard/credentials/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);
            }

            DocsService service = new DocsService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential
            });

            // Define request parameters.
            String documentId = "1tldY_DpX5IM9wPqIKN9FVgwfBT2Ueq__F8pwOI8dLVI";

            DocumentsResource.GetRequest documentRequest = service.Documents.Get(documentId);
            Document doc = documentRequest.Execute();

            List <Request> requests = new List <Request>();

            DeleteAllContent(requests, doc, service, documentId);

            foreach (Course course in courses)
            {
                //AddHeaderLine(course.title, requests, 1);
                foreach (Section section in course.sections)
                {
                    if (section.description != null)
                    {
                        AddHeaderLine(section.name, requests, 3);
                        AddParagraph(section.description.levels[0].description, requests);
                    }
                    else
                    {
                        AddHeaderLine(section.name, requests, 3, false);
                        AddQuizQuestions(section.quiz.levels[0], requests);
                    }
                }
            }



            BatchUpdateDocumentRequest body = new BatchUpdateDocumentRequest();

            body.Requests = requests;
            DocumentsResource.BatchUpdateRequest updateRequest = service.Documents.BatchUpdate(body, documentId);

            updateRequest.Execute();
        }
Пример #2
0
        public ActionResult UpdateAssignment(IFormCollection collection,
                                             [FromServices] IGoogleAuthProvider auth)
        {
            string courseId     = collection["CourseId"];
            string courseWorkId = collection["CourseWorkId"];
            string answer       = collection["Answer"];

            Document document = new Document()
            {
                Title = "My Assignment " + courseWorkId
            };

            var docService = this.GetGoogleDocsService(auth);

            var docReq = docService.Documents.Create(document);
            var docRes = docReq.Execute();

            string documentId = docRes.DocumentId;
            string docUrl     = $"https://docs.google.com/document/d/{documentId}/edit";

            DocumentsResource.BatchUpdateRequest batchUpdate =
                docService
                .Documents
                .BatchUpdate(GenerateGoogleDocText(answer), documentId);

            var batchUpdateResponse = batchUpdate.Execute();

            var submission = this.GetGoogleClassRoomService(auth)
                             .Courses
                             .CourseWork
                             .StudentSubmissions
                             .List(courseId, courseWorkId).Execute();

            string submissionId = submission.StudentSubmissions[0].Id;


            List <Attachment> attachments = new List <Attachment>()
            {
                new Attachment()
                {
                    Link = new Google.Apis.Classroom.v1.Data.Link()
                    {
                        Url = docUrl
                    }
                }
            };

            ModifyAttachmentsRequest body = new ModifyAttachmentsRequest()
            {
                AddAttachments = attachments
            };

            var req = this.GetGoogleClassRoomService(auth)
                      .Courses
                      .CourseWork
                      .StudentSubmissions
                      .ModifyAttachments(body, courseId, courseWorkId, submissionId).Execute();

            return(Redirect(req.AlternateLink));
        }
Пример #3
0
        private void DeleteAllContent(List <Request> requests, Document doc, DocsService service, string documentId)
        {
            DeleteContentRangeRequest deleteContentRangeRequest = new DeleteContentRangeRequest();

            Google.Apis.Docs.v1.Data.Range range = new Google.Apis.Docs.v1.Data.Range();
            range.StartIndex = 1;
            range.EndIndex   = 1000000000;

            deleteContentRangeRequest.Range = range;

            Request request = new Request();

            request.DeleteContentRange = deleteContentRangeRequest;

            try {
                BatchUpdateDocumentRequest tryBody = new BatchUpdateDocumentRequest();

                tryBody.Requests = new List <Request> {
                    request
                };
                DocumentsResource.BatchUpdateRequest tryUpdateRequest = service.Documents.BatchUpdate(tryBody, documentId);

                BatchUpdateDocumentResponse tryResponse = tryUpdateRequest.Execute();
            } catch (Exception e) {
                string[] responseData = e.Message.Split("must be less than the end index of the referenced segment, ");
                responseData = responseData[1].Split(".");

                range.EndIndex = Int32.Parse(responseData[0]) - 1;

                if (range.EndIndex - range.StartIndex != 0)
                {
                    deleteContentRangeRequest.Range = range;

                    request = new Request();
                    request.DeleteContentRange = deleteContentRangeRequest;
                    requests.Add(request);
                }
            }
        }