我正在尝试使用python构建一个facebook API客户端,代码如下:
import os
import urllib2
import urllib
import cookielib
import hashlib
import json
USER = ''
PASS = ''
LOGIN = 'http://www.facebook.com/login.php?login_attempt=1'
HOST = 'http://api.facebook.com/restserver.php'
API_KEY = 'eca5c767f0e5b65942419574374c34a4'
SECRET_KEY = 'e2aab4000e08f4199f3ca6793ab4f02c'
def getSig(params, secret):
sigStr = ''
for k in sorted(params.keys()):
sigStr += k + '=' + params[k]
sigStr += secret
return hashlib.md5(sigStr).hexdigest()
def call(host, params):
basicParams = {'api_key': API_KEY,
'format': 'JSON',
'v': '1.0'}
basicParams.update(params)
basicParams['sig'] = getSig(basicParams, SECRET_KEY)
finalParams = urllib.urlencode(basicParams)
cj = cookielib.LWPCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj), urllib2.HTTPRedirectHandler)
urllib2.install_opener(opener)
request = urllib2.Request(host)
request.add_header('User-Agent', 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.9) Gecko/20100402 Ubuntu/9.10 (karmic) Firefox/3.5.9')
request.add_header('Content-Type', 'application/x-www-form-urlencoded')
h = urllib2.urlopen(request, finalParams)
return h.read()
def getToken():
paramsRaw = {'method': 'Auth.createToken',
'api_key': API_KEY,
}
ret = call(HOST, paramsRaw)
return json.loads(ret)
def login(user, password, token):
# post login form
paramsRaw = {
'auth_token': token,
'email': user,
'pass': password,
}
call(LOGIN, paramsRaw)
def getSession(token):
paramsRaw = {'method': 'Auth.getSession',
'auth_token': token
}
print call(HOST, paramsRaw)
def main():
token = getToken()
login(USER, PASS, token)
getSession(token)
main()
我基本上是从Auth.createToken接收一个令牌,用USER / PASS将其发布到facebook.com/login.php并将相同的令牌发送到Auth.getSession(如下所述:http://wiki.developers.facebook.com/index.php/Auth.createToken) 但getSession返回错误100(“无效参数”),我做错了什么?网上有类似程序的例子吗?
答案 0 :(得分:1)
您还需要一个call_id参数,该参数可以基于time.time()以及方法参数。