2011-09-08 40 views
2

私は管理者(プロフィールではない)であるが、運がないということは、Facebookページの壁に投稿しようとしています。これをどのように達成するのですか?私はページアクセストークンの検索部分にこだわっています。Facebookページに投稿する

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

import facebook 
import urllib 
import urlparse 
import subprocess 
import warnings 

# Hide deprecation warnings. The facebook module isn't that up-to-date (facebook.GraphAPIError). 
warnings.filterwarnings('ignore', category=DeprecationWarning) 




# Parameters of your app and the id of the profile you want to mess with. 
FACEBOOK_APP_ID  = 'XXXXXXXXXXXXXX' 
FACEBOOK_APP_SECRET = 'XXXXXXXXXXXXXXXXXXXXX' 
FACEBOOK_PROFILE_ID = 'XXXXXXXXXXX' 


# Trying to get an access token. Very awkward. 
oauth_args = dict(client_id  = FACEBOOK_APP_ID, 
        client_secret = FACEBOOK_APP_SECRET, 
        scope   = 'manage_pages', 
        response_type = 'token' 
       ) 
oauth_curl_cmd = ['curl', 
        'https://graph.facebook.com/oauth/access_token?' + urllib.urlencode(oauth_args)] 
oauth_response = subprocess.Popen(oauth_curl_cmd, 
            stdout = subprocess.PIPE, 
            stderr = subprocess.PIPE).communicate()[0] 

print urllib.urlencode(oauth_args) 

try: 
    oauth_access_token = urlparse.parse_qs(str(oauth_response))['access_token'][0] 
except KeyError: 
    print('Unable to grab an access token!') 
    exit() 
print oauth_access_token 

facebook_graph = facebook.GraphAPI(oauth_access_token) 


# Try to post something on the wall. 
try: 
    fb_response = facebook_graph.put_wall_post('Hello from Python', \ 
               profile_id = FACEBOOK_PROFILE_ID) 
    print fb_response 
except facebook.GraphAPIError as e: 
    print 'Something went wrong:', e.type, e.message 

答えて

1

これは、あまり安全でなく信頼性が低いため、コマンドラインでcurlを使用することはお勧めしません。あなたはアクセスを取得するにはurllib2のとJSONモジュールと

をこのすべてを行うことができますあなたがちょうどあなたがどうなるhttps://graph.facebook.com/oauth/access_token?client_id=YOUR_APP_ID&client_secret=YOUR_APP_SECRET&grant_type=client_credentials

に電話をしたいトークン:

url='https://graph.facebook.com/oauth/access_token?client_id=YOUR_APP_ID&client_secret=YOUR_APP_SECRET&grant_type=client_credentials' 
target=urllib2.urlopen(url) 
token = target.read()[13:] 

EDIT: マイ悪い、facebook/oauthがプレーンテキストでアクセストークンを与えるのを忘れてしまったので、jsonモジュールは必要ありません。私はあなたが何をすべきかを示すために例を更新しました。注意target.read()はあなたに文字列 'access_token = ACCESS_TOKEN'を与えます。そしてそれを解析して識別子を削除しています。

どのレスポンスがURLに送られ、あなたの情報に入れているのかを見るには、acess_tokenを使ってjson dictになります。

this pageの後半には、必要なすべての情報が必要です。

関連する問題