简体   繁体   中英

How to read the wall of a private Facebook Group through a stand-alone python script?

We're making a game and for that game I need to 1) get all the messages in a private group's wall 2) parse the new messages since the last time I checked 3) other stuff not relevant.

I already wrote it, the problem is my access tokens expire from time to time. How can I get new access tokens from the python script ? This python script will be running for several weeks and I can't from time to time wake up in the middle of the night to go click in a dialog granting permissions to read that group's wall.

Also, I found this python code here and I was trying to use it to get the access tokens, but I'm clueless on the oauth_args:

def grabAccessToken ():
  print "grabbing access token..."
  oauth_args = dict(
    client_id     = "000000000000000",
    client_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    redirect_uri  = 'http://domain.com/',
    scope         = "user_groups"
  )
  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 oauth_response
  try:
      oauth_access_token = urlparse.parse_qs(str(oauth_response))['access_token'][0]
      print "got access token: "+oauth_access_token
      return(oauth_access_token)
  except KeyError:
      return('ENOTOKEN')

It keeps responding "{"error":{"message":"Missing authorization Code","type":"OAuthException","code":1}}"

In short: How can a stand-alone Python script keep reading a Facebook Group Wall for a lot of time ?

In short: How can a stand-alone Python script keep reading a Facebook Group Wall for a lot of time ?

You can not get a new access token without user interaction. That's the whole point of deprecating offline_access.

So you have to revisit your app to get a new token when the existing one expires.

I'd suggest storing your long-lived access token and it's expiry date in your database, and then have your app send you an email in the background if that expiry date is approaching. Chose whatever timeframe you think is appropriate – a few days in advance, one or two weeks, whatever you like.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM