def test_graph_api(request): from settings import FACEBOOK_APP_ID, FACEBOOK_SECRET import cgi, urllib, urllib2 import simplejson as json from tests import FACEBOOK_UID import facebook import utils auth = facebook.get_user_from_cookie(request.cookies, FACEBOOK_APP_ID, FACEBOOK_SECRET) access_token = auth['access_token'] #graph = facebook.GraphAPI(access_token) #user = graph.get_object("me") #logging.debug(user) #logging.debug(access_token) #graph.put_object("me", 'photos', image = urllib.urlopen('http://www.8squirrels.com/media/images/img1.jpg').read()) #params = urllib.urlencode({'file': urllib.urlopen('http://www.8squirrels.com/media/images/img1.jpg').read()}) #datagen, headers = multipart_encode(params) #request = urllib2.Request("https://graph.facebook.com/me/photos", params) #resp = urllib.urlopen('https://graph.facebook.com/me/photos', params) #logging.debug(request) #logging.debug(dir(request)) #logging.debug(request.headers) #image_content = urllib.urlopen('http://www.8squirrels.com/media/images/img1.jpg').read() if request.method == "POST": out = utils.posturl('https://graph.facebook.com/me/photos', [('access_token', request.form['access_token'])], [('myfile', 'myimage.jpg', request.files['file'].stream.read())]) post_url = "https://graph.facebook.com/me/photos" return render_to_response('fb/index.html', {'post_url': post_url, 'access_token': access_token})
def post(self): import_str = str(self.request.get("import_file")) imgs = mul_img(import_str)#img_split(import_str) user = facebook.get_user_from_cookie(self.request.cookies, id, secret) if user: graph = facebook.GraphAPI(user["access_token"]) # logging.error(graph.multipart_request("me/photos", post_args={'message': 'test'}, files={'source': import_str})) #graph.put_wall_post('test') for i in imgs: logging.error(i) out = utils.posturl('https://graph.facebook.com/me/photos', [('access_token', user['access_token'])], [('myfile', 'myimage.jpg', i)])#request.files['import_file'].stream.read() # graph.put_event(name='test', start_time='1272718027', location='someplace') self.response.out.write(self.request.cookies)
def upload_photo(self, profile_id, arquivo_raw): import facebook, utils #out = utils.posturl( # 'https://graph.facebook.com/%s/photos' % config.FACEBOOK_PAGE_ID, # [("access_token", config.FACEBOOK_PAGE_SECRET), ("source", "@")], # [("myfile", "cover.jpg", arquivo_raw)] #) out = utils.posturl( 'https://graph.facebook.com/%s/photos' % profile_id, [("access_token", self.access_token)], [("file", "arquivo", arquivo_raw)]) try: response = _parse_json(out) finally: logging.info(out) if response.get("error"): raise GraphAPIError(response["error"]["type"], response["error"]["message"]) return response
def upload_photo(self, profile_id, arquivo_raw): import facebook, utils #out = utils.posturl( # 'https://graph.facebook.com/%s/photos' % config.FACEBOOK_PAGE_ID, # [("access_token", config.FACEBOOK_PAGE_SECRET), ("source", "@")], # [("myfile", "cover.jpg", arquivo_raw)] #) out = utils.posturl( 'https://graph.facebook.com/%s/photos' % profile_id, [("access_token", self.access_token)], [("file", "arquivo", arquivo_raw)] ) try: response = _parse_json(out) finally: logging.info(out) if response.get("error"): raise GraphAPIError(response["error"]["type"], response["error"]["message"]) return response