0

我創建了一個Facebook頁面,並且正在嘗試使用下面的代碼在頁面牆上發佈信息。它是用Python編寫的,並使用Facebook SDK。發佈到頁面的Python facebook SDK權限錯誤

def main(): 
    cfg = { 
    "page_id"  : "PAGE_ID", # Step 1 
    "access_token" : "USER_ACCESS_TOKEN" # Step 3 
    } 

    graph = facebook.GraphAPI(cfg['access_token']) 
    id = graph.get_object('me')['id'] 
    print(graph.get_permissions(user_id=id)) 
    resp = graph.get_object('me/accounts') 

    page_access_token = None 
    for page in resp['data']: 
     if page['id'] == cfg['page_id']: 
      page_access_token = page['access_token'] 
    api = facebook.GraphAPI(page_access_token) 

    msg = "Hello, world!" 
    print(api.get_permissions(user_id=id)) 
    print(api.put_wall_post(msg)) 

這是給下面的輸出錯誤:

{'user_posts', 'publish_actions', 'public_profile', 'pages_manage_cta', 'manage_pages'} 
{'user_posts', 'publish_actions', 'public_profile', 'pages_manage_cta', 'manage_pages'} 
    print(api.put_wall_post(msg)) 
    File "C:\Python34\lib\site-packages\facebook_sdk-3.0.0a0-py3.4.egg\facebook\__init__.py", line 188, in put_wall_post 
    File "C:\Python34\lib\site-packages\facebook_sdk-3.0.0a0-py3.4.egg\facebook\__init__.py", line 169, in put_object 
    File "C:\Python34\lib\site-packages\facebook_sdk-3.0.0a0-py3.4.egg\facebook\__init__.py", line 298, in request 
facebook.GraphAPIError: (#200) The user hasn't authorized the application to perform this action 

我不明白我做錯了什麼?我給了用戶正確的權限。我查了其他重複的問題,但他們的解決方案不適用於當前的FacebookSDK。有人可以幫幫我嗎?

+1

用戶需要授予應用程序publish_pages才能在頁面上發佈 – WizKid

+0

非常感謝您,我一直在努力工作6個小時來實現這一目標。 – pratibha

+0

請將其發佈爲答案,以便我可以將其標記爲已接受。 – pratibha

回答

0

對於要發佈的頁面,請轉至頁面上的關於選項卡,並在底部獲取頁面ID。記筆記或保存。然後,

  • 首先在Facebook開發者控制檯上創建一個應用程序: https://developers.facebook.com/apps。提供顯示名稱和電子郵件,點擊創建應用程序ID。
    • 轉到設置>基本>選擇類別(頁面應用)>保存更改。
    • 轉到應用程序審查>使'AppName'公開在頂部>切換到是,然後確認。這將使應用程序公開。
    • 重新轉到儀表板並記下應用程序ID和應用程序密鑰(點擊它旁邊的顯示,它會詢問fb密碼)。

現在生成的訪問令牌:

  • 轉到https://developers.facebook.com/tools/explorer。在應用程序下拉菜單>選擇先前創建的應用程序>從獲取令牌>獲取用戶訪問令牌>從選擇權限>檢查manage_pages,publish_actions,publish_pages>獲取訪問令牌。
  • 單擊繼續作爲用戶(用戶名),將生成令牌。
  • 出現在「訪問令牌」字段中的令牌是您的短期訪問令牌。保存此令牌時,將需要此令牌來生成長壽命令牌。

這是一個短暫的令牌將在兩個小時內過期。保存令牌。安裝Facebook的SDK:

pip install facebook-sdk 

使用下面的代碼:

import facebook 

def main(): 
    # Fill in the values noted in previous steps here 
    cfg = { 
    "page_id"  : "value of the page id", # Step 1 
    "access_token" : "token you generated" # Step 3 
    } 

api = get_api(cfg) 
msg = "Hello, world!" 
status = api.put_wall_post(msg) 

def get_api(cfg): 
    graph = facebook.GraphAPI(cfg['access_token']) 
    # Get page token to post as the page. You can skip 
    # the following if you want to post as yourself. 
    resp = graph.get_object('me/accounts') 
    page_access_token = None 
    for page in resp['data']: 
    if page['id'] == cfg['page_id']: 
    page_access_token = page['access_token'] 
    graph = facebook.GraphAPI(page_access_token) 
    return graph 

if __name__ == "__main__": 
    main() 

或者乾脆你可以使用以下命令:

import facebook 
def main(): 
    graph = facebook.GraphAPI(access_token='your_user_access_token', version='2.8') 
    #if version 2.8 show error use 2.6 
    attachment = { 
     'name': 'Link name' 
     'link': 'https://www.example.com/', 
     'caption': 'Check out this example', 
     'description': 'This is a longer description of the attachment', 
     'picture': 'https://www.example.com/thumbnail.jpg' 
    } 

    graph.put_wall_post(message='Check this out...', attachment=attachment, profile_id='your_page_id') 

if __name__ == "__main__": 
    main() 

記住令牌將在2小時內到期。在這種情況下,您可以生成一個長壽命的令牌或永久令牌。

+0

我在評論中使用了WizKid提供的解決方案。我缺少publish_pages權限。這解決了這個問題。 – pratibha