2013-03-26 133 views
4

我需要在我的自己的 Facebook頁面上發佈消息;我需要這樣做以編程方式(在我的情況下使用Python)。我成功地做到使用此代碼(在Python)這一部分:Facebook:如何以編程方式檢索訪問令牌?

import urllib, urllib2 

access_token='XXXX' 
fb_page_id='YYYY' # my page ID 

post_data = {'access_token':access_token, 'message':'hey this is a test!'} 
request_path = str(fb_page_id)+'/feed' 
post_data = urllib.urlencode(post_data) 
response = urllib2.urlopen(
    'https://graph.facebook.com/%s' % request_path, post_data 
) 

的FB頁面上生成的帖子的ID是正確返回:

In [11]: response.readlines() 
Out[11]: ['{"id":"135386143198208_461964357207050"}'] 

問題:

爲了生成access_token並提出上述API請求,我必須手動按照詳細here的三個步驟。

但實際上這個手動過程是不可接受的,因爲我需要從cron作業運行此任務。因此我需要自動化它,因爲Facebook中的access_token是暫時的。即每次運行此腳本時,我都需要獲取訪問令牌。怎麼做?

只要您傳達涉及的步驟,就可以在答案(捲曲,JavaScript,Java,PHP)中隨意使用任何腳本工具。請注意,我需要使用任何服務器端語言(Python/Ruby/PHP)來執行此操作。

+2

downvote的任何理由?謝謝;) – 2013-03-26 16:28:03

+0

您不能繞過Facebook訪問令牌的手動用戶身份驗證。這是爲了一個目的。 – phwd 2013-03-26 16:55:48

+0

@phwd而且我們必須至少每兩個月手動驗證一次手動「訪問令牌」......我是否正確? – 2013-03-26 17:02:35

回答

4

如果擴展您的(用戶)訪問令牌,則可以請求一個根本不會實際到期的(Page)訪問令牌。

請參見下面的文檔的「擴展頁面訪問令牌」部分:https://developers.facebook.com/docs/howtos/login/extending-tokens/

+0

確實你是對的!謝謝!這甚至不需要以編程方式完成,因爲我可以只做一次,它不會過期。正確? – 2013-03-26 23:11:46

+0

什麼'APP_ID'和'APP_SECRET'應該傳遞給該URL? – 2013-03-27 00:00:04

+0

當您註冊Facebook應用程序時,您將獲得應用程序ID和密碼。 (創建一個後,它會顯示在頁面的頂部) – 2013-03-31 02:00:21

4

不能以編程方式檢索短期標記。它破壞了用戶交互的目的。

Facebook故意以這種方式確保用戶完全手動控制他們安裝的應用程序。

一旦用戶授權初始接入做一個HTTP請求到

(如果用戶通過更改密碼無效令牌,例如或更早版本),你可以再向上過程自動化,兩個月

https://graph.facebook.com/oauth/access_token? 
    grant_type=fb_exchange_token&   
    client_id=APP_ID& 
    client_secret=APP_SECRET& 
    fb_exchange_token=SHORT_LIVED_ACCESS_TOKEN 

在這兩個月結束之後,用戶必須是重新授予應用程序訪問權限的人員,才能獲得新的短期令牌,然後您可以使用上述代碼重新擴展令牌。

0

要獲取甚至普通用戶Facebook的令牌編程,你可能會感興趣的:https://github.com/fbessez/Tinder/blob/master/fb_auth_token.py,這是一個Python腳本自動檢索提供電子郵件/密碼時的令牌。

請確保您安裝了lxml,requestsrobobrowser,因爲這些是先決條件。無論requestsrobobrowser可以運行

pip install robobrowser

pip install requests

的LXML是一個「小」更靠譜,因爲它已經被編譯很容易獲得性(有一個最新版本)。按照這個SO吧:How to install lxml on Ubuntu

0

祝福寫這段代碼的靈魂。不是我,而是在某個地方找到了它。工作順利。使用您的電子郵件&密碼調用此函數。

MOBILE_USER_AGENT = "Mozilla/5.0 (Linux; U; en-gb; KFTHWI Build/JDQ39) AppleWebKit/535.19 (KHTML, like Gecko) Silk/3.16 Safari/535.19" 
FB_AUTH = "https://www.facebook.com/v2.6/dialog/oauth?redirect_uri=fb464891386855067%3A%2F%2Fauthorize%2F&display=touch&state=%7B%22challenge%22%3A%22IUUkEUqIGud332lfu%252BMJhxL4Wlc%253D%22%2C%220_auth_logger_id%22%3A%2230F06532-A1B9-4B10-BB28-B29956C71AB1%22%2C%22com.facebook.sdk_client_state%22%3Atrue%2C%223_method%22%3A%22sfvc_auth%22%7D&scope=user_birthday%2Cuser_photos%2Cuser_education_history%2Cemail%2Cuser_relationship_details%2Cuser_friends%2Cuser_work_history%2Cuser_likes&response_type=token%2Csigned_request&default_audience=friends&return_scopes=true&auth_type=rerequest&client_id=464891386855067&ret=login&sdk=ios&logger_id=30F06532-A1B9-4B10-BB28-B29956C71AB1&ext=1470840777&hash=AeZqkIcf-NEW6vBd" 

def get_access_token(email, password): 
    s = robobrowser.RoboBrowser(user_agent=MOBILE_USER_AGENT, parser="lxml") 
    s.open(FB_AUTH) 
    ##submit login form## 
    f = s.get_form() 
    f["pass"] = password 
    f["email"] = email 
    s.submit_form(f) 
    ##click the 'ok' button on the dialog informing you that you have already authenticated with the Tinder app## 
    f = s.get_form() 
    s.submit_form(f, submit=f.submit_fields['__CONFIRM__']) 
    ##get access token from the html response## 
    access_token = re.search(r"access_token=([\w\d]+)", s.response.content.decode()).groups()[0] 
    #print s.response.content.decode() 
    return access_token