# OAuth-Login mit ETRON onRetail für Apps/Websites

{% hint style="success" %}
*Modul: REST API*
{% endhint %}

Diese Anleitung beschreibt die Integration des OAuth 2.0 Logins von ETRON onRetail in externe Webanwendungen oder Apps. Dies ermöglicht die Anmeldung von Benutzerkonten aus ETRON onRetail in Drittanwendungen (z. B. progressive Web-Apps, Kundenzugänge oder Migrationsportale).&#x20;

{% stepper %}
{% step %}

### Einrichtung eines OAuth-Clients in ETRON onRetail

1. Anmeldung in der ETRON onRetail Verwaltungsoberfläche
2. Navigieren zu [API & Hooks](/verwaltungsoberflache/api-and-hooks.md).
3. Neuen OAuth-Client anlegen:
   * Name: z. B. *Meiiapp Auth*<br>

     <figure><img src="/files/Oz9qu4iYU0wN5K6T4y3b" alt=""><figcaption></figcaption></figure>
   * **Client-ID** und **Client-Secret** speichern
   * **Redirect URL (Callback-URL)** der Zielanwendung (Partner-App/-Website) eintragen\
     z. B. `https://meii.app/oauth/callback`

     <figure><img src="/files/X7dKRRTEgEqIIavcvl6b" alt=""><figcaption></figcaption></figure>

{% endstep %}

{% step %}

## Implementierung des OAuth-Flows in der Drittanwendung

1. **Login-Button**\
   Ein Login-Button in der Drittanwendung leitet zur Authorisierungsseite von ETRON onRetail weiter.

   <figure><img src="/files/6XTHLrTmu1M9WdE529YD" alt=""><figcaption></figcaption></figure>
2. **Autorisierungscode**\
   Nach erfolgreichem Login wird der Nutzer zur Callback-URL der Drittanwendung umgeleitet. Ein **Authorization-Code** wird dabei übermittelt.<br>
3. **Access-Token anfordern**\
   Serverseitig oder in der App (z. B. via Secure Storage) wird der Authorization-Code gegen ein **Access-Token** getauscht.
4. **Authentifizierte API-Aufrufe**\
   Alle weiteren API-Aufrufe können mit dem Access-Token über den `Authorization`-Header erfolgen:&#x20;

<pre class="language-http"><code class="lang-http"><strong>httpKopierenBearbeitenAuthorization: Bearer [ACCESS_TOKEN]
</strong></code></pre>

{% endstep %}
{% endstepper %}

## Beispiel in Python

````python
```python
#Beispiel via Python und flask lib

 

from flask_oauthlib.client import OAuth

 

app = Flask(__name__)

app.secret_key = CONFIG['app_secret']

oauth = OAuth(app)

 

# Configere the OAuth 2.0 client

Meiiapp = oauth.remote_app(

    'meiiapp',

    consumer_key='meiiappkey', #aus oath client config

    consumer_secret=CONFIG['wo_das_secret_abgelegt_ist'], #aus oauth client config

    request_token_params={

        'scope': 'Meiiapp',  # Scope ist frei waehlbar 

    },

    base_url='https://KUNDENDOMAIN_ONRETAIL/',

    request_token_url=None,

    access_token_method='POST',

    access_token_url=base_url+'/api/v1/authentication/oauth2/token',

    authorize_url=base_url+'/api/v1/authentication/oauth2/authorize',

)

 

# Beispiel fuer die Login/Logout Routen

@app.before_request

def check_login():

    #erlaube localhost ohne auth

    if request.host in ['localhost', '127.0.0.1', 'localhost:8000', '127.0.0.1:8000']:

        return None

 

    whitelist = ['/logout', '/login', '/authorized', '/static/']

    if not any(map(lambda x: request.path.startswith(x), whitelist)):

        if 'meiiapp_oauth_token' not in session:

            return redirect(url_for('login'))

 

@app.route('/login')

def login():

    return Meiiapp.authorize(callback=url_for('authorized', _external=True, _scheme=_scheme))

 

@app.route('/logout')

def logout():

    session.pop('meiapp_oauth_token', None)

    return redirect('/login')

 

@app.route('/authorized')

def authorized():

    response = Meiiapp.authorized_response()

    if response is None or response.get('access_token') is None:

        return 'Access denied: reason={} error={}'.format(

            request.args['error_reason'],

            request.args['error_description']

        )

 

    # Save the user's access token and other information as needed

    session['meiiapp_oauth_token'] = (response['access_token'], '')

 

    # At this point, the user is logged in. Redirect as needed.

    return redirect(url_for('meiiapp_homepage'))
````


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://helpcenter.etron.info/schnelles-wissen/verwaltungsoberflache/oauth-login-mit-etron-onretail-fur-apps-websites.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
