Authentication Exact Online API

112 Views Asked by At

I'm trying to connect the Exact Online API but I'm having problems get it working.| I want to run it local but I get the following response from Exact Online.

Callback URI 'http://localhost:8000/callback' is not valid

I tried different URI's and it's now register as:

https://localhost:8000/callback

The starth auth works but it just make me login into exact online. I've tried to fix this with ChatGTP but I can't figure it out.

def start_oauth(request) : 
    client_id = '64123f99-7276-435b-8f47-c09c553f0071'
    redirect_uri = 'https://localhost:8000/callback'  
    scope = 'financial' 
    state = 'qetiohqpewtioadvhsiofadjpasdf'
    auth_url = f"https://start.exactonline.nl/api/oauth2/auth?response_type=code&client_id={client_id}&redirect_uri={redirect_uri}&scope={scope}&state={state}"

    return redirect(auth_url)

def auth_callback(request): 
    code = request.GET.get('code')
    state = request.GET.get('qetiohqpewtioadvhsiofadjpasdf')

    return exchange_code_for_token(code)

def exchange_code_for_token(code):
    token_url = 'https://start.exactonline.nl/api/oauth2/token'
    client_id = '64123f99-7276-435b-8f47-c09c553f0071' 
    client_secret = '***********'
    redirect_uri = 'https://localhost:8000/callback'

    date = {
         'grant_type': 'authorization_code',
        'code': code,
        'redirect_uri': redirect_uri,
        'client_id': client_id,
        'client_secret': client_secret
    }

    try: 
        response = requests.post(token_url, data=data)
        response.raise_for_status()  # Raises an exception for HTTP errors

        # If the request is successful, retrieve the access and refresh tokens
        response_data = response.json(),
        access_token = response_data.get('access_token')
        refresh_token = response_data.get('refresh_token')

        # Here you'd store the access_token and refresh_token securely
        # For example, you might save them in your database associated with a user

        # Then you'd redirect the user or respond as needed
        # This is just an example response
        return JsonResponse({
            'access_token': access_token,
            'refresh_token': refresh_token
        })

    except requests.exceptions.RequestException as e:
        # Handle the exception, log it, and return an error response
        return JsonResponse({'error': str(e)}, status=400)

What am I doing wrong?

0

There are 0 best solutions below