I'm developing a FastAPI app. Now I need to integrate Xero in our app, but the authentication cannot pass. It gives me this error: xero_python.exceptions.OAuth2TokenGetterError: Invalid oauth2_token_getter=None function.Here is my code:
import json
from fastapi import APIRouter, FastAPI, HTTPException, Depends
from xero_python.accounting import AccountingApi, Invoice
from xero_python.api_client import ApiClient
from xero_python.api_client.oauth2 import OAuth2Token
from xero_python.api_client import Configuration
from xero_python.exceptions import ApiException
from xero_python.accounting import Contact, LineItem, Invoice
# Assuming you have the necessary OAuth2.0 tokens
# Define the invoice model
# Create a method to create the Xero ApiClient
CLIENT_ID = 'CLIENT_ID'
CLIENT_SECRET = 'CLIENT_SECRET'
REDIRECT_URI = 'developer.xero.com'
SCOPES = "openid profile email accounting.transactions offline_access" # Customize the scopes as needed
router = APIRouter()
api_client = ApiClient(
Configuration(
debug=True,
oauth2_token=OAuth2Token(
client_id = CLIENT_ID,
client_secret = CLIENT_SECRET,
),
),
pool_threads=1,
)
def get_xero_client() -> AccountingApi:
return AccountingApi(api_client)
@router.post("/invoice")
async def create_invoice(xero: AccountingApi = Depends(get_xero_client)):
# Define the invoice object according to Xero's requirements
xero_invoice = Invoice(
type='ACCREC', # Account Receivable
contact= {
'name': 'Jack'
},
line_items=[
LineItem(
description='xxx',
quantity=1,
unit_amount=20,
account_code='200' # This is an example. Please choose the appropriate Account Code.
)
],
status='AUTHORISED'
)
try:
# Post the invoice
response = xero.create_invoices('a6d4f890-9c73-4f62-807c-9555f12a47ac', invoices=[xero_invoice])
# Return the ID of the new invoice
return 200
except ApiException as e:
raise HTTPException(status_code=400, detail=str(e))
@router.get("/invoice")
async def get_invoice(xero: AccountingApi = Depends(get_xero_client)):
try:
response = xero.get_invoices('TENANT_ID')
return response
except ApiException as e:
raise HTTPException(status_code=400, detail=str(e))
When I call get invoice endpoint, I got this error:
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "/usr/local/lib/python3.11/site-packages/uvicorn/protocols/http/h11_impl.py", line 429, in run_asgi
result = await app( # type: ignore[func-returns-value]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/uvicorn/middleware/proxy_headers.py", line 78, in __call__
return await self.app(scope, receive, send)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/fastapi/applications.py", line 276, in __call__
await super().__call__(scope, receive, send)
File "/usr/local/lib/python3.11/site-packages/starlette/applications.py", line 122, in __call__
await self.middleware_stack(scope, receive, send)
File "/usr/local/lib/python3.11/site-packages/starlette/middleware/errors.py", line 184, in __call__
raise exc
File "/usr/local/lib/python3.11/site-packages/starlette/middleware/errors.py", line 162, in __call__
await self.app(scope, receive, _send)
File "/usr/local/lib/python3.11/site-packages/starlette/middleware/trustedhost.py", line 34, in __call__
await self.app(scope, receive, send)
File "/usr/local/lib/python3.11/site-packages/starlette/middleware/cors.py", line 83, in __call__
await self.app(scope, receive, send)
File "/usr/local/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 79, in __call__
raise exc
File "/usr/local/lib/python3.11/site-packages/starlette/middleware/exceptions.py", line 68, in __call__
await self.app(scope, receive, sender)
File "/usr/local/lib/python3.11/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in __call__
raise e
File "/usr/local/lib/python3.11/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in __call__
await self.app(scope, receive, send)
File "/usr/local/lib/python3.11/site-packages/starlette/routing.py", line 718, in __call__
await route.handle(scope, receive, send)
File "/usr/local/lib/python3.11/site-packages/starlette/routing.py", line 276, in handle
await self.app(scope, receive, send)
File "/usr/local/lib/python3.11/site-packages/starlette/routing.py", line 66, in app
response = await func(request)
^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/fastapi/routing.py", line 237, in app
raw_response = await run_endpoint_function(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/fastapi/routing.py", line 163, in run_endpoint_function
return await dependant.call(**values)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/projecrt_name/app/api/endpoints/v1/xero.py", line 66, in get_invoice
response = xero.get_invoices('a6d4f890-9c73-4f62-807c-9555f12a47ac')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/xero_python/accounting/api/accounting_api.py", line 10631, in get_invoices
return self.api_client.call_api(
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/xero_python/api_client/__init__.py", line 349, in call_api
return self.__call_api(
^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/xero_python/api_client/__init__.py", line 185, in __call_api
self.update_params_for_auth(header_params, query_params, auth_settings)
File "/usr/local/lib/python3.11/site-packages/xero_python/api_client/__init__.py", line 578, in update_params_for_auth
auth_setting = auth_setting(api_client=self)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/xero_python/api_client/oauth2.py", line 176, in create_auth_settings
self.update_token(**api_client.get_oauth2_token())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/xero_python/api_client/__init__.py", line 720, in get_oauth2_token
raise OAuth2TokenGetterError(
xero_python.exceptions.OAuth2TokenGetterError: Invalid oauth2_token_getter=None function
Can anyone help me solve the authentication issue? thank you.