def get_tokens(screen_name):
    app_key = dal.get_token("twitter app key " + screen_name)
    app_secret = dal.get_token("twitter app secret " + screen_name)

    final_key = dal.get_token("twitter final key " + screen_name)
    final_secret = dal.get_token("twitter final secret " + screen_name)

    if not final_key or not final_secret:
        twitter = Twython(app_key, app_secret)
        auth = twitter.get_authentication_tokens()
        oauth_token = auth["oauth_token"]
        oauth_token_secret = auth["oauth_token_secret"]
        url = auth["auth_url"]

        logger.info(url)
        if myhardware.is_andrew_desktop or myhardware.is_andrew_macbook:
            webbrowser.open(url)
        oauth_verifier = input("Please enter your pin:")

        twitter = Twython(app_key, app_secret, oauth_token, oauth_token_secret)
        final_step = twitter.get_authorized_tokens(oauth_verifier)
        final_key = final_step["oauth_token"]
        final_secret = final_step["oauth_token_secret"]

        dal.set_token("twitter final key " + screen_name, final_key)
        dal.set_token("twitter final secret " + screen_name, final_secret)

    tokens = [app_key, app_secret, final_key, final_secret]

    return tokens
def authenticate():
    token_dir = "temp" + os.sep + "tokens" + os.sep

    if not os.path.exists(token_dir):
        os.makedirs(token_dir)

    app_key_path = token_dir + "APP_KEY.pkl"
    app_secret_path = token_dir + "APP_SECRET.pkl"
    final_oauth_token_path = token_dir + "FINAL_OAUTH_TOKEN.pkl"
    final_oauth_token_secret_path = token_dir + "FINAL_OAUTH_TOKEN_SECRET.pkl"

    exists = os.path.isfile(app_key_path) and os.path.isfile(app_secret_path)

    if exists:
        app_key = pickle.load(open(app_key_path, "rb"))
        app_secret = pickle.load(open(app_secret_path, "rb"))
    else:
        app_key = raw_input("Enter your APP_KEY:")
        app_secret = raw_input("Enter your APP_SECRET:")

        pickle.dump(app_key, open(app_key_path, "wb"))
        pickle.dump(app_secret, open(app_secret_path, "wb"))

    exists = os.path.isfile(final_oauth_token_path) and os.path.isfile(
        final_oauth_token_secret_path)

    if exists:

        final_oauth_token = pickle.load(open(final_oauth_token_path, "rb"))
        final_oauth_token_secret = pickle.load(
            open(final_oauth_token_secret_path, "rb"))

    else:

        t = Twython(app_key, app_secret)

        auth = t.get_authentication_tokens()

        oauth_token = auth["oauth_token"]
        oauth_token_secret = auth["oauth_token_secret"]

        url = auth["auth_url"]
        webbrowser.open(url)

        oauth_verifier = raw_input("Enter your pin:")

        t = Twython(app_key, app_secret, oauth_token, oauth_token_secret)

        final_step = t.get_authorized_tokens(oauth_verifier)

        final_oauth_token = final_step["oauth_token"]
        final_oauth_token_secret = final_step["oauth_token_secret"]

        pickle.dump(final_oauth_token, open(final_oauth_token_path, "wb"))
        pickle.dump(final_oauth_token_secret,
                    open(final_oauth_token_secret_path, "wb"))

    return [app_key, app_secret, final_oauth_token, final_oauth_token_secret]
示例#3
0
def twitter_connect(request, apk):
    
    # Twython Library https://github.com/ryanmcgrath/twython  
    twitter = Twython(settings.TWITTER_API_KEY, settings.TWITTER_API_SECRET)
    auth = twitter.get_authentication_tokens(callback_url=request.build_absolute_uri(reverse('twitter_callback', args=[apk])))
    
    request.session['TEMP_OAUTH_TOKEN'] = auth['oauth_token']
    request.session['TEMP_OAUTH_TOKEN_SECRET'] = auth['oauth_token_secret']
    
    return HttpResponseRedirect(auth['auth_url'])
def authenticate():
    token_dir = "temp" + os.sep + "tokens" + os.sep

    if not os.path.exists(token_dir):
        os.makedirs(token_dir)

    app_key_path = token_dir + "APP_KEY.pkl"
    app_secret_path = token_dir + "APP_SECRET.pkl"
    final_oauth_token_path = token_dir + "FINAL_OAUTH_TOKEN.pkl"
    final_oauth_token_secret_path = token_dir + "FINAL_OAUTH_TOKEN_SECRET.pkl"

    exists = os.path.isfile(app_key_path) and os.path.isfile(app_secret_path)

    if exists:
        app_key = pickle.load(open(app_key_path, "rb"))
        app_secret = pickle.load(open(app_secret_path, "rb"))
    else:
        app_key = raw_input("Enter your APP_KEY:")
        app_secret = raw_input("Enter your APP_SECRET:")

        pickle.dump(app_key, open(app_key_path, "wb"))
        pickle.dump(app_secret, open(app_secret_path, "wb"))

    exists = os.path.isfile(final_oauth_token_path) and os.path.isfile(final_oauth_token_secret_path)

    if exists:

        final_oauth_token = pickle.load(open(final_oauth_token_path, "rb"))
        final_oauth_token_secret = pickle.load(open(final_oauth_token_secret_path, "rb"))

    else:

        t = Twython(app_key, app_secret)

        auth = t.get_authentication_tokens()

        oauth_token = auth["oauth_token"]
        oauth_token_secret = auth["oauth_token_secret"]

        url = auth["auth_url"]
        webbrowser.open(url)

        oauth_verifier = raw_input("Enter your pin:")

        t = Twython(app_key, app_secret, oauth_token, oauth_token_secret)

        final_step = t.get_authorized_tokens(oauth_verifier)

        final_oauth_token = final_step["oauth_token"]
        final_oauth_token_secret = final_step["oauth_token_secret"]

        pickle.dump(final_oauth_token, open(final_oauth_token_path, "wb"))
        pickle.dump(final_oauth_token_secret, open(final_oauth_token_secret_path, "wb"))

    return [app_key, app_secret, final_oauth_token, final_oauth_token_secret]
