2016-08-29 52 views
5

Vì vậy, tôi đã trả lời câu hỏi này trước here. Tuy nhiên, một cái gì đó trên trang web Flurry đã thay đổi và câu trả lời không còn hoạt động nữa.Yêu cầu đăng nhập Flurry.Session() Python 3

from bs4 import BeautifulSoup 
import requests 

loginurl = "https://dev.flurry.com/secure/loginAction.do" 
csvurl = "https://dev.flurry.com/eventdata/.../..."  #URL to get CSV 
data = {'loginEmail': 'user', 'loginPassword': 'pass'} 

with requests.Session() as session: 
    session.headers.update({ 
     "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36"}) 
    soup = BeautifulSoup(session.get(loginurl).content) 
    name = soup.select_one("input[name=struts.token.name]")["value"] 
    data["struts.token.name"] = name 
    data[name] = soup.select_one("input[name={}]".format(name))["value"] 
    login = session.post(loginurl, data=data) 
    getcsv = session.get(csvurl) 

Đoạn mã trên đã hoạt động tốt trong tháng trước và sau đó nó ngừng hoạt động vào tuần trước. Đối với cuộc sống của tôi, tôi không thể tìm ra những gì trên trang web đã thay đổi. ID Tên và mã thông báo tất cả có vẻ chính xác, tên người dùng và pass không thay đổi. Im thua lỗ.

Nếu tôi đăng nhập theo cách thủ công, tôi có thể tải xuống csv chỉ bằng cách sử dụng csvurl.

login.histroy show:

[<Response [302]>, <Response [302]>, <Response [302]>, <Response [302]>, <Response [303]>] 

Nếu bất cứ ai có thể xem xét và tìm ra nơi tôi đang đi sai, tôi sẽ đánh giá rất cao nó.

Cảm ơn.

CẬP NHẬT

Vì vậy, từ địa chỉ đăng nhập mới, tôi thấy bài viết cần phải được định dạng này:

{"data":{"type":"session","id":"bd7d8dc1-4a86-4aed-a618-0b2765b03fb7","attributes":{"scopes":"","email":"myemail","password":"mypass","remember":"false"}}} 

Những gì tôi không thể tìm ra dù là làm thế nào họ đã tạo ra id . Có ai có thể xem không?

+1

ID này trông giống như mã thông báo phiên. Mã thông thường là dữ liệu được mã hóa ngẫu nhiên hoặc phía máy chủ để tự động xác thực người dùng đã đăng nhập và ngăn chặn [session hijacking] (https: // www .owasp.org/index.php/Session_hijacking_attack). Thông qua python, có thể bạn sẽ cần đăng nhập vào dịch vụ web, nhận ID phiên/Mã thông báo yêu cầu dữ liệu bạn muốn xóa. –

Trả lời

1

Bạn có thể cung cấp id phiên giả và nó sẽ đăng nhập bạn bằng một phiên bản mới. Postman thiết bị chặn được trợ giúp với các chuyển hướng.

import requests 
import json 

def login(email, password, session, session_id=None): 
    """ Authenticate with flurry.com, start a fresh session 
     if no session id is provided. """ 
    auth_url = 'https://auth.flurry.com/auth/v1/session' 
    login_url = 'https://login.flurry.com' 
    auth_method = 'application/vnd.api+json' 
    if session_id is None: 
     session_id = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa' 
    response = session.request('OPTIONS', auth_url, data='') 
    headers = response.headers 
    headers.update({'origin': login_url, 'referer': login_url, 
        'accept': auth_method, 'content-type': auth_method}) 
    data = {'data': {'type': 'session', 'id': session_id, 'attributes': { 
      'scopes': '', 'email': email, 'password': password, 'remember': 'false'}}} 
    payload = json.dumps(data) 
    response = session.request('POST', auth_url, data=payload, headers=headers) 
    return response 

email, password = 'your-email', 'your-password' 
session = requests.Session() 
response = login(email, password, session) 
# session_id = response.json()['data']['id'] 

Và sau đó bạn có thể lấy dữ liệu csv của bạn sau khi chạm trang web cũ:

response = session.request('GET', 'https://dev.flurry.com/home.do') 
data = session.request('GET', your_csv_url).text 
+0

Bạn là vị cứu tinh của tôi - tôi sẽ không bao giờ nghĩ ra điều này. – Nefariis

1

Bây giờ, họ có thiết kế mới và new login page mà họ cũng chuyển hướng bạn - đó là lý do bạn thấy mã trạng thái 302 và 303. Quá trình đăng nhập và logic đằng sau nó, các URL, liên kết tới tệp CSV - mọi thứ giờ đây khác và bạn phải "thực hiện lại"/"gợi nhớ lại" nó.

0

bạn có thể sử dụng lib uuid để tạo ra một uuid cho id phiên, để sử dụng giao diện cũ bạn sẽ cần phải thực hiện một yêu cầu đến https://dev.flurry.com/home.do?isFirstPostLogin=true, bây giờ bạn có thể nhận được csv. (biến url_get)

id = uuid.uuid4() 
payload = {"data": 
      {"type":"session", 
      "id": str(id), 
      "attributes":{ 
       "scopes":"", 
       "email": username, 
       "password": password, 
       "remember":"false"} 
      } 
      } 

with session() as api: 
    headers = { 
    'Origin': 'https://login.flurry.com', 
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36', 
    'Content-Type': 'application/vnd.api+json', 
    'Accept': 'application/vnd.api+json', 
    'Connection': 'keep-alive', 
    } 
    req = api.post('https://auth.flurry.com/auth/v1/session', data=json.dumps(payload), headers=headers) 
    if req.status_code == 201: 
    api.get('https://dev.flurry.com/home.do?isFirstPostLogin=true') 
    return api.get(url_get).content.encode('ascii', 'ignore') 
    else: 
    raise Exception('Login failed') 
Các vấn đề liên quan