def fb_current_user(self): """Returns the logged in Facebook user, or None if unconnected.""" if not hasattr(self, "_fb_current_user"): self._fb_current_user = None user_id = facebookoauth.parse_cookie(self.request.cookies.get("fb_user")) if user_id: self._fb_current_user = FBUser.get_by_key_name(user_id) return self._fb_current_user
def putwall(req,message,attachment): import facebookoauth from facebookoauth import FBUser import pprint, StringIO # prid=self.request.get('id') # prid="me" # args = dict(grant_type="client_credentials", client_id=FACEBOOK_APP_ID, client_secret=FACEBOOK_APP_SECRET) # response = cgi.parse_qs(urllib.urlopen("https://graph.facebook.com/oauth/access_token?" +urllib.urlencode(args)).read()) # access_token = response["access_token"][-1] #print access_token hout = "" user_id = facebookoauth.parse_cookie(req.request.cookies.get("fb_user")) errorjson = False aaa="<h2>wall post</h2>error" profile = {'id': 0} perm_mp=False perm_pa=False permtext="" cmd=req.request.get('cmd') oid=req.request.get('oid') if user_id: fb2_current_user = FBUser.get_by_key_name(user_id) if fb2_current_user and fb2_current_user.login: try: graph = GraphAPI(fb2_current_user.access_token) profile = graph.get_object("me") permissions = graph.request("/v2.1/me/permissions") for item in permissions["data"]: if item["permission"]=="manage_pages" and item["status"] == "granted": perm_mp=True if item["permission"]=="publish_actions" and item["status"] == "granted": perm_pa=True if not perm_mp: permtext = "manage_pages" if not perm_pa: permtext = permtext + " publish_actions" s = StringIO.StringIO() # pprint.pprint(permissions, s) # message = "Zinute patalpinta per\nhttp://www.nerij.us/fbputwall" # attachment = {} # attachment['name'] = "Nerijaus Terebo turinio valdymo sistemele \"Upelis\"" # attachment['caption'] = 'www.upelis.org' # attachment['link'] = 'http://www.upelis.org/' # attachment['picture'] = 'http://www.nerij.us/static/images/upelis116.jpg' # attachment['description'] = 'Turinio valdymo sistemele \"Upelis\" - Python, AppEngine' if cmd =="delperm": # mess = graph.delete_object(oid) # mess = graph.request(oid, post_args={"method": "delete"}) post_args=dict(method = "delete", access_token = fb2_current_user.access_token) post_data = urllib.urlencode(post_args) file = urllib.urlopen("https://graph.facebook.com/me/permissions" , post_data) try: response = json.loads(file.read()) finally: file.close() json.dump(response, s, sort_keys=True, indent=1) #pprint.pprint(response, s) hout=s.getvalue() aaa="<h2>permissions delete - ok </h2><pre>%s</pre>" % (hout) elif not perm_mp or not perm_pa: aaa="<h2>wall post - error - registruodamiesi nepatvirtinot: %s </h2><br><pre>%s</pre>" % (permtext,hout) else: if cmd =="delete": # mess = graph.delete_object(oid) # mess = graph.request(oid, post_args={"method": "delete"}) post_args=dict(method = "delete", access_token = fb2_current_user.access_token) post_data = urllib.urlencode(post_args) file = urllib.urlopen("https://graph.facebook.com/" + oid , post_data) try: response = json.loads(file.read()) finally: file.close() #pprint.pprint(response, s) json.dump(response, s, sort_keys=True, indent=1) hout=s.getvalue() aaa="<h2>wall post delete - ok </h2><pre>%s</pre>" % (hout) elif cmd =="info": mess = graph.request("/me/accounts") #pprint.pprint(mess, s) json.dump(mess, s, sort_keys=True, indent=1) hout=s.getvalue() aaa="<h2>accounts info</h2><p><pre>%s</pre></p>" % (hout) else: mess = graph.put_wall_post(message, attachment=attachment) #pprint.pprint(mess, s) json.dump(mess, s, sort_keys=True, indent=1) hout=s.getvalue() aaa="<h2>wall post - ok </h2><p><a href=\"/fbputwall?cmd=delete&oid=%s\">delete</a></p><pre>%s</pre>" % (mess["id"],hout) except Exception, e: errorjson = True errtext = cgi.escape(str(sys.exc_info()[0])) + ' ' + cgi.escape(str(sys.exc_info()[1])) + ' ' + cgi.escape(str(sys.exc_info()[2])) else: aaa="<h2>wall post</h2>not loged - <a href=\"/auth/login?continue=%s\">login</a>" % (urllib.quote(req.request.uri))