2015-11-06 64 views
1

我正在尋找一種在Facebook上發佈狀態的方法,只需提供用戶名(電子郵件),密碼以及我將作爲輸入發佈的狀態即可。 這是我的實際代碼。 (標題是示例,它們是錯誤的)在Facebook上發佈不帶API但通過http請求

代碼不工作,因爲我不知道要發送什麼以及將它發送到哪裏。 (http請求)

我該如何改進?

import requests 

s = requests.Session() 
s.auth = (username, passw) 
s.headers.update({'username': 'Liam', "password": "*********","status": "this is a status"}) 

print s.get('http://httpbin.org/headers', headers={'username': 'Liam', "password": "*********","status": "that's a status"}) 
+1

該代碼無法運行?什麼是問題? –

+0

我不知道有多少和最重要的請求發送和他們的頭@ChadS。 – Veltro

+1

硒就像是用大炮向蚊子射擊,但你可以試試它:) – f126ck

回答

3

我在Facebook羣組中發佈了這個功能。它部分基於一個腳本,該腳本不再適用於將圖片上傳到Facebook頁面。

備註: 1-用戶組ID是硬編碼的。您可以通過查詢將Facebook組名轉換爲ID並將其合併到腳本中的網站進入。我已經爲其他腳本製作了它,並不在意這一點。 2 - 我剛剛刪除了評論,並在發佈之前更改了一些內容,因爲它不是一團糟。它可能會抱怨某些東西(不能在手機上運行),但總的來說它起作用。

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 

import argparse 
import datetime 
import json 
import logging 
import re 
import random 
import requests 
import shutil 
from pyquery import PyQuery as pq 


def main(username, password, page): 

    logging.basicConfig(filename='imgur2fb.log', level=logging.DEBUG) 

    session = requests.session() 

    uid, dtsg = login(session, username, password) 

    grupoID = "580864492030176" #(Fijo) 

    postToFacebook(session, dtsg, grupoID, "Prueba 98765",uid) 


def login(session, username, password): 

    ''' 
    Login to Facebook 
    ''' 

    # Navigate to the Facebook homepage 
    response = session.get('https://facebook.com') 

    # Construct the DOM 
    dom = pq(response.text) 

    # Get the lsd value from the HTML. This is required to make the login request 
    lsd = dom('[name="lsd"]').val() 

    # Perform the login request 
    response = session.post('https://www.facebook.com/login.php?login_attempt=1', data={ 
     'lsd': lsd, 
     'email': username, 
     'pass': password, 
     'default_persistent': '0', 
     'timezone': '-60', 
     'lgndim': '', 
     'lgnrnd': '', 
     'lgnjs': '', 
     'locale':'en_GB', 
     'qsstamp': '' 
    }) 

    ''' 
    Get the users ID and fb_dtsg token. The fb_dtsg token is required when making requests as a logged in user. It 
    never changes, so we only need to grab this token once. 

    If the login was successful a cookie 'c_user' is set by Facebook. If the login failed, the 'c_user' cookie 
    will not be present. This will raise an exception. 
    ''' 
    try: 
     uid = session.cookies['c_user'] 
     dtsg = re.search(r'(type="hidden" name="fb_dtsg" value="([0-9a-zA-Z-_:]+)")', response.text).group(1) 

     dtsg = dtsg[dtsg.find("value")+6:] 
     dtsg = dtsg[1:-1] 

    except KeyError: 
     raise Exception('Login Failed!') 

    return uid, dtsg 


def postToFacebook(session, dtsg, pageID, message,uID): 

    data = { 
     "[0]":"", 
     "[1]":"", 
     "__ajax__":"", #AYmIVqL7VfighmiRmFWSBGl6Aucepl7b-I5RPZaAyEEk7rT-6UQQ2zOpUe433RwWQaZACpH9gA--j8otSHry0_Kmd6iZtK2QHyufgZ59eoLtwA 
     "__dyn":"",# 1KQdAm1mxu4UpwDF3GAgy6K6Acgy6F8mxq2K2i5U9EowRwFzohxO3J0GwywlEf8lwJwsE2xCyoe8hwv9E887u4o2CyUb852i1gw 
     "__req":"",# e 
    "__user":uID, 
    "album_fbid":"0", 
    "appid":"", 
    "at":"",  
    "backdated_day":"", 
    "backdated_month":"", 
    "backdated_year":"", 
    "ch":"",  
    "csid":"", #b4f44053-0da3-46b8-8c54-782fb428a624 
    "fb_dtsg":dtsg, #AQEHse9gjZYA:AQGng01UK1fd 
    "freeform_tag_place":"",  
    "fs":"",  
    "internal_extra":"", 
    "is_backdated":"", 
    "iscurrent":"", 
    "linkUrl":"", 
    "link_no_change":"",  
    "loc":"{}", 
    "m_sess":"",  
    "message":message,# prueba 88 
    "npa":"", 
    "npc":"", 
    "npn":"", 
    "npp":"", 
    "npw":"", 
    "npz":"", 
    "ogaction":"", 
    "oghideattachment":"", 
    "ogicon":"",  
    "ogobj":"", 
    "ogphrase":"", 
    "ogsuggestionmechanism":"", 
    "rating":"0", 
    "scheduled_am_pm":"", 
    "scheduled_day":"", 
    "scheduled_hours":"", 
    "scheduled_minutes":"", 
    "scheduled_month":"", 
    "scheduled_year":"", 
    "sid":"", 
    "source_loc":"composer_group", 
    "target":pageID, 
    "text_[0]":"", 
    "text_[1]":"", 
    "unpublished_content_type":"0", 
    "waterfall_id":"988d79257398ec678a8e287046f322ca", # Esto cambia en cada logueo?? 
    "waterfall_source":"composer_group" 

    } 


    response = session.post('https://m.facebook.com/a/group/post/add/?gid='+pageID+'&refid=18', 

          #params=params, 
          data=data, 
          #headers = {'content-type': 'multipart/form-data'}) 
          headers = {'Content-Type':'application/x-www-form-urlencoded'}) 

    print response 


try: 
    main(username='j*****', password='****', page='https://www.facebook.com/groups/linkdelgrupo') 
except Exception, e: 
    logging.exception(e) 
    print e