Announcements

Help Wizard

Step 1

NEXT STEP

FAQs

Please see below the most popular frequently asked questions.

Loading article...

Loading faqs...

VIEW ALL

Ongoing Issues

Please see below the current ongoing issues which are under investigation.

Loading issue...

Loading ongoing issues...

VIEW ALL

Invalid Client Error In Docker Container

Solved!

Invalid Client Error In Docker Container

Running into an issue with my flask application. When not running my app inside of a docker container, I'm able to successfully authorize via Spotify API.

1. User navigates to localhost:5000/login
2. Spotify authorization agreement is displayed
3. User clicks "Agree"
4. User is routed to localhost:5000/callback where the text "Success" is displayed

 

But, when I dockerize the flask app, I get the following error in the browser when the user clicks "Agree" on the authorize page:

Internal Server Error

The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

 

On the backend, here's the error i'm getting in the logs

{'error': 'invalid_client', 'error_description': 'Invalid client'}

 

 

Below is my Dockerfile, and app.py file

Dockerfile 
FROM python:3.9-slim-buster
WORKDIR /app
COPY ./requirements.txt /app
RUN pip install -r requirements.txt
COPY . .
EXPOSE 5000
ENV FLASK_APP=app.py
CMD ["flask", "run", "--host", "0.0.0.0"]

app.py
from flask import Flask, render_template, flash, session, redirect, request, Response, session, make_response
from spotify_connector import SpotifyConnector
connector = SpotifyConnector()

@app.route('/')
def index():
return "INDEX"

@app.route('/login')
def login():
client_id = app.config['CLIENT_ID']
redirect_uri = app.config['REDIRECT_URI']
scope = app.config['SCOPE']
authorize_url = 'https://accounts.spotify.com/en/authorize?'
state_key = connector.create_state_key(15)
session['state_key'] = state_key
parameters = 'response_type=code&client_id=' + client_id + '&redirect_uri=' + redirect_uri + '&scope=' + scope + '&state=' + state_key
response = make_response(redirect(authorize_url + parameters))
return response


@app.route('/callback')
def callback():
code = request.args.get('code', None)
state = request.args.get('state', None)
session.pop('state_key', None)
payload = connector.getToken(code)
if payload is not None:
session['access_token'] = payload['access_token']
session['refresh_token'] = payload['refresh_token']
return "SUCCESS"
else:
return "FAILURE"

if __name__ == "__main__":
app.run(host="localhost")

 

Reply

Accepted Solutions
Marked as solution

In the part of my code that generates the token for Spotify (where the error was originating), I was retrieving my client id and secret by using:
CLIENT_ID = os.getenv("CLIENT_ID")

CLIENT_SECRET = os.getenv("CLIENT_SECRET")

I threw in a print stament, and saw that Authorization header looked like this --Authorization: Basic "":"" , when it should look like this Authorization: Basic CLIENT_ID:CLIENT_SECRET. This confirmed that CLIENT_ID and CLIENT_SECRET weren't available.

I added the following to my Dockerfile, and I'm now able to successfully authorize:

ENV CLIENT_ID=*****

ENV CLIENT_SECRET=*****

Going further, I'm most likely going to get rid of the above two ENV declarations, and add the os.getenv declarations to my flask app config file.

View solution in original post

4 Replies

Did you add the adress:port of the docker container to the callback URIs at the dashboard of your app?

XimzendSpotify Star
Help others find this answer and click "Accept as Solution".
If you appreciate my answer, maybe give me a Like.
Note: I'm not a Spotify employee.

CMD ["flask", "run", "--host", "0.0.0.0"] ?

XimzendSpotify Star
Help others find this answer and click "Accept as Solution".
If you appreciate my answer, maybe give me a Like.
Note: I'm not a Spotify employee.
Marked as solution

In the part of my code that generates the token for Spotify (where the error was originating), I was retrieving my client id and secret by using:
CLIENT_ID = os.getenv("CLIENT_ID")

CLIENT_SECRET = os.getenv("CLIENT_SECRET")

I threw in a print stament, and saw that Authorization header looked like this --Authorization: Basic "":"" , when it should look like this Authorization: Basic CLIENT_ID:CLIENT_SECRET. This confirmed that CLIENT_ID and CLIENT_SECRET weren't available.

I added the following to my Dockerfile, and I'm now able to successfully authorize:

ENV CLIENT_ID=*****

ENV CLIENT_SECRET=*****

Going further, I'm most likely going to get rid of the above two ENV declarations, and add the os.getenv declarations to my flask app config file.

Suggested posts

Type a product name