2012-07-08 85 views
1

我嘗試了一些關於如何在Facebook牆上發佈的代碼。但我想做一點不同的事情。我想知道在我的Facebook粉絲頁面發佈。以下代碼只是在我的個人資料上發佈。任何人都可以給我一個在粉絲頁面發佈的線索嗎?使用Python發佈在Facebook粉絲頁面

#!/usr/bin/python 

import facebook 
import urllib 
import urlparse 

FACEBOOK_APP_ID = 'X' 
FACEBOOK_APP_SECRET = 'Y' 
FACEBOOK_PROFILE_ID = 'MyProfileId (**not page id, right?**)' 

oauth_args = dict(client_id  = FACEBOOK_APP_ID, 
        client_secret = FACEBOOK_APP_SECRET, 
        grant_type = 'client_credentials') 

oauth_response = urllib.urlopen('https://graph.facebook.com/oauth/access_token?' + urllib.urlencode(oauth_args)).read()         
page_token='PAGE TOKEN GOT INhttps://graph.facebook.com/SITE' 
fields=access_token 
attach = { 
    "name": 'Hello world', 
    "link": 'http://www.example.com', 
    "caption": 'test post', 
    "description": 'some test', 
    "picture" : 'http://www.example.com/picture.jpg', 
    "page_token" : page_token 
} 
try: 
    oauth_access_token = urlparse.parse_qs(str(oauth_response))['access_token'][0] 
except KeyError: 
    raise 
print oauth_access_token 
facebook_graph = facebook.GraphAPI(oauth_access_token) 
try: 
    response = facebook_graph.put_wall_post('', attachment=attach,profile_id = FACEBOOK_PROFILE_ID) 
except facebook.GraphAPIError as e: 
    print e 
+0

獲取頁面訪問令牌,並張貼到的進料口連接你的頁。這些都在FB文檔中,請看一下。 – CBroe 2012-07-08 11:26:08

+0

我已經有頁面訪問令牌,但我仍然有問題。首先我得到了「演員是頁面的帖子不能包含target_id」然後我從put_wall_post中刪除了FACEBOOK_PROFILE_ID,並且我得到了「主題必須是頁面」。 – Thomas 2012-07-08 12:47:04

回答

1

我糾正了代碼,這裏是解決方案:

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

import facebook 
import urllib 
import urlparse 

access_token_page='X' 
FACEBOOK_APP_ID = 'Y' 
FACEBOOK_APP_SECRET = 'Z' 
FACEBOOK_PROFILE_ID = 'W' 

oauth_args = dict(client_id  = FACEBOOK_APP_ID, 
        client_secret = FACEBOOK_APP_SECRET, 
        grant_type = 'client_credentials') 

oauth_response = urllib.urlopen('https://graph.facebook.com/oauth/access_token?' + urllib.urlencode(oauth_args)).read()         

attach = { 
    "name": 'Hello world', 
    "link": 'http://www.example.com', 
    "caption": 'test post', 
    "description": 'some test', 
    "picture" : 'http://www.example.com/picture.jpg', 
} 


facebook_graph = facebook.GraphAPI(access_token_page) 
try: 
    response = facebook_graph.put_wall_post('', attachment=attach) 
except facebook.GraphAPIError as e: 
    print e 

有關身份驗證的信息可以得到:https://developers.facebook.com/docs/authentication/pages/

+4

這是否實際發佈到粉絲頁面或只是特定用戶的流?我沒有看到關於實際粉絲頁面的任何細節。 – chaostheory 2013-07-12 18:12:38