Google Classroom - Programmatically create assignment - google-apps-script

I'm using google apps script to create assignment with an uploaded document for a classroom. However, there's an error.
Execution failed: Invalid JSON payload received. Unknown name
"share_mode" at 'course_work.materials[0]': Cannot find field. Invalid
JSON payload received. Unknown name "id" at
'course_work.materials[0].drive_file': Cannot find field. Invalid JSON
payload received. Unknown name "title" at
'course_work.materials[0].drive_file': Cannot find field. (line 2,
file "TEST") [0.061 seconds total runtime]
Here's my code. I know the error is in materials but I'm not sure what I did wrongly.
function myFunction() {
var exec = Classroom.Courses.CourseWork.create({
title: "Test File",
state: "DRAFT",
materials: [
{
driveFile: {id: "1ENk55RMtApIydyPFe0uyuhmu6nSV4", title: "Test File"},
shareMode: "STUDENT_COPY"
}
],
workType: "ASSIGNMENT"
}, "3896298178");
Logger.log(exec);
}

Found out the root of your issue. I've updated your codes to make it work.
Request:
function myFunction() {
var ClassSource = {
title: "Test File",
state: "DRAFT",
materials: [
{
driveFile:{
driveFile: {
id: "fileID",
title: "Sample Document"
},
shareMode: "STUDENT_COPY"
}
}
],
workType: "ASSIGNMENT"
};
Classroom.Courses.CourseWork.create(ClassSource, COURSEID)
//Logger.log(exec);
}
Result:
We receive Invalid JSON payload received. because the formating of the request is wrong. Its a little bit more complicated than I thought, that is why I tried using Try this API to see the request format and it really helped me solve your issue.
Hope this helps.

Per the docs Drivefile property title is marked read only. Just use the id.
https://developers.google.com/classroom/reference/rest/v1/DriveFile

Following ajax request can be sent to create the assignment. The code below was written for Angular but it can be easily converted to jQuery script. You can build your own courseWork object that is being passed as 'data' of ajax request to see the full Object structure visit CourseWork API
$http({
url: 'https://classroom.googleapis.com/v1/courses/'+courseId+'/courseWork?access_token='+$scope.session.access_token,
method: 'POST',
data:{
"title": title,
"description": description,
"state": "PUBLISHED",
"workType": "ASSIGNMENT",
"submissionModificationMode": "MODIFIABLE_UNTIL_TURNED_IN",
"associatedWithDeveloper": true
}
}).then(function(response){
console.log(response);
if(response.status==200){
}
}, function(response){
console.log(response);
});
}

Related

How do you pass parameters to a Deployed Google Apps Script API function?

