# Can I use the Discourse API to authenticate users in another app?

**URL:** <https://meta.discourse.org/t/can-i-use-the-discourse-api-to-authenticate-users-in-another-app/266322>\
**Category:** SSO\
**Created:** [May 26, 2023, 2:29pm UTC](https://meta.discourse.org/t/can-i-use-the-discourse-api-to-authenticate-users-in-another-app/266322 "2023-05-26T14:29:17Z")\
**Posts on this page:** 1\
**Showing post:** 13

<div class="post-metadata">

**Author:** ![ygramoel](https://avatars.discourse-cdn.com/v4/letter/y/74df32/32.png) [@ygramoel](https://meta.discourse.org/u/ygramoel)\
**Post date:** [May 30, 2023, 3:20pm UTC](https://meta.discourse.org/t/can-i-use-the-discourse-api-to-authenticate-users-in-another-app/266322/13 "2023-05-30T15:20:01Z")

</div>

> [@supermathie](#):
>
> More information on this is explained here: [Use Discourse as an identity provider (SSO, DiscourseConnect)](https://meta.discourse.org/t/use-discourse-as-an-identity-provider-sso-discourseconnect/32974)

I’ve tried to apply this but can’t get it to work. Below is some (simplified) Python code that generates a url for `.../session/sso_provider`. When I try it, I get `Login Error`. No idea what that means.

```plaintext
import secrets
import base64
import urllib.parse
import hmac
import hashlib

forum_url = 'https://forum.embeetle.com'
target_url = 'https://embeetle.com/#account'
sso_secret = b'JCLSVcqbAnEPXz2p2xBY'

nonce = secrets.token_urlsafe()
payload = f'nonce={nonce}&return_sso_url={target_url}'
payload_base64 = base64.b64encode(payload.encode('utf-8')).decode()
payload_for_url = urllib.parse.quote(payload_base64)

payload_for_url = 'bm9uY2U9YklKeEU1WWw2OFhjSkJydGlwSU15UTRZeVlMeWd6ZzQyUU9mOFo0SWF5QSZyZXR1cm5fc3NvX3VybD1odHRwczovL2VtYmVldGxlLmNvbS8jYWNjb3VudA%3D%3D'

signature = hmac.new(
    sso_secret, payload_for_url.encode('utf-8'), hashlib.sha256
).hexdigest()

print(f'{forum_url}/session/sso_provider?sso={payload_for_url}&sig={signature}')

```

For example, one run might generate the url in the `curl` command below:

```plaintext
johan@morla:~/sa\> curl 'https://forum.embeetle.com/session/sso_provider?sso=bm9uY2U9YklKeEU1WWw2OFhjSkJydGlwSU15UTRZeVlMeWd6ZzQyUU9mOFo0SWF5QSZyZXR1cm5fc3NvX3VybD1odHRwczovL2VtYmVldGxlLmNvbS8jYWNjb3VudA%3D%3D&sig=a392ebb81b93ba7411290fbd00240921ae053bbb82998830dda994c8a71853da'
Login Errorjohan@morla:~/sa\> 

```

---

_[View the full topic](https://meta.discourse.org/t/can-i-use-the-discourse-api-to-authenticate-users-in-another-app/266322)._
