# Set audience inside Django App to call protected API

**URL:** <https://community.auth0.com/t/set-audience-inside-django-app-to-call-protected-api/112260>\
**Category:** Get Help\
**Tags:** login-prompt-new-experience, login-experience\
**Created:** [July 27, 2023, 1:05pm UTC](https://community.auth0.com/t/set-audience-inside-django-app-to-call-protected-api/112260 "2023-07-27T13:05:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mkd](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mkd/32/49346_2.png) [@mkd](https://community.auth0.com/u/mkd)\
**Post date:** [July 27, 2023, 1:05pm UTC](https://community.auth0.com/t/set-audience-inside-django-app-to-call-protected-api/112260/1 "2023-07-27T13:05:54Z")

</div>

Hello,

I have started using Auth0 inside of a django application and I followed the quickstart guide here:

> **[Auth0 Django SDK Quickstarts: Login](https://auth0.com/docs/quickstart/webapp/django/01-login)**
>
> This tutorial demonstrates how to add user login to a Python web application built with the Django framework and Authlib OAuth library.

I have it all up and running, login works like a dream and everything was fine.

I then moved on to building an API which again has Auth0 authentication. API is written in python fastapi and again you have amazing documentation which I have followed here:

> **[Build and Secure a FastAPI Server with Auth0](https://auth0.com/blog/build-and-secure-fastapi-server-with-auth0/)**
>
> Learn the basics of FastAPI, how to quickly set up a server and secure endpoints with Auth0.

The problem I am running into now, is getting a user who is logged into the django app to hit the API.

I am getting the current error message:

```auto
{
  "detail": {
    "status": "error",
    "message": "Audience doesn't match"
  }
}

```

Which is all pretty self explanatory. My Bearer token has the `aud` set as the Client ID of my django app. What needs to happen is during sign in I need to be able to update the audience, so I tried the following:

```auto
def login(request):
    return oauth.auth0.authorize_redirect(
        request,
        request.build_absolute_uri(reverse("callback")),
        audience=settings.AUTH0_AUDIENCE
    )

```

But this does not work.

How do I update the audience inside the django app so I can authenticate inside of the API?

Any help would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![mkd](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mkd/32/49346_2.png) [@mkd](https://community.auth0.com/u/mkd)\
**Post date:** [July 27, 2023, 1:07pm UTC](https://community.auth0.com/t/set-audience-inside-django-app-to-call-protected-api/112260/2 "2023-07-27T13:07:59Z")

</div>

I reason for trying:

```auto
def login(request):
    return oauth.auth0.authorize_redirect(
        request,
        request.build_absolute_uri(reverse("callback")),
        audience=settings.AUTH0_AUDIENCE
    )

```

Was due to the fact it talks about it here in the Flask example:

> **[Flask Authentication By Example](https://developer.auth0.com/resources/guides/web-app/flask/basic-authentication#call-a-protected-api-from-your-flask-web-app)**
>
> Python code that implements user login, logout and sign-up features to secure a Flask Web Application using Auth0.

---

<div class="post-metadata">

**Author:** ![mkd](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/mkd/32/49346_2.png) [@mkd](https://community.auth0.com/u/mkd)\
**Post date:** [July 27, 2023, 1:13pm UTC](https://community.auth0.com/t/set-audience-inside-django-app-to-call-protected-api/112260/3 "2023-07-27T13:13:49Z")

</div>

I have resolved my own problem.

Solution:

I was trying to get the Django app to match the API, but instead I needed to get the API to match the django app.

Therefore all i needed to do was to update the audience in my fastapi code be the client id of my django app and then boom…all working perfectly.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [August 10, 2023, 1:14pm UTC](https://community.auth0.com/t/set-audience-inside-django-app-to-call-protected-api/112260/4 "2023-08-10T13:14:46Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
