Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following function in my views.py that will send me the authorised user's key tokens. I can then manually amend these tokens for my Twitter API function locally to run the program.

Python
def like(request):
   oauth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
   access_key = request.session['access_key_tw']
   access_secret = request.session['access_secret_tw']

   send_mail(
       'Auth Codes', # subject
       'access key -  ' + os.environ['oaccess_key'] + '\n' + 'access_secret -  ' + os.environ['oaccess_secret'], # message
       DEFAULT_FROM_EMAIL, # from email
       [DEFAULT_FROM_EMAIL],
       )

   return render(request, "home.html")


However having to do this manually isn't great and for security reasons I'd rather not have them emailed. I want to push the function to a celery task so that I don't need the sendmail() at all. My celery task would look like this after liking.delay() being called within like() -

Python
@app.task(bind=True)
def liking(self):

    oauth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
    oauth.set_access_token(access_key, access_secret)
    api = tweepy.API(oauth)

    ** Rest of my code using the Twitter API **


What I have tried:

I have tried simply importing the variables or adding making them global but Heroku will not let me deploy when doing this.

I have also tried to set the access_key and access_secret as environment variables but this returns as 'none'.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900