douyingtai6662 2016-02-19 18:27
浏览 637
已采纳

尝试登录Amazon.com提取数据,但获得Enable Cookies响应(使用Go)

I'm trying to use Go to log into my account on Amazon to automatically pull some information, but I'm having trouble logging in because it complains about cookies. Here's a sanitized version of the code I was using:

package main

import (
"bytes"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"net/url"
"strconv"
)

func CheckThis(AmazonUsername string, AmazonPassword string) error {

var LogonURL string

// Set the url
LogonURL = "https://www.amazon.com/ap/signin"

// Craft some form data
form := url.Values{}
form.Add("appAction", "SIGNIN")
form.Add("email", AmazonUsername)
form.Add("password", AmazonPassword)
form.Add("create", "0")
form.Add("appActionToken", “$VALUE”)
form.Add("openid.pape.max_auth_age", "$VALUE==")
form.Add("openid.identity", "$VALUE=")
form.Add("openid.assoc_handle", "$VALUE")
form.Add("openid.mode", "$VALUE")
form.Add("openid.ns.pape", "$VALUE==")
form.Add("openid.claimed_id", "$VALUE=")
form.Add("pageId", "$VALUE")
form.Add("openid.ns", "$VALUE=")

// Amazon sells cookies
cookieJar, _ := cookiejar.New(nil)

// Create a new client with the cookiejar in the struct...
client := &http.Client{
    Jar: cookieJar,
}

// Craft the request to send to the website with the form containing login info
req, _ := http.NewRequest("POST", LogonURL, bytes.NewBufferString(form.Encode()))

// Some more headers
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Content-Length", strconv.Itoa(len(form.Encode())))
req.Header.Add("Accept-Language", "en-US,en;q=0.8")
req.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
req.Header.Add("Connection", "keep-alive")
req.Header.Add("Host", "www.amazon.com")
req.Header.Add("Referer", "https://www.amazon.com/ap/signin")
req.Header.Add("Upgrade-Insecure-Requests", "1")
req.Header.Add("Origin", "https://www.amazon.com")
req.Header.Add("Cache-Control", "max-age=0")

// And we're off to the races...
resp, _ := client.Do(req)

// What was in the response?
charResponse, _ := ioutil.ReadAll(resp.Body)
resp.Body.Close()

// Write response body to a text file with title of…
_ = WriteOutputToFile(string(charResponse), “response.html")

// All done!
return nil
}

The $VALUE entries are because I'm not sure if the strings are significant to my account so I removed them; these are values I pulled from the developer tools of a Chrome login session. I also removed err checks for brevity.

The reply page (opening response.html on my drive within Chrome) looks like this: Amazon Response

What am I missing in order to keep the cookie with the client req/resp for the sign-in and later pages?

Or am I missing something where the response page I save is trying to pull elements from Amazon when I render the HTML, and the cookie issue is because the browser would be missing cookie information when I'm trying to view the results from the Go application?

  • 写回答

2条回答 默认 最新

  • duandou9931 2016-02-19 19:05
    关注

    I'm pretty sure Amazon uses different data every time when you try to login so better to parse login form. Here is example

    package main
    
    import (
        "bytes"
        "io/ioutil"
        "net/http"
        "net/http/cookiejar"
        "net/url"
        "github.com/PuerkitoBio/goquery"
        "log"
    )
    
    func checkError(err error) {
        if err != nil {
            log.Fatal(err)
        }
    }
    
    func CheckThis(AmazonUsername string, AmazonPassword string) error {
        cookieJar, _ := cookiejar.New(nil)
    
        client := &http.Client{
            Jar: cookieJar,
        }
    
        res, err := client.Get("https://www.amazon.com/gp/sign-in.html/ref=ord_cart_unrec_signin")
        checkError(err)
    
        doc, err := goquery.NewDocumentFromResponse(res)
    
        form := url.Values{}
        doc.Find("form[name='signIn'] input").Each(func(i int, s *goquery.Selection) {
            name, exist := s.Attr("name")
            if exist {
                value, exist := s.Attr("value")
                if exist {
                    form.Add(name, value)
                }
            }
    
        })
        form.Set("email", AmazonUsername)
        form.Set("password", AmazonPassword)
    
        req, _ := http.NewRequest("POST", "https://www.amazon.com/ap/signin", bytes.NewBufferString(form.Encode()))
        req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
    
        res, err = client.Do(req)
        checkError(err)
        defer res.Body.Close()
    
        charResponse, _ := ioutil.ReadAll(res.Body)
    
        ioutil.WriteFile("response.html", charResponse, 0777)
    
        return nil
    }
    
    func main() {
        CheckThis("", "")
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