Attaching a Drive file to an assignment with Google Classroom API - json

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!

Related

Get all fields from google spreadsheet with api v4

I'm setting up a project in NodeJS and for testing I get the information from a spreadsheet in JSON format from the following page: https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/batchGet
To obtain the information of a sheet, in the spreadsheetId field I put the ID of the spreadsheet and in the range field the name of the sheet.
This is the JSON I get:
{
"spreadsheetId": "{spreadsheetId}",
"valueRanges": [
{
"range": "Parameters!A1:Z1000",
"majorDimension": "ROWS",
"values": [
[
"Country",
"COLOMBIA"
]
]
}
]
}
What I want is to show all the fields of the sheet to obtain the title and the modification and creation dates. To do this, in the fields field, I am putting the title string but I get the following error:
{
"error": {
"code": 400,
"message": "Request contains an invalid argument.",
"status": "INVALID_ARGUMENT",
"details": [
{
"#type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"field": "title",
"description": "Error expanding 'fields' parameter. Cannot find matching fields for path 'title'."
}
]
}
]
}
}
I have tried putting * but I get the same JSON.
My problem: How can I get the date of creation and the date of last modification of the spreadsheet?
you must use this endpoint to get a spreadsheet properties: https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/get

JSON - replacement characters for Rest API query

I'm trying to retrieve data from a WEB API rest server.
https://stat-xplore.dwp.gov.uk/webapi/online-help/Open-Data-API.html
Which stores the data in Cubes.
I understand the basics and am successfully retrieving data. However I want to use a Postcode, (ZIP Code) grouping dimension.
This dimension groups postcodes by their first 3 characters. CF1, CF2 etc.
The schema has this as the definition for sending the request for this dimension.
["str:group:UC_Households:X_Geography+%28postcode%29"]
I'm assuming I'm meant to replace the end part with the Postcode group that I'm interested in and enclose the postcode group with some special characters.
So far I have tried
["str:group:UC_Households:X_Geography:CF1"]
["str:group:UC_Households:X_Geography:'CF1'"]
["str:group:UC_Households:X_Geography:#CF1#"]
["str:group:UC_Households:X_Geography:CF1%"]
["str:group:UC_Households:X_Geography#CF1#"]
["str:group:UC_Households:X_GeographyCF1"]
["str:group:UC_Households:X_Geography:CF1"]
the complete JSON is
{ "database" : "str:database:UC_Households",
"measures" : [ "str:count:UC_Households:V_F_UC_HOUSEHOLDS" ],
"dimensions" : [
[ "str:field:UC_Households:V_F_UC_HOUSEHOLDS:HNFAMILY_TYPE" ],
["str:group:UC_Households:X_Geography:CF1"]
]
}
Any help would be appreciated.
How to easily generate the query JSON for the Stat-Xplore API
Log into https://stat-xplore.dwp.gov.uk
Open the data set and table you want
In the top-right hand corner, find the "Download Table" tool
In the drop-down box, select "Open Data API Query (.json) and click "Go"
This will download a JSON file with the parameters for the query that describes the table you're looking at in your web browser. An example is shown below:
{
"database" : "str:database:UC_Monthly",
"measures" : [ "str:count:UC_Monthly:V_F_UC_CASELOAD_FULL" ],
"recodes" : {
"str:field:UC_Monthly:F_UC_DATE:DATE_NAME" : {
"map" : [ [ "str:value:UC_Monthly:F_UC_DATE:DATE_NAME:C_UC_DATE:202010" ] ],
"total" : false
},
"str:field:UC_Monthly:V_F_UC_CASELOAD_FULL:EMPLOYMENT_CODE" : {
"map" : [ [ "str:value:UC_Monthly:V_F_UC_CASELOAD_FULL:EMPLOYMENT_CODE:C_UC_EMPLOYMENT:0" ], [ "str:value:UC_Monthly:V_F_UC_CASELOAD_FULL:EMPLOYMENT_CODE:C_UC_EMPLOYMENT:1" ] ],
"total" : true
},
"str:field:UC_Monthly:V_F_UC_CASELOAD_FULL:PARLC_CODE" : {
"map" : [ [ "str:value:UC_Monthly:V_F_UC_CASELOAD_FULL:PARLC_CODE:V_C_MASTERGEOG11_PARLC_TO_REGION:E14000541" ], [ "str:value:UC_Monthly:V_F_UC_CASELOAD_FULL:PARLC_CODE:V_C_MASTERGEOG11_PARLC_TO_REGION:E14000542" ], [ "str:value:UC_Monthly:V_F_UC_CASELOAD_FULL:PARLC_CODE:V_C_MASTERGEOG11_PARLC_TO_REGION:W07000041" ] ],
"total" : true
}
},
"dimensions" : [ [ "str:field:UC_Monthly:V_F_UC_CASELOAD_FULL:PARLC_CODE" ], [ "str:field:UC_Monthly:F_UC_DATE:DATE_NAME" ], [ "str:field:UC_Monthly:V_F_UC_CASELOAD_FULL:EMPLOYMENT_CODE" ] ]
}
The API table endpoint docs give more info.
An explanation for OP's issue
I'm also attempting to use this API. (I happen to be using Python requests library.)
If I make a request to the /v1/table endpoint where one dimension is of type GROUP (rather than FIELD)
measures=["str:count:UC_Monthly:V_F_UC_CASELOAD_FULL"],
dimensions=[["str:field:UC_Monthly:F_UC_DATE:DATE_NAME"],
["str:group:UC_Monthly:X_Geography+%28residence-based%29"]
]
then this error 422 is returned in the HTTP response:
requests.exceptions.HTTPError: 422 Client Error: Unprocessable Entity for url: https://stat-xplore.dwp.gov.uk/webapi/rest/v1/table
This is the payload of the response, which describes the error in more detail:
{
"message":"Unexpected schema component type 'group' for 'str:group:UC_Monthly:X_Geography+%28residence-based%29'. Expected one of type [field, valueset, udf].",
"errorType":"UNEXPECTED_SCHEMA_COMPONENT_TYPE_EXCEPTION",
"component" : "str:group:UC_Monthly:X_Geography+%28residence-based%29",
"type" : "group",
"expectedTypes" : [ "field", "valueset", "udf" ]
}
If I understand this correctly, it's not complaining about the formatting of the string, it's saying that only FIELD, VALUESET and UDF objects are expected.
If you look at the schema endpoint one can retrieve info about the possible groups, fields, etc.
{
'id': 'str:group:UC_Monthly:X_Geography+%28postcode%29',
'label': 'Geography (postcode)',
'location': 'https://stat-xplore.dwp.gov.uk/webapi/rest/v1/schema/str:group:UC_Monthly:X_Geography+%2528postcode%2529',
'type': 'GROUP'
}

