python - 使用 Python 在 Facebook 粉丝页面上发布

标签 python facebook facebook-graph-api

我尝试了一些关于如何在 Facebook 墙上发布的代码。但我想做一点不同的事情。我想在我的 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

最佳答案

我更正了代码,这是解决方案:

#!/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/中获取

关于python - 使用 Python 在 Facebook 粉丝页面上发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11380520/

相关文章:

python正则表达式匹配 "ab"或 "ba"单词

python - STORE_NAME 和 STORE_GLOBAL 在主要范围内是等价的吗?

javascript - 使用 Facebook 登录但仍然收到 : "Unauthenticated access is not supported for this identity pool"

android - 我在使用 Ionic 2 登录 Facebook.login 时看到 "Class not found"警报

Facebook iframe fbxml,页面选项卡问题

java - Matlab安装(LD_LIBRARY_PATH)把其他库文件弄乱了

电子邮件地址无法正常工作的 Facebook 搜索 API?

javascript - Facebook用户认证流程

iphone - iOS 客户端、Facebook API 和服务器之间的安全通信

python - 在 Python 中创建类后向其添加方法