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 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型