Example #1
0
    def get(self, mode=""):
        google_user = users.get_current_user()

        if google_user is None:
            self.redirect(users.create_login_url('/login'))

        # last_access_timeを記録
        user = User.gql("WHERE user_id=:1 and service=:2",
                        google_user.user_id(), "google").get()
        if user is None:
            user = User(user_id=google_user.user_id(),
                        name=google_user.nickname(),
                        mail=google_user.email(),
                        service='google',
                        access_token=None,
                        google_user=google_user,
                        post_key='control',
                        last_access_time=datetime.datetime.now())
            user.put()

        session = Session(self.request, self.response)
        session.new(user)
        if mode == 'mlogin':
            self.redirect('/mhome?xoauth_requestor_id=' + user.user_id)
            return
        self.redirect('/')
Example #2
0
    def get(self, mode=""):
        google_user = users.get_current_user()

        if google_user is None:
            self.redirect(users.create_login_url("/login"))

        # last_access_timeを記録
        user = User.gql("WHERE user_id=:1 and service=:2", google_user.user_id(), "google").get()
        if user is None:
            user = User(
                user_id=google_user.user_id(),
                name=google_user.nickname(),
                mail=google_user.email(),
                service="google",
                access_token=None,
                google_user=google_user,
                post_key="control",
                last_access_time=datetime.datetime.now(),
            )
            user.put()

        session = Session(self.request, self.response)
        session.new(user)
        if mode == "mlogin":
            self.redirect("/mhome?xoauth_requestor_id=" + user.user_id)
            return
        self.redirect("/")
Example #3
0
    def get(self, action="", account="", param=""):
        verification_code = self.request.get("code")
        args = dict(client_id=settings.FACEBOOK_APP_ID,
                    redirect_uri=self.request.path_url)
        if verification_code:
            args["client_secret"] = settings.FACEBOOK_APP_SECRET
            args["code"] = verification_code
            response = cgi.parse_qs(
                urllib.urlopen(
                    "https://graph.facebook.com/oauth/access_token?" +
                    urllib.urlencode(args)).read())
            access_token = response["access_token"][-1]

            profile_res = unicode(
                urllib.urlopen("https://graph.facebook.com/me?" +
                               urllib.urlencode(dict(
                                   access_token=access_token))).read(),
                'utf-8')
            profile = simplejson.loads(profile_res)

            user = None
            is_not_login = False
            try:
                self.check_login()
                user = self.session.get_user()
            except utils.NotLoginError:
                is_not_login = True
                user = User.gql("WHERE user_id=:1 and service=:2",
                                str(profile["id"]), "facebook").get()
                if user is None:
                    user = User(user_id=str(profile["id"]),
                                name=str(profile["name"]),
                                mail=str(profile["email"]),
                                service='facebook',
                                access_token=access_token,
                                post_key='control',
                                last_access_time=datetime.datetime.now())
                    user.put()
                session = Session(self.request, self.response)
                session.new(user)

            account = Account(user_ref=user,
                              service="facebook",
                              account_name=str(profile["id"]),
                              display_name=str(profile["name"]),
                              account_info=profile_res,
                              scope=FACEBOOK_SCOPE,
                              access_token=access_token)
            account.put()

            #既に同じアカウントが登録されていたら削除します
            saved_accounts = Account.gql(
                "WHERE service = :1 and user_ref = :2 and account_name = :3",
                "facebook", user.key(), account.account_name)
            for saved_account in saved_accounts:
                if saved_account.key() != account.key():
                    saved_account.delete()

            if is_not_login:
                if action == 'mlogin':
                    self.redirect('/mhome?xoauth_requestor_id=' + user.user_id)
                    return
                self.redirect('/')
                return

            #ウィンドウを閉じます
            tmpl = os.path.join(os.path.dirname(__file__),
                                "../view/oauth_callback.html")
            return self.response.out.write(
                template.render(tmpl, {'account': account}))
        else:
            args["scope"] = FACEBOOK_SCOPE
            if action == 'oauth':
                args["display"] = 'popup'
            self.redirect("https://www.facebook.com/dialog/oauth?" +
                          #"https://graph.facebook.com/oauth/authorize?" +
                          urllib.urlencode(args))
            return
Example #4
0
 def get(self, action="", account="", param=""):
     verification_code = self.request.get("code")
     args = dict(client_id=settings.FACEBOOK_APP_ID, redirect_uri=self.request.path_url)
     if verification_code:
         args["client_secret"] = settings.FACEBOOK_APP_SECRET
         args["code"] = verification_code
         response = cgi.parse_qs(urllib.urlopen(
             "https://graph.facebook.com/oauth/access_token?" +
             urllib.urlencode(args)).read())
         access_token = response["access_token"][-1]
         
         profile_res = unicode(urllib.urlopen(
             "https://graph.facebook.com/me?" +
             urllib.urlencode(dict(access_token=access_token))).read(),'utf-8')
         profile = simplejson.loads(profile_res)
         
         user = None
         is_not_login = False
         try:
             self.check_login()
             user = self.session.get_user()
         except utils.NotLoginError:
             is_not_login = True
             user = User.gql("WHERE user_id=:1 and service=:2", str(profile["id"]), "facebook").get()
             if user is None:
                 user = User(
                     user_id=str(profile["id"]),
                     name=str(profile["name"]),
                     mail=str(profile["email"]),
                     service='facebook',
                     access_token=access_token,
                     post_key='control',
                     last_access_time=datetime.datetime.now()
                 )
                 user.put()
             session = Session(self.request, self.response)
             session.new(user)
         
         account = Account(
             user_ref=user,
             service="facebook",
             account_name=str(profile["id"]),
             display_name=str(profile["name"]),
             account_info=profile_res,
             scope=FACEBOOK_SCOPE,
             access_token=access_token
             )
         account.put()
         
         #既に同じアカウントが登録されていたら削除します
         saved_accounts = Account.gql(
             "WHERE service = :1 and user_ref = :2 and account_name = :3",
             "facebook",
             user.key(),
             account.account_name)
         for saved_account in saved_accounts:
             if saved_account.key() != account.key():
                 saved_account.delete()
         
         if is_not_login:
             if action == 'mlogin':
                 self.redirect('/mhome?xoauth_requestor_id='+user.user_id)
                 return
             self.redirect('/')
             return
         
         #ウィンドウを閉じます
         tmpl = os.path.join(os.path.dirname(__file__), "../view/oauth_callback.html")
         return self.response.out.write(template.render(tmpl, {'account':account}))
     else:
         args["scope"] = FACEBOOK_SCOPE
         if action == 'oauth':
             args["display"] = 'popup'
         self.redirect(
             "https://www.facebook.com/dialog/oauth?" +
             #"https://graph.facebook.com/oauth/authorize?" +
             urllib.urlencode(args))
         return