1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
)
func get_courses(token string, link string, client http.Client) []CanvasCourse {
resp, err := client.Get(link)
if err != nil {
// handle error
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
var canvas_courses []CanvasCourse
err2 := json.Unmarshal(body, &canvas_courses)
if err2 != nil {
fmt.Println(err2)
return nil
}
return canvas_courses
}
func get_aplus(token string, link string, client http.Client) string {
resp, err := client.Get(link)
if err != nil {
// handle error
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
var aplus Aplus
json.Unmarshal(body, &aplus)
return aplus.URL
}
func get_course_ids(canvas_courses []CanvasCourse) []uint64 {
var courses []uint64
for _, course := range canvas_courses {
courses = append(courses, course.ID%10000)
}
return courses
}
func main() {
client := http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
fmt.Println("Redirected to:", req.URL)
return nil
},
}
// https://canvas.instructure.com/api/v1/courses?access_token={}&per_page=100", canvas_token
base_link := "https://floridapolytechnic.instructure.com/api/v1"
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 {
// handle error
}
//fmt.Println(resp)
defer resp.Body.Close()
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)
}
*/
|