I created a test function (doPost) in a Google Apps Script API using Google Cloud Platform (GCP). I am now trying to call that function from another script in the same project.
I know I am almost there, because this code works:
var token = ScriptApp.getOAuthToken();
var header = {
"Authorization": "Bearer " + token,
"function": "doPost",
"devMode": true,
};
var options = {
"method": "POST",
"headers": header,
"muteHttpExceptions": true,
"payload": {
"function": "doPost",
"devMode": true
}
};
var url = 'https://script.googleapis.com/v1/scripts/xxxxxxxxxxxxxxxxxxxx:run';
var response = UrlFetchApp.fetch(url, options);
However, when I try to include a parameter in payload above, it no longer works:
"payload": {
"function": "doPost",
"parameters": ['1'],
"devMode": true
}
Following other stackoverflow answers, I've tried using in the header:
"contentType": 'application/json',
And accordingly, for the payload:
"payload": JSON.stringify({
"function": "doPost",
"parameters": ['1'],
"devMode": true
})
Whenever I use "JSON.stringify", even without parameters (just like the situation I got to work), it errors out.
With JSON.stringify (and parameters in the payload), I get a worse error, which seems to say it doesn't like any of the payload:
"error": {
"code": 400,
"message": "Invalid JSON payload received. Unknown name \"{\"function\":\"doPost\",\"parameters\":[1007],\"devMode\":true}\": Cannot bind query parameter. Field '{\"function\":\"doPost\",\"parameters\":[1007],\"devMode\":true}' could not be found in request message.",
"status": "INVALID_ARGUMENT",
"details": [
{
"#type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"description": "Invalid JSON payload received. Unknown name \"{\"function\":\"doPost\",\"parameters\":[1007],\"devMode\":true}\": Cannot bind query parameter. Field '{\"function\":\"doPost\",\"parameters\":[1007],\"devMode\":true}' could not be found in request message."
Without JSON.stringify (and with parameters in the payload), I get the error:
"error": {
"code": 400,
"message": "Invalid JSON payload received. Unknown name \"parameters\": Cannot bind query parameter. 'parameters' is a message type. Parameters can only be bound to primitive types.",
"status": "INVALID_ARGUMENT",
"details": [
{
"#type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"description": "Invalid JSON payload received. Unknown name \"parameters\": Cannot bind query parameter. 'parameters' is a message type. Parameters can only be bound to primitive types."
Finally, it doesn't matter what I do with parameters. I'm pretty sure it should be in the format I put above, but I've also tried:
"parameters": [1]
"parameters": 1
"parameters": "1"
among others.
The doPost script is simple for now:
function doPost(parameters) {
Logger.log('parameters = ' + parameters);
return "Hello";
}
Here is the stackoverflow question that seems to be most like this: Apps Script API returning 404 error for existing project. Error returned as HTML rather than JSON, but doesn't seem to answer my problem.
I've studied the scripts.run page about parameters: https://developers.google.com/apps-script/api/reference/rest/v1/scripts/run#authorization-scopes, along with many other pages, including the URL Fetch Service: https://developers.google.com/apps-script/reference/url-fetch.
This is certainly not my first time using UrlFetchApp in Google Apps Script, but it is when calling my own GAS API.
Any help would be greatly appreciated!
Thanks!
Not long after I posted this, and after continuing to see post after post say that you should use JSON.stringify for the payload and contentType: application/json in the header, in the header I changed:
"contentType": "application/json"
to
"Content-Type": "application/json"
and it works now!

Go dialogflow sdk WebhookResponse doesn't return the correct json

I'm trying to write a webhook in Go for Dialogflow, I'm using the apiv2 of the official SDK
google.golang.org/genproto/googleapis/cloud/dialogflow/v2
But I can't generate a correct response using the official sdk.
What I mean is that following the documentation and the WebhookResponse struct I can't generate the expected json for the response.
This is the piece of code that I'm using:
response = dialogflow.WebhookResponse{
FulfillmentMessages: []*dialogflow.Intent_Message{
{
Message: &dialogflow.Intent_Message_Card_{
Card: &dialogflow.Intent_Message_Card{
Title: "Title",
Subtitle: "Subtitle",
ImageUri: "https://example.com/images/example.png",
Buttons: []*dialogflow.Intent_Message_Card_Button{
{
Text: "Button",
Postback: "https://example.com/path/for/end-user/to/follow",
},
},
},
},
},
},
}
This is the json that it generates:
{
"fulfillment_messages": [
{
"Message": {
"Card": {
"title": "Title",
"subtitle": "Subtitle",
"image_uri": "https://example.com/images/example.png",
"buttons": [
{
"text": "Button",
"postback": "https://example.com/path/for/end-user/to/follow"
}
]
}
}
}
]
}
But this is the json that I should send back (according to the official documentation)
"fulfillmentMessages": [
{
"card": {
"title": "card title",
"subtitle": "card text",
"imageUri": "https://example.com/images/example.png",
"buttons": [
{
"text": "button text",
"postback": "https://example.com/path/for/end-user/to/follow"
}
]
}
}
]
}
So my json doesn't work, because it has the Message that shouldn't be there, and Card with uppercase first letter. I've tried to send the json of the documentation and it works, Dialog Flow responds correctly.
I don't understand how to generate the correct json using the official SDK. Please consider that I'm pretty new using Go Lang. This is my first project.
This is the documentation that I'm using at the moment:
https://pkg.go.dev/google.golang.org/genproto/googleapis/cloud/dialogflow/v2?tab=doc#WebhookResponse
As you can see the FulfillmentMessages is an array of Intent_Message
FulfillmentMessages []*Intent_Message
And the Intent_Message has to contain Message (here the documentation)
Thanks in advance for any help and suggestions.
H2K
UPDATE:
if I use log.Println(response) I can see the correct response inside the log
fulfillment_messages:{card:{title:"Title" subtitle:"Subtitle" image_uri:"https://example.com/images/example.png" buttons:{text:"Button" postback:"https://example.com/path/for/end-user/to/follow"}}}
It is not a JSON but the structure is correct, no Message, no Card...
So the problem is when I return it with Gin and the command:
c.JSON(200, response)
I've found a solution!
I need to use the jsonpb marshaler and return it as string with Gin
Here an example:
m := jsonpb.Marshaler{}
result, _ := m.MarshalToString(response)
c.String(200, result)
I've totally went crazy on it, I hope that it could be helpful for someone else.

"errorCode":"UNSPECIFIED_ERROR","message":"Non-static method requires a target."

I am trying to create an envelope using E-signature post API from AppScript. I am able to Authenticate and call other Get API's like Status of Envelope and List of all envelopes successfully. But for this Post API I am facing below error.
"errorCode":"UNSPECIFIED_ERROR",
"message":"Non-static method requires a target."
here is the object which I am passing
var createEnvelopeObj =
{
"documents": [
{
"documentBase64": "JVBERi0----------DI0NGItMThmMzAtNS41LjEzLVNOQVBTSE9UCnN0YXJ0eHJlZgoxOTY2MDcKJSVFT0YK",
"documentId": "1323457",
"fileExtension": ".pdf",
"name": "sampledoc"
}
],
"emailSubject": "Please sign below Document ref: G654sfd238",
"recipients": {
"signers": [
{
"clientUserId": "xxxx#gmail.com",
"email": "xxxx#dddd.com",
"name": "xxxx",
"recipientId": "124",
"routingOrder": "1"
}
]
},
"status": "sent"
};
Below is the API call with above object
function DocusignPost(createEnvelopeObj){
var options = {
headers: {Authorization: 'Bearer eyJ0eXAiOiJNVCIsImFsZyI6IlJTMjU2Iiwia2lkIjoiNjgxO___xxxxx_VKdnH4FHUtI80s5xtZ9tusnP1DmYw '
},
'method' : 'post',
'contentType': 'application/json',
'payload' : JSON.stringify(createEnvelopeObj),
'muteHttpExceptions': true};
try{
var response = UrlFetchApp.fetch("https://demo.docusign.net/restapi/v2.1/accounts/xxxxxxx/envelopes", options );
var postresponce = JSON.parse(response);
Logger.log("postresponce : " + JSON.stringify(postresponce));
return postresponce;}catch(exp){
Logger.log(exp); }}
I have referred both these 57258880, 35047127 but I am using latest Auth method and passing object as payload, but still facing an issue.
Request someone to look into this and shed some light on it. I got stuck here for a couple of days.
I don't see any error in your JSON. So the next step I recommend is to obtain the API trace from DocuSign to see exactly what is being received from DocuSign.
See https://support.docusign.com/en/guides/ndse-user-guide-api-request-logging
You should also carefully check how you're reading in the PDF file before base64 encoding it. PDF files are (almost always) binary, so you need to read them using a binary-clean method.

Attaching a Drive file to an assignment with Google Classroom API

Using Google Apps Script I have managed to create an assignment with the following code:
Classroom.Courses.CourseWork.create({
"courseId": id,
"title" : title,
"description" : desc,
"workType" : 'ASSIGNMENT',
}, id)
This works fine, creating a draft assignment on Classroom. However, when I try to attach a Drive file like so:
Classroom.Courses.CourseWork.create({
"courseId": id,
"title" : title,
"description" : desc,
"workType" : 'ASSIGNMENT',
"materials" : [
{
"driveFile": {
"id" : fileId,
"title" : fileTitle,
"alternateLink" : fileLink,
"thumbnailUrl" : fileThumbnail,
},
"shareMode" : 'STUDENT_COPY',
}
],
}, id)
I receive these errors:
Execution failed: Invalid JSON payload received. Unknown name "share_mode" at 'course_work.materials[0]': Cannot find field.
Invalid JSON payload received. Unknown name "alternate_link" at 'course_work.materials[0].drive_file': Cannot find field.
Invalid JSON payload received. Unknown name "id" at 'course_work.materials[0].drive_file': Cannot find field.
Invalid JSON payload received. Unknown name "title" at 'course_work.materials[0].drive_file': Cannot find field.
Invalid JSON payload received. Unknown name "thumbnail_url" at 'course_work.materials[0].drive_file': Cannot find field. (line 61, file "Classroom") [1.187 seconds total runtime]
Any help will be appreciated, thank you
You may refer with this SO answer. This error might happen because of wrong formatting of the request.
Sample code:
function myFunction() {
var ClassSource = {
title: "Test File",
state: "DRAFT",
materials: [
{
driveFile:{
driveFile: {
id: "fileID",
title: "Sample Document"
},
shareMode: "STUDENT_COPY"
}
}
],
workType: "ASSIGNMENT"
};
Classroom.Courses.CourseWork.create(ClassSource, COURSEID)
//Logger.log(exec);
}
You may try this using Try this API.
Hope this helps!

Using JSON.parse for accessing a json file

jQuery Code:
$(document).ready(function(){
$.getJSON('dat.js', function(data) {
var obj = JSON.parse(data);
alert(obj[0].title);
});
});
My JSON file :
{
"posts":
[
{
"title": "ajax | Programming ",
"url": "hello"
},
{
"title": "jQuery and Ajax Demos Pard - 3",
"url": "how are you"
},
]
}
Its giving me an error JSON.parse:unexpected character. But when I tried to do it by taking the json inside an array its ok then. I want to access the data from json file itself
you do parseJSON when your input is a json string and u expect an object. Here, getJSON is already giving u the response as an object.
try this
$(document).ready(function(){
$.getJSON('dat.js', function(obj) {
alert(obj.posts[0].title);
});
});
A Quick jslint check says that you have invalid json at line 11 },, Try removing the comma from the last member of "posts" and see if that help
{
"title": "jQuery and Ajax Demos Pard - 3",
"url": "how are you"
}, <---- THIS