How to redirect the url after logging into Facebook?

前端 未结 2 1876
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-07 03:56

I have created a django application with a Facebook connect to the application. Now when i click on the Facebook login button, the oauth page pop ups. When i give the userna

2条回答
  •  说谎
    说谎 (楼主)
    2021-01-07 04:21

    I suppose you are using the JS Facebook library if you say that the oauth just pop ups. I would go the simpler oauth redirect way, using a the following :

    def build_authentication_redirect(self):
                args = {}
                args["client_id"]=self.app_id
                args["redirect_uri"]=self.redirect_uri
                args["scope"]=",".join(self.req_perms)
                redirect_url = "https://www.facebook.com/dialog/oauth?"+urllib.urlencode(args)
                redirect_code = """
                        
                """ % redirect_url;
                return HttpResponse(redirect_code,mimetype="text/html")
    

    Where self.app_id is your facebook app id. Where self.redirect_uri is the url the user will be redirected after login. Where self.scope is built from self.req_perms which is an array of the required permissions.

    After that they user will be redirected to redirect_uri with the access token in post 'signed_request' parameter, you can use the following function to decode it :

    def load_signed_request(self, signed_request):
                """Load the user state from a signed_request value"""
                sig, payload = signed_request.split(u'.', 1)
                sig = self.base64_url_decode(sig)
                data = json.loads(self.base64_url_decode(payload))
    
                expected_sig = hmac.new(self.app_secret, msg=payload, digestmod=hashlib.sha256).digest()
    
                # allow the signed_request to function for upto 1 day
                if sig == expected_sig and data[u'issued_at'] > (time.time() - 86400):
                        return data.get(u'user_id'), data.get(u'oauth_token')
                else:
                        return None,None
    

    Example :

    sigreq =request.POST.get('signed_request', None)
    user_id,access_token = load_signed_request(sigreq)
    

    Again you will need self.app_secret and the following function :

    @staticmethod
        def base64_url_decode(data):
                data = data.encode(u'ascii')
                data += '=' * (4 - (len(data) % 4))
                return base64.urlsafe_b64decode(data)
    

    I had a lot of trouble with FB JS login and auth, especially on different browsers, this way is the more robust way I could find :)

    Btw If you want my facebook.py file I can put it available online somewhere ... It even has a @fbsig_required and @fbsig_redirect view decorators ...

    Oh Also here are my imports :

    import cgi
    import hashlib
    import time
    import urllib
    import base64
    import datetime
    import hmac
    
    from django.conf import settings
    from django.http import HttpResponseRedirect,HttpResponse,HttpResponseNotFound
    

提交回复
热议问题