示例#5
0
 def run(self, args):
     # configure the server port for the callback
     serverPort = 8000
     
     # initiate OAuth workflow
     twitter = Twython(args.twitter_app_key, args.twitter_app_secret)
     # step 1
     self._log.info("Initiating Twitter OAuth workflow.")
     oAuth = twitter.get_authentication_tokens(callback_url="http://localhost:%d/callback" % (serverPort))
     oAuthToken = oAuth['oauth_token']
     oAuthTokenSecret = oAuth['oauth_token_secret']
     # step 2
     webbrowser.open_new(oAuth['auth_url'])
     # step 3
     self._log.info("Starting OAuth HTTP callback handler.")
     httpd = OAuthCallbackServer(('', serverPort), self._log, Twython(args.twitter_app_key, args.twitter_app_secret, oAuthToken, oAuthTokenSecret))
     httpd.handle_request()
示例#6
0
class Twitter:
    def __init__(self, consumer_token, consumer_secret):
        self.t = Twython(consumer_token, consumer_secret)
        self.consumer_token = consumer_token
        self.consumer_secret = consumer_secret

    def generate_access_token(self):
        auth = self.t.get_authentication_tokens()

        print("Please go to {auth_url} for a verification code.".format(**auth))
        verifier = input("Verification code: ")

        twitter = Twython(self.consumer_token, self.consumer_secret, auth['oauth_token'], auth['oauth_token_secret'])
        tokens = twitter.get_authorized_tokens(oauth_verifier=verifier)
        return tokens['oauth_token'], tokens['oauth_token_secret']

    def authenticate(self, access_token, secret):
        return AuthorizedTwitter(self.consumer_token, self.consumer_secret, access_token, secret)
def twitter_login(request):

    if "oauth_verifier" in request.GET:
        oauth_verifier = request.GET["oauth_verifier"]
        twitter = Twython(
            settings.PJ_TW_APP_KEY,
            settings.PJ_TW_APP_SECRET,
            request.session["OAUTH_TOKEN"],
            request.session["OAUTH_TOKEN_SECRET"],
        )
        final_step = twitter.get_authorized_tokens(oauth_verifier)
        if not final_step.get("oauth_token_secret"):
            return render(
                request,
                "404.html",
                {
                    "message": "Sorry, Your session has been expired",
                    "reason": "Please kindly try again to update your profile",
                    "email": settings.DEFAULT_FROM_EMAIL,
                    "number": settings.CONTACT_NUMBER,
                },
                status=404,
            )
        twitter = Twython(
            settings.PJ_TW_APP_KEY,
            settings.PJ_TW_APP_SECRET,
            final_step["oauth_token"],
            final_step["oauth_token_secret"],
        )
        followers = twitter.get_followers_list(
            screen_name=final_step["screen_name"])
        friends = twitter.get_friends_list(
            screen_name=final_step["screen_name"])

        if not request.user.is_tw_connected and request.user.is_authenticated:
            Twitter.objects.create(
                user=request.user,
                twitter_id=final_step.get("user_id", ""),
                screen_name=final_step.get("screen_name", ""),
                oauth_token=final_step.get("oauth_token", ""),
                oauth_secret=final_step.get("oauth_token_secret", ""),
            )

        add_twitter_friends_followers.delay(request.user.id, friends,
                                            followers)

        if request.is_mobile == "mobile":
            return HttpResponseRedirect("/jobs/")
        if request.session.get("job_id"):
            log_apply = login_and_apply(request)
            if log_apply:
                return HttpResponseRedirect(log_apply[0].slug + "?job_apply=" +
                                            log_apply[1])
        return HttpResponseRedirect(reverse("my:profile"))
    else:
        twitter = Twython(settings.PJ_TW_APP_KEY, settings.PJ_TW_APP_SECRET)
        url = (request.scheme + "://" + request.META["HTTP_HOST"] +
               reverse("social:twitter_login"))
        auth = twitter.get_authentication_tokens(callback_url=url)
        request.session["OAUTH_TOKEN"] = auth["oauth_token"]
        request.session["OAUTH_TOKEN_SECRET"] = auth["oauth_token_secret"]
        return HttpResponseRedirect(auth["auth_url"])