Exemple #1
0
def validate(access_token):
    """Validate a Facebook access token and return the associated local user
    ID, creating a new user record if necessary.
    """

    user_id = AccessTokenCache.get(access_token)

    # If the access token isn't in the cache then validate it with Facebook
    # and retrieve its metadata. Extract the user ID from the metadata and
    # query Facebook again for the user metadata. If a record for the user
    # already exists update it with the latest metadata, otherwise create a new
    # record. Finally cache the access token until Facebook expires it.
    #
    # If new user metadata is being retrieved it may contain a different list
    # of friends to those already cached for the user, so clear the user's
    # friends cache.
    if user_id is None:
        access_token_meta = yield FacebookGraphAPI.debug_token(access_token)
        fb_user_id = access_token_meta["user_id"]
        user_meta = yield FacebookGraphAPI.get_user(fb_user_id, access_token)
        user_id = yield \
            _create_or_update_user_record(fb_user_id, user_meta, access_token)
        FriendsCache.clear(user_id)
        AccessTokenCache.put(access_token, user_id,
                             access_token_meta["expires_at"])

    raise tornado.gen.Return(user_id)
Exemple #2
0
def validate(access_token):
    """Validate a Facebook access token and return the associated local user
    ID, creating a new user record if necessary.
    """

    user_id = AccessTokenCache.get(access_token)

    # If the access token isn't in the cache then validate it with Facebook
    # and retrieve its metadata. Extract the user ID from the metadata and 
    # query Facebook again for the user metadata. If a record for the user
    # already exists update it with the latest metadata, otherwise create a new
    # record. Finally cache the access token until Facebook expires it.
    #
    # If new user metadata is being retrieved it may contain a different list
    # of friends to those already cached for the user, so clear the user's
    # friends cache.
    if user_id is None:
        access_token_meta = yield FacebookGraphAPI.debug_token(access_token)
        fb_user_id = access_token_meta["user_id"]
        user_meta = yield FacebookGraphAPI.get_user(fb_user_id, access_token)
        user_id = yield \
            _create_or_update_user_record(fb_user_id, user_meta, access_token)
        FriendsCache.clear(user_id)
        AccessTokenCache.put(
            access_token, user_id, access_token_meta["expires_at"])

    raise tornado.gen.Return(user_id)