从需要登录的私有网站获取信息(使用Golang)

huangapple go评论75阅读模式
英文:

Pull info from private site that requires login (Golang)

问题

我正在尝试从我的在线课程表中获取我的课程,但是似乎无法通过登录阶段。我的代码如下:

package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
	"strings"

	"github.com/PuerkitoBio/goquery"
)

type App struct {
	Client *http.Client
}

type Timetable struct {
	Name string
}

const (
	baseURL string = "https://myclasswebsite.com"
)

func (app *App) login() {
	//login := loginInfo()
	client := app.Client
	loginURL := baseURL + "/portal2/#!/login"

	data := url.Values{
		"inputEmail": {"my_actual_username"},
		"password":   {"my_actual_password"},
	}

	response, err := client.PostForm(loginURL, data)

	if err != nil {
		log.Fatalln(err)
	}

	defer response.Body.Close()

	_, err = ioutil.ReadAll(response.Body)
	if err != nil {
		log.Fatalln(err)
	}
}

func (app *App) getTimetable() []Timetable {
	timetableURL := baseURL + "/portal/dashboard"
	client := app.Client

	response, err := client.Get(timetableURL)
	if err != nil {
		log.Fatalln("Error fetching response. ", err)
	}

	defer response.Body.Close()

	document, err := goquery.NewDocumentFromReader(response.Body)

	fmt.Println(document.Html())

	if err != nil {
		log.Fatal("Error loading HTTP response body. ", err)
	}

	var classes []Timetable

	document.Find(".timetable table").Each(func(i int, s *goquery.Selection) {
		className := strings.TrimSpace(s.Text())
		class := Timetable{
			Name: className,
		}

		classes = append(classes, class)
	})

	return classes
}

func main() {
	jar, _ := cookiejar.New(nil)

	app := App{
		Client: &http.Client{Jar: jar},
	}

	app.login()
	classes := app.getTimetable()
	fmt.Println("class array is", classes)
	for index, class := range classes {
		fmt.Printf("%d: %s\n", index+1, class.Name)
	}
}

最后的打印返回一个空切片,并且当我将 response.Html() 打印到控制台时,我收到的是登录页面的 HTML 而不是仪表板页面的 HTML。

我并不指望有人为我修复这个问题,但是再多一双眼睛和一个指引方向的线索可能会有所帮助。非常感谢!

英文:

I am trying to pull my classes from my online timetable, however, it seems as if I cannot get past the login stage. My code is:

package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
"github.com/PuerkitoBio/goquery"
)
type App struct {
Client *http.Client
}
type Timetable struct {
Name string
}
const (
baseURL string = "https://myclasswebsite.com"
)
func (app *App) login() {
//login := loginInfo()
client := app.Client
loginURL := baseURL + "/portal2/#!/login"
data := url.Values{
"inputEmail": {"my_actual_username"},
"password":   {"my_actual_password"},
}
response, err := client.PostForm(loginURL, data)
if err != nil {
log.Fatalln(err)
}
defer response.Body.Close()
_, err = ioutil.ReadAll(response.Body)
if err != nil {
log.Fatalln(err)
}
}
func (app *App) getTimetable() []Timetable {
timetableURL := baseURL + "/portal/dashboard"
client := app.Client
response, err := client.Get(timetableURL)
if err != nil {
log.Fatalln("Error fetching response. ", err)
}
defer response.Body.Close()
document, err := goquery.NewDocumentFromReader(response.Body)
fmt.Println(document.Html())
if err != nil {
log.Fatal("Error loading HTTP response body. ", err)
}
var classes []Timetable
document.Find(".timetable table").Each(func(i int, s *goquery.Selection) {
className := strings.TrimSpace(s.Text())
class := Timetable{
Name: className,
}
classes = append(classes, class)
})
return classes
}

I changed the base URL and login info just for privacy reasons, however, the rest of the code is as-is.

My main. go file is:

package main
import (
"fmt"
"net/http"
"net/http/cookiejar"
)
func main() {
jar, _ := cookiejar.New(nil)
app := App{
Client: &http.Client{Jar: jar},
}
app.login()
classes := app.getTimetable()
fmt.Println("class array is", classes)
for index, class := range classes {
fmt.Printf("%d: %s\n", index+1, class.Name)
}
}

The final print returns an empty slice, and when I print the response.Html() to the console, I receive the login-page Html rather than the dashboard-page HTML.

I'm in no way expecting anyone to fix this for me but a second pair of eyes and maybe a clue in which direction I should go would be helpful. Thank you so much!

答案1

得分: 1

由于我对你的课程网站不熟悉,以下是一些建议:

你没有检查登录调用的状态码。你可能得到了一个非200的状态码。

确认状态码后,检查cookie jar以确保已保存cookie。这可能不太可能发生,但还是值得检查一下。

最后,尝试使用curl和-v选项进行相同的序列(如果你还没有这样做)。这将为你提供更多关于调用计划发生了什么的信息。

英文:

Since I'm unfamiliar with your class website, some ideas for progressing:

You're not checking the status code from your login call. You may be getting a non-200 status code.

After you've confirmed the status code, check the cookie jar to ensure that a cookie has been saved. This is probably on the unlikelier side, but it's worth checking.

Lastly, attempt the same sequence with curl with -v (if you haven't already). That will give you more insight as to what is happening with your call plan.

答案2

得分: 0

我在测试过程中不小心忽略了一些 cookie。哎呀...

英文:

I ended up ignoring some cookies accidentally while testing. Whoops...

huangapple
  • 本文由 发表于 2021年6月23日 20:50:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/68100183.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定