title: Flask Web Application Cannot Connect to Spotify API on my Nginx linode server, works on my local machine
body: <p>I'm pretty new to web development, trying to learn using a website I created using flask which is hosted on a linode server. I created an application to generate playlists using the spotify API, however my API calls don't seem to work when actually put into my server. It functions perfectly on my local machine.</p>
  <p>I get in my error.log a bunch of this message:
  2022/03/07 19:36:35 [error] 7013#7013: *8223 upstream prematurely closed connection while reading response header from upstream, client: ip, server: ip, request: &quot;GET /spotify/verify/Island%20in%20the%20sun%2C%20Ms.%20Jackson HTTP/1.1&quot;, upstream: &quot;http://127.0.0.1:8000/spotify/verify/Island%20in%20the%20sun%2C%20Ms.%20Jackson&quot;, host: &quot;umatt.me&quot;, referrer: &quot;http://umatt.me/spotify/&quot;
  2022/03/07 19:36:35 [error] 7013#7013: *8223 connect() failed (111: Connection refused) while connecting to upstream, client: ip, server: ip, request: &quot;GET /spotify/verify/Island%20in%20the%20sun%2C%20Ms.%20Jackson HTTP/1.1&quot;, upstream: &quot;http://[::1]:8000/spotify/verify/Island%20in%20the%20sun%2C%20Ms.%20Jackson&quot;, host: &quot;umatt.me&quot;, referrer: &quot;http://umatt.me/spotify/&quot;</p>
  <p>On the frontend, nginx displays:</p>
  <p>502 Bad Gateway</p>
  <p>nginx/1.14.2</p>
  <p>The site is hosted at <a href="http://www.umatt.me/spotify" rel="nofollow noreferrer">http://www.umatt.me/spotify</a> if that helps at all.</p>
  <p>I've been looking around for a few months trying to figure this one out and as a result the project hasn't really been able to move forward. I'm wondering if the problem is with spotify permissions? I am using the spotipy python package as well.</p>
  <p>Here is some code I believe is relevant. The &quot;spotify&quot; object is  imported as a flask blueprint on my main script:</p>
  <pre><code>from flask import Flask, Blueprint, request, redirect, url_for, render_template, session
  from commandline_interface import walk_two_songs, get_track, create_playlist
  import json
  
  spotify = Blueprint(&quot;spotify&quot;, __name__, template_folder='templates')
  
  
  @spotify.route(&quot;/verify/&lt;searchstring&gt;&quot;, methods=['POST','GET'])
  def verify_tracks(searchstring):
      # this page will display up to 5 search queries for the up to 5 listed anchors
      searchstring=searchstring.split(&quot;,&quot;)
  
      if len(searchstring) &gt; 5:
          return &quot;only 5 anchors allowed&quot;
  
      if request.method == 'POST':
          # from this spot, we need to feed the data into the next page
          tracks = []
          for i, search in enumerate(searchstring):
              # we need to:
              #   1. check the set of radios
              #   2. identify which is selected
              #   3. append the correct values into tracks
              id = request.form[f'options{i}']
              tracks.append(id)
          tracksstring = &quot;|&quot;
          tracksstring = tracksstring.join(tracks)
          return redirect(url_for(&quot;spotify.spotify_tracks&quot;, tracks=tracksstring))
      else:
          queries = []
          print(searchstring)
          for search in searchstring:
              results = get_track(search)
              if not results:
                  return f&quot;could not find any results for {search}. please try again.&quot;
              queries.append(results)
          values = range(0, len(queries))
          return render_template(&quot;verification_page.html&quot;, queries=queries, values=values)
  
  
  @spotify.route(&quot;/&quot;, methods=['POST', 'GET'])
  def spotify_home():
      if request.method == 'POST':
          tracks = request.form['tracks']
          return redirect(url_for(&quot;spotify.verify_tracks&quot;,searchstring=tracks))
      else:
          return render_template(&quot;spotify_home.html&quot;)
  
  
  @spotify.route(&quot;/tracks/&lt;tracks&gt;&quot;, methods=[&quot;POST&quot;,&quot;GET&quot;])
  def spotify_tracks(tracks):
      if request.method == 'POST':
          playlist = session['total_playlist']
          create_playlist(f&quot;{session['start']} to {session['end']}&quot;, playlist)
          return &quot;&lt;h1&gt;check your spotify o.o&lt;/h1&gt;&quot;
      else:
          tracks = tracks.split(&quot;|&quot;)
          print(tracks)
          # currently, we have a list of track ids. the idea here is:
          #   1. walk between every track to create a playlist
          #   2. create a spotify playlist
          total_playlist = []
          last_track = None
          for ind, track in enumerate(tracks):
              if ind == len(tracks)-1:
                  continue
              next_track = tracks[ind+1]
              walk = walk_two_songs(track, next_track)
              total_playlist.extend(walk[:-1])
              last_track = walk[-1]
          total_playlist.append(last_track)
  
          to_return_test = &quot;&quot;
          for track in total_playlist:
              to_return_test += f&quot;&lt;p&gt;{track['name']} by {track['artists'][0]['name']}&lt;/p&gt;&quot;
          # all we need now is to create a spotify playlist
          session['total_playlist'] = [x['id'] for x in total_playlist]
          session['start'] = total_playlist[0]['name']
          session['end'] = total_playlist[-1]['name']
          values = range(0, len(total_playlist))
          return render_template(&quot;display_playlist.html&quot;, total_playlist=total_playlist, values=values)
  
  
  if __name__ == '__main__':
      app = Flask(__name__)
      app.register_blueprint(spotify)
      with open(&quot;/etc/config.json&quot;) as config:
          config = json.load(config)
          session_key = config.get(&quot;session_key&quot;)
      app.secret_key = session_key
      '''
      app.config.update(
          SESSION_PERMANENT=False,
          SESSION_TYPE=&quot;filesystem&quot;
      )
      '''
      app.run(debug=True)
  </code></pre>