3

http://developers.facebook.com/docs/reference/ads-api/batch-requests/にあるバッチ処理の例に従おうとしています。

具体的には、curlコマンド:

  curl -F 'access_token=____' 
    -F 'batch=[
               {
                "method": "POST",
                "relative_url": "6004251715639",
                "body": "redownload=1&max_bid=35"
               },
               {
                "method": "POST",
                "relative_url": "6004251716039",
                "body": "redownload=1&max_bid=35"
               },
               {
                "method": "POST",
                "relative_url": "6004251715839",
                "body": "redownload=1&max_bid=35"
               }
              ]' https://graph.facebook.com

正常に動作します。

Pythonでurllib2を使おうとすると、「-F」フラグをエミュレートする方法がわかりません。

単一のリクエストに対して「-d」だったとき、私は何をすべきかを知っていました。

curl -d  "name=Chm&daily_budget=1000&lifetime_budget=10000
&campaign_status=1" "https://graph.facebook.com/
act_368811234/adcampaigns?access_token=___"

Pythonコードを使用してエミュレートしました:

def sendCommand(self, url, dataForPost=None):        
    if dataForPost == None:
        req = urllib2.Request(url)
    else:
        req = urllib2.Request(url, dataForPost)        
    jar = cookielib.CookieJar()
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar))
    content = opener.open(req)
    response = content.read()
    return response

上記の-Fコマンドをエミュレートするにはどうすればよいですか?

4

2 に答える 2

2

関数sendCommandが機能するはずです。dataForPostは辞書を期待しています。以下のものを渡すと、access_tokenとbatchの-F関数が複製されます。"""文字列リテラルを使用して、空白を削除しています。そのままにしておくこともできますが、urllib2はそれをurlエンコードしようとするため、デバッグが難しくなる可能性があります。バッチ値を生成するためにjsonライブラリを使用してみてください。

dataForPost = {'access_token' : '____',  
               'batch' : """[
               {
                "method": "POST",
                "relative_url": "6004251715639",
                "body": "redownload=1&max_bid=35"
               },
               {
                "method": "POST",
                "relative_url": "6004251716039",
                "body": "redownload=1&max_bid=35"
               },
               {
                "method": "POST",
                "relative_url": "6004251715839",
                "body": "redownload=1&max_bid=35"
               }
              ]""".replace('\n', '').replace('\t', '').replace(' ', '')}
于 2012-06-07T22:01:03.607 に答える
-1

Pythonの場合、Facepyを使用して、バッチリクエストの機能を取得し、FacebookGraphAPIのページネーションを処理できます。

Facebookによると、graph apiは一度に50のリクエストを受け取りますが、facepyはそれを処理します。つまり、リストにリクエストをいくつでも追加してバッチ処理できます。

from facepy import GraphAPI
access = <access_token>
batch1=[
    {'method': 'GET', 'relative_url': 'me/accounts'}
]
graph = GraphAPI(access)
data= graph.batch(batch1)

for i in data:
    print i
于 2015-03-24T15:46:57.123 に答える