# 401 Not Authorized for 'mozilla\_django\_oidc.contrib.drf.OIDCAuthentication' authenticate()

**URL:** <https://forum.keycloak.org/t/401-not-authorized-for-mozilla-django-oidc-contrib-drf-oidcauthentication-authenticate/6160>\
**Category:** Miscellanaeous\
**Tags:** authentication, oidc\
**Created:** [November 27, 2020, 8:06pm UTC](https://forum.keycloak.org/t/401-not-authorized-for-mozilla-django-oidc-contrib-drf-oidcauthentication-authenticate/6160 "2020-11-27T20:06:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmath1](https://avatars.discourse-cdn.com/v4/letter/j/b3f665/32.png) [@jmath1](https://forum.keycloak.org/u/jmath1)\
**Post date:** [November 27, 2020, 8:06pm UTC](https://forum.keycloak.org/t/401-not-authorized-for-mozilla-django-oidc-contrib-drf-oidcauthentication-authenticate/6160/1 "2020-11-27T20:06:17Z")

</div>

The django logs that are comming up are:

```auto
DJANGO [2020-11-27 19:55:14.894] DEBUG [urllib3.connectionpool:452] http://keycloak:8080 "GET /auth/realms/Clarsynt/protocol/openid-connect/certs HTTP/1.1" 200 1466
DJANGO [2020-11-27 19:55:14.919] DEBUG [urllib3.connectionpool:227] Starting new HTTP connection (1): keycloak:8080
DJANGO [2020-11-27 19:55:14.926] DEBUG [urllib3.connectionpool:452] http://keycloak:8080 "GET /auth/realms/Clarsynt/protocol/openid-connect/userinfo HTTP/1.1" 401 73
*** requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: 
http://keycloak:8080/auth/realms/Clarsynt/protocol/openid-connect/userinfo

```

The keycloak logs:

```auto
20:01:02,746 WARN [org.keycloak.events] (default task-2) type=USER_INFO_REQUEST_ERROR, realmId=Clarsynt, clientId=null, userId=null, ipAddress=172.20.0.7, error=invalid_token, auth_method=validate_access_token

```

I am using the settings as described in the mozilla\_django\_oidc package. I am trying to use Keycloak OAuth2 authentication for my django app. Keycloak and Django are running in separate containers. Can somebody please tell me why I am getting the 401 error?

 ![Screen Shot 2020-11-27 at 15.01.25](https://global.discourse-cdn.com/free1/uploads/keycloak/original/2X/4/46a0ec806023e5b5c816275984004aa90fbdf6a4.png)

this is the relevant portion of my docker-compose file:

```auto
  keycloak:
      image: jboss/keycloak
      environment:
        DB_VENDOR: POSTGRES
        DB_ADDR: db
        DB_DATABASE: postgres
        DB_USER: postgres
        DB_SCHEMA: public
        DB_PASSWORD: webapp-devel
        KEYCLOAK_USER: myuser
        KEYCLOAK_PASSWORD: *******
        KEYCLOAK_IMPORT: /tmp/realm-export.json
      ports:
        - 9990:8080
  webapp:
    command: ./manage.py runserver 0.0.0.0:8000
    depends_on:
      - db
    environment:
      PORT: "8000"
      DEBUG: "True"

```

---

<div class="post-metadata">

**Author:** ![diegosm](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/diegosm/32/1850_2.png) [@diegosm](https://forum.keycloak.org/u/diegosm)\
**Post date:** [December 8, 2020, 6:46pm UTC](https://forum.keycloak.org/t/401-not-authorized-for-mozilla-django-oidc-contrib-drf-oidcauthentication-authenticate/6160/2 "2020-12-08T18:46:24Z")

</div>

Hi,

Did you already checked the client and secret on django?

```auto
20:01:02,746 WARN [org.keycloak.events] (default task-2) type=USER_INFO_REQUEST_ERROR, realmId=Clarsynt, clientId=null, userId=null, ipAddress=172.20.0.7, error=invalid_token, auth_method=validate_access_token

```

clientId is null and user id also, and gives invalid token.

---

<div class="post-metadata">

**Author:** ![jmath1](https://avatars.discourse-cdn.com/v4/letter/j/b3f665/32.png) [@jmath1](https://forum.keycloak.org/u/jmath1)\
**Post date:** [December 30, 2020, 3:05pm UTC](https://forum.keycloak.org/t/401-not-authorized-for-mozilla-django-oidc-contrib-drf-oidcauthentication-authenticate/6160/3 "2020-12-30T15:05:16Z")

</div>

yes, I did. I think it is working this way because it is making a request to keycloak:8080, but the jwt has the url localhost:9990 which is the frontend url. This problem has still not been fixed

---

<div class="post-metadata">

**Author:** ![diegosm](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/diegosm/32/1850_2.png) [@diegosm](https://forum.keycloak.org/u/diegosm)\
**Post date:** [December 30, 2020, 3:25pm UTC](https://forum.keycloak.org/t/401-not-authorized-for-mozilla-django-oidc-contrib-drf-oidcauthentication-authenticate/6160/4 "2020-12-30T15:25:52Z")

</div>

So, the keycloak open other ports, is not just 8080, maybe this is not the error.

Try to authenticate on postman with the same data, in 8080 port.

POST : [http://keycloak:8080/auth/realms/master/protocol/openid-connect/token](http://keycloak:8080/auth/realms/master/protocol/openid-connect/token)  
grant\_type \> client\_credentials  
client\_id \>  
client\_secret \>

try it 🙂
