本文整理汇总了Python中open_facebook.FacebookAuthorization.parse_signed_data方法的典型用法代码示例。如果您正苦于以下问题:Python FacebookAuthorization.parse_signed_data方法的具体用法?Python FacebookAuthorization.parse_signed_data怎么用?Python FacebookAuthorization.parse_signed_data使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类open_facebook.FacebookAuthorization
的用法示例。
在下文中一共展示了FacebookAuthorization.parse_signed_data方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _get_access_token_from_signed_request
# 需要导入模块: from open_facebook import FacebookAuthorization [as 别名]
# 或者: from open_facebook.FacebookAuthorization import parse_signed_data [as 别名]
def _get_access_token_from_signed_request(request, redirect_uri=None):
"""Try to retrieve an access_token from a signed request"""
## Check whether we got any signed request
_signed_data = None
signed_data = request.REQUEST.get('signed_request')
if signed_data:
logger.info('Got signed data from Facebook')
_signed_data = signed_data
else:
cookie_name = 'fbsr_%s' % facebook_settings.FACEBOOK_APP_ID
cookie_data = request.COOKIES.get(cookie_name)
if cookie_data:
logger.info('Got signed cookie from Facebook')
_signed_data = cookie_data
if _signed_data:
parsed_data = FacebookAuthorization.parse_signed_data(signed_data)
if parsed_data:
logger.debug('Parsing of signed data was successful')
## Parsed data can fail because of signing issues
if 'oauth_token' in parsed_data:
logger.info('The signed data contains a valid oauth_token')
## We already have an active access token in the data
access_token = parsed_data['oauth_token']
return dict(access_token=access_token)
else:
logger.info('Got code from parsed data')
## no access token, need to use this code to get one
code = parsed_data.get('code', None)
return _exchange_oauth_code_for_access_token(code, redirect_uri)
示例2: get_facebook_graph
# 需要导入模块: from open_facebook import FacebookAuthorization [as 别名]
# 或者: from open_facebook.FacebookAuthorization import parse_signed_data [as 别名]
def get_facebook_graph(request=None, access_token=None, redirect_uri=None, raise_=False):
"""
given a request from one of these
- js authentication flow (signed cookie)
- facebook app authentication flow (signed cookie)
- facebook oauth redirect (code param in url)
- mobile authentication flow (direct access_token)
- offline access token stored in user profile
returns a graph object
redirect path is the path from which you requested the token
for some reason facebook needs exactly this uri when converting the code
to a token
falls back to the current page without code in the request params
specify redirect_uri if you are not posting and recieving the code
on the same page
"""
# this is not a production flow, but very handy for testing
if not access_token and request.REQUEST.get("access_token"):
access_token = request.REQUEST["access_token"]
# should drop query params be included in the open facebook api,
# maybe, weird this...
from open_facebook import OpenFacebook, FacebookAuthorization
from django.core.cache import cache
parsed_data = None
expires = None
if hasattr(request, "facebook"):
graph = request.facebook
_add_current_user_id(graph, request.user)
return graph
if not access_token:
# easy case, code is in the get
code = request.REQUEST.get("code")
if code:
logger.info("Got code from the request data")
if not code:
# signed request or cookie leading, base 64 decoding needed
signed_data = request.REQUEST.get("signed_request")
cookie_name = "fbsr_%s" % facebook_settings.FACEBOOK_APP_ID
cookie_data = request.COOKIES.get(cookie_name)
if cookie_data:
signed_data = cookie_data
# the javascript api assumes a redirect uri of ''
redirect_uri = ""
if signed_data:
logger.info("Got signed data from facebook")
parsed_data = FacebookAuthorization.parse_signed_data(signed_data)
if parsed_data:
logger.info("Got parsed data from facebook")
# parsed data can fail because of signing issues
if "oauth_token" in parsed_data:
logger.info("Got access_token from parsed data")
# we already have an active access token in the data
access_token = parsed_data["oauth_token"]
else:
logger.info("Got code from parsed data")
# no access token, need to use this code to get one
code = parsed_data.get("code", None)
if not access_token:
if code:
cache_key = "convert_code_%s" % code
access_token = cache.get(cache_key)
if not access_token:
# exchange the code for an access token
# based on the php api
# https://github.com/facebook/php-sdk/blob/master/src/base_facebook.php
# create a default for the redirect_uri
# when using the javascript sdk the default
# should be '' an empty string
# for other pages it should be the url
if not redirect_uri:
redirect_uri = ""
# we need to drop signed_request, code and state
redirect_uri = cleanup_oauth_url(redirect_uri)
try:
logger.info("trying to convert the code with redirect uri: %s", redirect_uri)
# This is realy slow, that's why it's cached
token_response = FacebookAuthorization.convert_code(code, redirect_uri=redirect_uri)
expires = token_response.get("expires")
access_token = token_response["access_token"]
# would use cookies instead, but django's cookie setting
# is a bit of a mess
cache.set(cache_key, access_token, 60 * 60 * 2)
except open_facebook_exceptions.OAuthException, e:
# this sometimes fails, but it shouldnt raise because
# it happens when users remove your
# permissions and then try to reauthenticate
logger.warn("Error when trying to convert code %s", unicode(e))
if raise_:
raise
else:
return None
elif request.user.is_authenticated():
#.........这里部分代码省略.........
示例3: get_facebook_graph
# 需要导入模块: from open_facebook import FacebookAuthorization [as 别名]
# 或者: from open_facebook.FacebookAuthorization import parse_signed_data [as 别名]
def get_facebook_graph(request=None, access_token=None, redirect_uri=None):
"""
given a request from one of these
- js authentication flow (signed cookie)
- facebook app authentication flow (signed cookie)
- facebook oauth redirect (code param in url)
- mobile authentication flow (direct access_token)
returns a graph object
redirect path is the path from which you requested the token
for some reason facebook needs exactly this uri when converting the code
to a token
falls back to the current page without code in the request params
specify redirect_uri if you are not posting and recieving the code on the same page
"""
# should drop query params be included in the open facebook api, maybe, weird this...
DROP_QUERY_PARAMS = ["code", "signed_request", "state"]
from open_facebook import OpenFacebook, FacebookAuthorization
parsed_data = None
if not access_token:
# easy case, code is in the get
code = request.REQUEST.get("code")
if not code:
# signed request or cookie leading, base 64 decoding needed
signed_data = request.REQUEST.get("signed_request")
cookie_name = "fbsr_%s" % facebook_settings.FACEBOOK_APP_ID
cookie_data = request.COOKIES.get(cookie_name)
if cookie_data:
signed_data = cookie_data
# the javascript api assumes a redirect uri of ''
redirect_uri = ""
if signed_data:
parsed_data = FacebookAuthorization.parse_signed_data(signed_data)
if "oauth_token" in parsed_data:
# we already have an active access token in the data
access_token = parsed_data["oauth_token"]
else:
# no access token, need to use this code to get one
code = parsed_data["code"]
if not access_token:
if code:
# exchange the code for an access token
# based on the php api
# https://github.com/facebook/php-sdk/blob/master/src/base_facebook.php
# we need to drop signed_request, code and state
if redirect_uri is None:
query_dict_items = [(k, v) for k, v in request.GET.items() if k not in DROP_QUERY_PARAMS]
new_query_dict = QueryDict("", True)
new_query_dict.update(dict(query_dict_items))
# TODO support http and https
redirect_uri = "http://" + request.META["HTTP_HOST"] + request.path
if new_query_dict:
redirect_uri += "?%s" % new_query_dict.urlencode()
token_response = FacebookAuthorization.convert_code(code, redirect_uri=redirect_uri)
access_token = token_response["access_token"]
else:
from open_facebook import exceptions
return None
# raise exceptions.MissingParameter('Cant find code or access token')
facebook_open_graph = OpenFacebook(access_token, parsed_data)
return facebook_open_graph
示例4: get_facebook_graph
# 需要导入模块: from open_facebook import FacebookAuthorization [as 别名]
# 或者: from open_facebook.FacebookAuthorization import parse_signed_data [as 别名]
def get_facebook_graph(request=None, access_token=None, redirect_uri=None):
'''
given a request from one of these
- js authentication flow (signed cookie)
- facebook app authentication flow (signed cookie)
- facebook oauth redirect (code param in url)
- mobile authentication flow (direct access_token)
- offline access token stored in user profile
returns a graph object
redirect path is the path from which you requested the token
for some reason facebook needs exactly this uri when converting the code
to a token
falls back to the current page without code in the request params
specify redirect_uri if you are not posting and recieving the code on the same page
'''
#should drop query params be included in the open facebook api, maybe, weird this...
from open_facebook import OpenFacebook, FacebookAuthorization
parsed_data = None
expires = None
if hasattr(request, 'facebook'):
graph = request.facebook
_add_current_user_id(graph, request.user)
return graph
if not access_token:
#easy case, code is in the get
code = request.REQUEST.get('code')
if not code:
#signed request or cookie leading, base 64 decoding needed
signed_data = request.REQUEST.get('signed_request')
cookie_name = 'fbsr_%s' % facebook_settings.FACEBOOK_APP_ID
cookie_data = request.COOKIES.get(cookie_name)
if cookie_data:
signed_data = cookie_data
#the javascript api assumes a redirect uri of ''
redirect_uri = ''
if signed_data:
parsed_data = FacebookAuthorization.parse_signed_data(signed_data)
if 'oauth_token' in parsed_data:
# we already have an active access token in the data
access_token = parsed_data['oauth_token']
else:
# no access token, need to use this code to get one
code = parsed_data.get('code', None)
if not access_token:
if code:
#exchange the code for an access token
#based on the php api
#https://github.com/facebook/php-sdk/blob/master/src/base_facebook.php
#create a default for the redirect_uri
#when using the javascript sdk the default should be '' an empty string
if not redirect_uri:
redirect_uri = ''
#we need to drop signed_request, code and state
redirect_uri = cleanup_oauth_url(redirect_uri)
try:
logger.info('trying to convert the code with redirect uri: %s', redirect_uri)
token_response = FacebookAuthorization.convert_code(code, redirect_uri=redirect_uri)
expires = token_response.get('expires')
access_token = token_response['access_token']
except open_facebook_exceptions.OAuthException, e:
#this sometimes fails, but it shouldnt raise because it happens when users remove your
#permissions and then try to reauthenticate
logger.warn('Error when trying to convert code %s', unicode(e))
return None
elif request.user.is_authenticated():
#support for offline access tokens stored in the users profile
profile = request.user.get_profile()
access_token = getattr(profile, 'access_token', None)
if not access_token:
return None
else:
return None
示例5: get_facebook_graph
# 需要导入模块: from open_facebook import FacebookAuthorization [as 别名]
# 或者: from open_facebook.FacebookAuthorization import parse_signed_data [as 别名]
def get_facebook_graph(request=None, access_token=None, redirect_uri=None):
'''
given a request from one of these
- js authentication flow (signed cookie)
- facebook app authentication flow (signed cookie)
- facebook oauth redirect (code param in url)
- mobile authentication flow (direct access_token)
returns a graph object
redirect path is the path from which you requested the token
for some reason facebook needs exactly this uri when converting the code
to a token
falls back to the current page without code in the request params
specify redirect_uri if you are not posting and recieving the code on the same page
'''
#should drop query params be included in the open facebook api, maybe, weird this...
DROP_QUERY_PARAMS = ['code','signed_request','state']
from open_facebook import OpenFacebook, FacebookAuthorization
parsed_data = None
if not access_token:
#easy case, code is in the get
code = request.REQUEST.get('code')
if not code:
#signed request or cookie leading, base 64 decoding needed
signed_data = request.REQUEST.get('signed_request')
cookie_name = 'fbsr_%s' % facebook_settings.FACEBOOK_APP_ID
cookie_data = request.COOKIES.get(cookie_name)
if cookie_data:
signed_data = cookie_data
#the javascript api assumes a redirect uri of ''
redirect_uri = ''
if signed_data:
parsed_data = FacebookAuthorization.parse_signed_data(signed_data)
if 'oauth_token' in parsed_data:
# we already have an active access token in the data
access_token = parsed_data['oauth_token']
else:
# no access token, need to use this code to get one
code = parsed_data.get('code', None)
if not access_token:
if code:
#exchange the code for an access token
#based on the php api
#https://github.com/facebook/php-sdk/blob/master/src/base_facebook.php
#we need to drop signed_request, code and state
if redirect_uri is None:
query_dict_items = [(k,v) for k, v in request.GET.items() if k not in DROP_QUERY_PARAMS]
new_query_dict = QueryDict('', True)
new_query_dict.update(dict(query_dict_items))
#TODO support http and https
# redirect_uri = 'http://' + request.META['HTTP_HOST'] + request.path
redirect_uri = facebook_settings.FACEBOOK_CANVAS_PAGE
if new_query_dict:
redirect_uri += '?%s' % new_query_dict.urlencode()
try:
token_response = FacebookAuthorization.convert_code(code, redirect_uri=redirect_uri)
except open_facebook_exceptions.OAuthException, e:
return None
access_token = token_response['access_token']
else:
return None
示例6: get_facebook_graph
# 需要导入模块: from open_facebook import FacebookAuthorization [as 别名]
# 或者: from open_facebook.FacebookAuthorization import parse_signed_data [as 别名]
def get_facebook_graph(request=None, access_token=None, redirect_uri=None):
'''
given a request from one of these
- js authentication flow (signed cookie)
- facebook app authentication flow (signed cookie)
- facebook oauth redirect (code param in url)
- mobile authentication flow (direct access_token)
- offline access token stored in user profile
returns a graph object
redirect path is the path from which you requested the token
for some reason facebook needs exactly this uri when converting the code
to a token
falls back to the current page without code in the request params
specify redirect_uri if you are not posting and recieving the code on the same page
'''
#should drop query params be included in the open facebook api, maybe, weird this...
DROP_QUERY_PARAMS = ['code', 'signed_request', 'state']
from open_facebook import OpenFacebook, FacebookAuthorization
parsed_data = None
expires = None
if not access_token:
#easy case, code is in the get
code = request.REQUEST.get('code')
if not code:
#signed request or cookie leading, base 64 decoding needed
signed_data = request.REQUEST.get('signed_request')
cookie_name = 'fbsr_%s' % facebook_settings.FACEBOOK_APP_ID
cookie_data = request.COOKIES.get(cookie_name)
if cookie_data:
signed_data = cookie_data
#the javascript api assumes a redirect uri of ''
redirect_uri = ''
if signed_data:
parsed_data = FacebookAuthorization.parse_signed_data(signed_data)
if 'oauth_token' in parsed_data:
# we already have an active access token in the data
access_token = parsed_data['oauth_token']
else:
# no access token, need to use this code to get one
code = parsed_data.get('code', None)
if not access_token:
if code:
#exchange the code for an access token
#based on the php api
#https://github.com/facebook/php-sdk/blob/master/src/base_facebook.php
#we need to drop signed_request, code and state
if redirect_uri:
redirect_base, redirect_query = redirect_uri.split('?', 1)
query_dict_items = QueryDict(redirect_query).items()
else:
redirect_base = facebook_settings.FACEBOOK_CANVAS_PAGE
query_dict_items = request.GET.items()
filtered_query_items = [(k, v) for k, v in query_dict_items if k.lower() not in DROP_QUERY_PARAMS]
new_query_dict = QueryDict('', True)
new_query_dict.update(dict(filtered_query_items))
#TODO support http and https
redirect_uri = redirect_base
if new_query_dict:
redirect_uri = '%s?%s' % (redirect_base, new_query_dict.urlencode())
try:
logger.info('trying to convert the code with redirect uri: %s', redirect_uri)
token_response = FacebookAuthorization.convert_code(code, redirect_uri=redirect_uri)
expires = token_response.get('expires')
except open_facebook_exceptions.OAuthException, e:
#TODO: this sometimes fails, should it raise?
raise
return None
access_token = token_response['access_token']
elif request.user.is_authenticated():
#support for offline access tokens stored in the users profile
profile = request.user.get_profile()
access_token = getattr(profile, 'access_token', None)
if not access_token:
return None
else:
return None