Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
818 views
in Technique[技术] by (71.8m points)

flask - Heroku Clock Process and Oauthlib

The context to my app is that a user logs in to a third party service such as Xero via the requests_oauthlib library which works fine.

What I need to achieve is that after this login, a scheduled job which runs once a day using the authorised Oauth2Session to grab data such as transactions etc.

I cannot get the clock process to work as it keeps giving me the error:

Working outside of request context. This typically means that you attempted to use functionality that needed an active HTTP request.  Consult the documentation on testing for information about how to avoid this problem.

Below is my app.py file

from requests_oauthlib import OAuth2Session
@app.route("/login")
def login():
    auth= OAuth2Session(client_id,redirect_uri=redirect_uri,scope=scope)
    authorization_url, state = auth.authorization_url(authorization_base_url)
    return redirect(authorization_url)

@app.route("/callback", methods=["GET"])
def callback():
    state = request.args.get('state')
    code = request.args.get('code')
    scope = request.args.get('scope')
    request_url_rebuild = f'{redirect_uri}?code={code}&scope={scope}&state={state}'
    auth = OAuth2Session(client_id, state=state) #
    token = auth.fetch_token(token_url,code=code, body=f'client_id={client_id}&client_secret={client_secret}&redirect_uri={redirect_uri}')
    session['oauth_token'] = token
    return redirect(url_for('.profile'))

@app.route("/profile", methods=["GET","POST"])
def profile():
    try:
        auth= OAuth2Session(client_id, token=session['oauth_token'])
        tennant_id = auth.get('https://api.xero.com/connections').json()[0]['tenantId']
        response = auth.get("https://api.xero.com/api.xro/2.0/BankTransactions",headers={'xero-tenant-id':tennant_id,'Content-Type':'application/json','Accept': 'application/json'})
        transactions = response.json()['BankTransactions']
        return {'results':transactions}

And clock.py file

from apscheduler.schedulers.blocking import BlockingScheduler
from app import app,profile

sched = BlockingScheduler()

@sched.scheduled_job('interval', minutes=2)
def scheduled_task():
  try:   
      with app.app_context():
          profile()
  except Exception as e:
    print(e)
sched.start()

I thought the with app.app_context() would set the context within the clock process (please excuse my terminology). Any help would be appreciated.

question from:https://stackoverflow.com/questions/66055113/heroku-clock-process-and-oauthlib

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...