Google Classroom - Programmatically create assignment

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

mailchimp "Segment test" error

im working on mailchimp api. i wanted to create a segment but before creating the segment i wanted to test the condition. so i tried this code
{
"apikey": "xxxxxx7e0992xxx1e3bdxxxe03axxx342-us3",
"list_id": "ec3d67c617",
"options": {
"match": "all",
"conditions": [
{
"field" : "testgrouptitle",
"op" : "all",
"value" : "testgroupname"
}
]
}
}
after firing this in postman(chrome) im getting this error
{
"status": "error",
"code": 506,
"name": "Invalid_Options",
"error": "op=\"all\" is invalid for field=\"testgrouptitle\" and value=\"testgroupname\"" }
the error might be the incorrect condition. can anyone tell me what wrong in the json code?
thanks in advance.
If you want to make a condition by Group:
Field = "interests-X": where X is the Grouping Id from lists/interest-groupings()
Valid Op(erations): one / none / all
Valid Values: a comma delimited string of interest groups for the list, just like you'd use in lists/subscribe() - see lists/interest-groupings()
For more information, refer to the documentation

boto 2.8.0 and cloudseach

Hi I am trying to add documents to a cloudsearch domain as per
http://docs.pythonboto.org/en/latest/cloudsearch_tut.html#adding-documents-to-the-index
my code snppet is:
import boto
conn = boto.connect_cloudsearch(aws_access_key_id='<>',aws_secret_access_key='<>')
domain = conn.lookup('testfoo')
doc_service = domain.get_document_service()
doc_service.add(doc_id, version, data)
First I got the same requests issues
Boto CloudSearch on GAE: TypeError: request() got an unexpected keyword argument 'config'
and so I removed the config kwarg (also not sure of the consequences)
and then I get
boto.cloudsearch.document.CommitMismatchError: Incorrect number of adds returned. Commit: 1 Response: 0
My data is like this
[
{
"raw" : "whole bunch of raw text",
"title" : "My new title",
"blurb" : "A really exciting article",
"document_type" : "Tech Guide",
"url" : "http://www.foobar/7199/tech-advice"
}
]
Any help greatly appreciated
It turns out the problem is when I built up the json in data it is not json but a string. So when this produces the json being sent to the cloudsearch domain and it combines the id and "Add" operation it includes "fields": "[
{
"raw" : "whole bunch of raw text",
"title" : "My new title",
"blurb" : "A really exciting article",
"document_type" : "Tech Guide",
"url" : "http://www.foobar/7199/tech-advice"
}
]" as a string
The solution is simply data needs json encoding
doc_service.add(doc_id, version, json.loads(data))
Here just remove [] from data. Because if you have single object then you must pass it with dictionay. {}
[
{
"raw" : "whole bunch of raw text",
"title" : "My new title",
"blurb" : "A really exciting article",
"document_type" : "Tech Guide",
"url" : "http://www.foobar/7199/tech-advice"
}
]