我试图用Python创建一个Flask应用程序,该应用程序与使用OAuth2.0的RESTful API进行通信,但是我无法发送请求。我已成功从服务器获取访问令牌,但是当我尝试发出GET或POST请求时,总是得到404作为响应。我是RESTful API的新手,所以我不知道自己在这里可能做错了什么,或者在这种情况下404意味着什么。
这是.py文件,我尝试在其中将GET和POST请求发送到API。 TokenHandler
只是我制作的一个帮助程序类,用于跟踪令牌相关的信息,并且print(response)
行始终显示“ Response [404]”。
from flask import Flask, render_template, request, redirect
from src.apitest import TokenHandler
import requests
from oauthlib.oauth2 import LegacyApplicationClient
from requests_oauthlib import OAuth2Session
import json
app = Flask(__name__)
token_handler = TokenHandler()
@app.route('/find_customer')
def find_customer():
return render_template('customer_find_page.html')
@app.route('/find_customer', methods=['POST'])
def find_customer_get():
customer_id = request.form['text0']
oauth = OAuth2Session(client=LegacyApplicationClient(client_id=token_handler.CLIENT_ID))
oauth.fetch_token(token_url=token_handler.TOKEN_ENDPOINT,
username=token_handler.USERNAME, password=token_handler.PASSWORD, client_id=token_handler.CLIENT_ID,
client_secret=token_handler.CLIENT_SECRET,
scope=token_handler.SCOPE)
response = oauth.get("https://sedonacloudtest.com/api/customers/"+customer_id)
print(response)
return redirect('/find_customer')
@app.route('/add_customer')
def add_customer():
return render_template('customer_add_page.html')
@app.route('/add_customer', methods=['POST'])
def add_customer_post():
customer_id = request.form['text0']
customer_name = request.form['text1']
data = {"customer_id": customer_id, "customer_name": customer_name}
oauth = OAuth2Session(client=LegacyApplicationClient(client_id=token_handler.CLIENT_ID))
oauth.fetch_token(token_url=token_handler.TOKEN_ENDPOINT,
username=token_handler.USERNAME, password=token_handler.PASSWORD, client_id=token_handler.CLIENT_ID,
client_secret=token_handler.CLIENT_SECRET,
scope=token_handler.SCOPE)
response = oauth.post("https://sedonacloudtest.com/api/customers", data=data)
print(response)
return redirect('/add_customer')
if __name__ == "__main__":
app.run()