使用Flask API,如何使用jwt_required装饰器从浏览器/客户端从服务器上传和下载文件?

时间:2019-05-16 08:56:56

标签: python api flask sqlalchemy

我怀疑它与刷新令牌有关。无法理解文档如何使用它。我能知道确切的代码如何使用吗? 访问令牌是在登录期间创建的:

@app.route('/login', methods=['POST','GET'])
def login():
    username = request.form["email"]
    password = request.form["password"]
    my_token_expiry_time = datetime.timedelta(seconds=60)
    segments = 0
    access_token = None
    if request.method == 'POST':
            result_set = authenticate_user(username, password)
            if result_set:
                ss1 = select([nsettings]).\
                   where(nsettings.c.mattribute == 'my_jwt_expiry_time_min')
                rss1 = g.conn.execute(ss1)
                if rss1.rowcount > 0:
                    for r in rss1:
                        my_token_expiry_time = datetime.timedelta(seconds=
                            (int(r[nsettings.c.mvalue])* 60))
                else:
                    my_token_expiry_time = datetime.timedelta(
                        seconds=(2 * 60 *60)) # 2 hours

                #print result_set, 'result_set result_set'
                session['email'] = result_set['email']
                access_token = create_access_token(
                    identity=username, expires_delta=my_token_expiry_time)

            user_dict = result_set
            if user_dict:
                session['email'] = user_dict['email']
                session['id'] = result_set['id']
                # users and related views
                session['access_token'] = access_token
                print access_token, 'aaaaaaaaaaa'
                return jsonify({
                    'email': session['email'],
                    'user_id': result_set['id'],
                    'access_token': access_token,
                    'id': session['id'],
                }), 200
            else:
                return jsonify({'message': "Invalid credentials, retry"}), 401
return "True"

烧瓶API调用以上传:

@app.route('/rt/api/v1.0/issues/<int:issue_id>/documents', methods=['POST'])
    @jwt_required
    def rt_doc_upload(issue_id):
    '''
    Upload documents for a rt ticket.
    '''
    # Iterate through the list of files, we don't care about the
    # attribute name. We consider only the first file and ignore the
    # rest.
    if 'id' in session:
        uploader = "3"
        minternal_only = True
        bool_internal_update = False
        msg_str = None

        for attr, document in request.files.iteritems():
            trans = g.conn.begin()
            try:
                orig_filename = document.filename
                filename, upload_folder = check_or_insert_document(
                    orig_filename, uploader)
                new_doc = add_doc(orig_filename, filename)
                print orig_filename, 'origooooo'
                ins = archival_docs.insert().values(new_doc)
                rs = g.conn.execute(ins)
                doc_id = rs.inserted_primary_key[0]
                filename = (str(doc_id) + '_' + orig_filename)
                stmt = archival_docs.update().values(stored_name=filename).\
                    where(archival_docs.c.id == doc_id)
                g.conn.execute(stmt)
                document.save(os.path.join(upload_folder, filename))
                mattach_doc_id = genUrl(doc_id)
                trans.commit()
                return jsonify(
                    {'issue_doc_id': rs.inserted_primary_key[0]}), 201
            except Exception, e:
                print e
                trans.rollback()

        return jsonify({'message': "Did not find any file"}), 400

    return jsonify({'message': "UNAUTHORIZED"}), 401

当与runserver一起使用并注释jwt_required装饰器时,我可以上传和下载文件  使用sqlalchemy核心,python和flask。上载的api调用工作了一个多月,但现在突然停止了工作

0 个答案:

没有答案