Spotify-Anmeldeinformationen mit Spotipy @ festleg

Ich probiere spotipy mit Python 2.7.10 aus, das auf meinem Mac 10.10 vorinstalliert ist, und zwar [add_a_saved_track.py] [1] Hier ist der Code, der von github kopiert wurde:

# Add tracks to 'Your Collection' of saved tracks

import pprint
import sys

import spotipy
import spotipy.util as util

scope = 'user-library-modify'

if len(sys.argv) > 2:
    username = sys.argv[1]
    tids = sys.argv[2:]
else:
    print("Usage: %s username track-id ..." % (sys.argv[0],))
    sys.exit()

token = util.prompt_for_user_token(username, scope)

if token:
    sp = spotipy.Spotify(auth=token)
    sp.trace = False
    results = sp.current_user_saved_tracks_add(tracks=tids)
    pprint.pprint(results)
else:
    print("Can't get token for", username)

Ich habe die Anwendung bei developer.spotify.com/my-applications registriert und client_id und client_secret erhalten. Ich bin ein bisschen unklar über die Auswahl von redirect_uri, also setze ich das auf 'https: //play.spotify.com/collection/song'

Beim Ausführen dieses Befehls vom Terminal erhalte ich die Fehlermeldung:

You need to set your Spotify API credentials. You can do this by
setting environment variables like so:
export SPOTIPY_CLIENT_ID='your-spotify-client-id'
export SPOTIPY_CLIENT_SECRET='your-spotify-client-secret'
export SPOTIPY_REDIRECT_URI='your-app-redirect-url'

Ich füge das in meinen Code mit der ID, dem Geheimnis und der URL als Zeichenfolge ein, nur nach dem Import, aber oberhalb der Methode util.prompt_for_user_token.

Das hat einen Traceback verursacht:

File "add-track.py", line 8
export SPOTIPY_CLIENT_ID='4f...6'
                       ^
SyntaxError: invalid syntax

Mir ist aufgefallen, dass Text Wrangler 'export' nicht als spezielles Wort erkennt. Und ich suchte in docs.python.org nach 'export' und fand nichts hilfreiches. Was ist Export? Wie verwende ich es falsch?

Ich habe als nächstes versucht, client_id, client_secret und redirect_uri als Argumente in der util.prompt_for_user_token-Methode zu übergeben:

util.prompt_for_user_token(username,scope,client_id='4f...6',client_secret='xxx...123',redirect_uri='https://play.spotify.com/collection/songs')

Wenn ich das ausprobiert habe, passiert Folgendes im Terminal:

User authentication requires interaction with your
        web browser. Once you enter your credentials and
        give authorization, you will be redirected to
        a url.  Paste that url you were directed to to
        complete the authorization.


Opening https://accounts.spotify.com/authorize?scope=user-library-modify&redirect_uri=https%3A%2F%2Fplay.spotify.com%2Fcollection%2Fsongs&response_type=code&client_id=4f...6 in your browser


Enter the URL you were redirected to: 

Ich trat einhttps: //play.spotify.com/collection/song und bekam dann diesen Traceback:

Traceback (most recent call last):
File "add-track.py", line 21, in <module>
token = util.prompt_for_user_token(username, scope, client_id='4f...6', client_secret='xxx...123', redirect_uri='https://play.spotify.com/collection/songs')
File "/Library/Python/2.7/site-packages/spotipy/util.py", line 86, in prompt_for_user_token
token_info = sp_oauth.get_access_token(code)
File "/Library/Python/2.7/site-packages/spotipy/oauth2.py", line 210, in get_access_token
raise SpotifyOauthError(response.reason)
spotipy.oauth2.SpotifyOauthError: Bad Request

Es scheint, als würde mir etwas fehlen, vielleicht muss ein anderer Teil von Spotipy importiert werden oder ein anderes Python-Modul. Es scheint, ich vermisse das Stück, das Client-Anmeldeinformationen setzt. Wie mache ich das? Ich bin ziemlich neu darin (wenn das nicht offensichtlich war). Bitte helfen Sie.

UPDATE: Ich habe redirect_uri in localhost: 8888 / callback geändert. Das führt dazu, dass eine Firefox-Registerkarte mit dem Fehler "Verbindung zum Server kann nicht hergestellt werden" geöffnet wird. (Da auf meinem Computer kein Server ausgeführt wird, habe ich mir überlegt, node.js wie im Spotify-Web-API-Lernprogramm zu installieren, dies jedoch noch nicht.) Das Python-Skript fordert mich dann auf, die URL, zu der ich umgeleitet wurde, zu kopieren und einzufügen. Obwohl FF keine Seite öffnen konnte, funktionierte dies, indem ich das @ -Zeichen kopiert gesamtes URL einschließlich "code = BG ..." nach localhost: 8888 / callback? Ich bin mir nicht sicher, ob dies ein ideales Setup ist, aber es funktioniert zumindest.

Ist es wichtig, ob ich node.js eingerichtet habe oder nicht?