isolate form HTML

This commit is contained in:
Juan Roig 2023-10-30 17:58:38 -04:00
parent 19957a5adc
commit cc3ab10c6a

107
main.go
View File

@ -5,6 +5,7 @@ import (
"fmt"
"io"
"net/http"
"strings"
)
func get_courses(token string, link string, client http.Client) []CanvasCourse {
@ -58,21 +59,22 @@ func main() {
// https://canvas.instructure.com/api/v1/courses?access_token={}&per_page=100", canvas_token
base_link := "https://floridapolytechnic.instructure.com/api/v1"
token := "insert api key here"
token := "token here"
// course selection by user happens below
//favorites := fmt.Sprintf("%s/users/self/favorites/courses?access_token=%s&per_page=100", base_link, token)
//all_courses := fmt.Sprintf("%s/courses?access_token=%s&per_page=100", base_link,token)
//var canvas_courses []CanvasCourse
//canvas_courses = get_courses(token, favorites, client)
//var course_ids []uint64
//course_ids = get_course_ids(canvas_courses)
//selected := 1 // course_ids[selected]
// course selection by user happens above
aplus_link := fmt.Sprintf("%s/courses/%d/external_tools/sessionless_launch?id=913&access_token=%s", base_link, 7329, token) // 7329 always works
var aplus string
aplus = get_aplus(token, aplus_link, client)
fmt.Println(aplus)
resp, err := client.Get(aplus)
if err != nil {
@ -82,9 +84,104 @@ func main() {
//fmt.Println(resp)
defer resp.Body.Close()
//body, err := io.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
//fmt.Println(string(body))
//fmt.Println(resp)
//fmt.Println("Status code:", resp.StatusCode)
if err != nil {
// handle error
}
body_str := string(body)
form_start := strings.Index(body_str, "<form")
form_end := strings.Index(body_str, "</form>") + 7
form_html := body[form_start:form_end]
fmt.Println(string(form_html))
}
/*
The code below takes the form that defines the post request we must perform to the aplusattendance.com site, and actually maps it to a request.
what I have to do is programmatically grab that form, translate it on the fly, and submit the request rather than doing this manually.
// Define the form data as a map of key-value pairs.
formData := map[string]string{
"oauth_consumer_key": "aplus-attendance",
"oauth_signature_method": "HMAC-SHA1",
"oauth_timestamp": "1698650339",
"oauth_nonce": "nV9EswFv8LtXXRVgRmWW8NSQ7mO3Sheeqi1SjJWPJGE",
"oauth_version": "1.0",
"context_id": "0c1f49ff7b35cba2226f93e9cbb84ae810ed00ab",
"context_label": "CIS4367.01I&T",
"context_title": "Computer Security(FA 2023_CIS4367.01 I&T",
"custom_canvas_api_domain": "floridapolytechnic.instructure.com",
"custom_canvas_course_id": "7329",
"custom_canvas_enrollment_state": "active",
"custom_canvas_user_id": "6858",
"custom_canvas_user_login_id": "jroig9590",
"custom_canvas_workflow_state": "available",
"ext_roles": "urn:lti:instrole:ims/lis/Student,urn:lti:role:ims/lis/Learner,urn:lti:sysrole:ims/lis/User",
"launch_presentation_document_target": "iframe",
"launch_presentation_locale": "en",
"launch_presentation_return_url": "https://floridapolytechnic.instructure.com/courses/7329",
"lis_course_offering_sourcedid": "7093",
"lis_person_contact_email_primary": "jroig9590@floridapoly.edu",
"lis_person_name_family": "Roig",
"lis_person_name_full": "Juan Roig",
"lis_person_name_given": "Juan",
"lis_person_sourcedid": "10308",
"lti_message_type": "basic-lti-launch-request",
"lti_version": "LTI-1p0",
"oauth_callback": "about:blank",
"resource_link_id": "0c1f49ff7b35cba2226f93e9cbb84ae810ed00ab",
"resource_link_title": "aPlus+ Attendance",
"roles": "Learner",
"tool_consumer_info_product_family_code": "canvas",
"tool_consumer_info_version": "cloud",
"tool_consumer_instance_contact_email": "notifications@instructure.com",
"tool_consumer_instance_guid": "7db438071375c02373713c12c73869ff2f470b68.floridapolytechnic.instructure.com",
"tool_consumer_instance_name": "Florida Polytechnic University",
"user_id": "91ba5b390440f646d9d4c47a0dc14f91cf595887",
"user_image": "https://canvas.instructure.com/images/messages/avatar-50.png",
"oauth_signature": "u3mIfUZ+qgSN4zzk0Ipklc0A7KU=",
}
// Create an HTTP client
client := &http.Client{}
// Create a new request
req, err := http.NewRequest("POST", "https://floridapoly.aplusattendance.com/canvas", bytes.NewBuffer([]byte{}))
if err != nil {
fmt.Println("Error creating the request:", err)
return
}
// Add the form data as URL-encoded values to the request
q := req.URL.Query()
for key, value := range formData {
q.Add(key, value)
}
req.URL.RawQuery = q.Encode()
// Set the appropriate content type for the form
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
// Perform the POST request
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error sending the POST request:", err)
return
}
defer resp.Body.Close()
// Check the response
if resp.StatusCode == http.StatusOK {
fmt.Println("Request successful. You may need to parse and process the response body if applicable.")
} else {
fmt.Println("Request failed with status code:", resp.Status)
}
*/