开发者

Facebook API non-interactive authorization/login

i'm trying to build a facebook API client using python, code follows:

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(param开发者_如何学JAVAs, 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()

I'm basically receiving a token from Auth.createToken, POSTing it with USER/PASS to facebook.com/login.php and sending the same token to Auth.getSession (as outlined here: http://wiki.developers.facebook.com/index.php/Auth.createToken) But getSession returns error 100 ("Invalid parameter"), what am i doing wrong? Are there any examples of similar programs on the web?


You also need a call_id parameter which can be based on time.time(), as well as a method parameter.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