简体   繁体   中英

Why do I keep getting a 400 Bad Request Error when exchanging code for access token from Spotify API?

import requests
import base64
from secrets import USER_CLIENT_ID, USER_CLIENT_SECRET, USER_REDIRECT_URI

# using OAuth we create a link to redirect user to their spotify account
def create_oauth_link():
    params = {
        "client_id": USER_CLIENT_ID,
        "response_type": "code",
        "redirect_uri": USER_REDIRECT_URI,
        "scope": "user-read-private user-read-email"
    }
    endpoint = "https://accounts.spotify.com/authorize"
    response = requests.get(endpoint, params=params)
    url = response.url
    return url

# authorization process to exchange code for token
def exchange_code_token(code=None):
    message = f"{USER_CLIENT_ID}:{USER_CLIENT_SECRET}"
    messageBytes = message.encode("ascii")
    base64Bytes = base64.b64encode(messageBytes)
    base64Message = base64Bytes.decode("ascii")
    headers = {'Authorization': f'Basic {base64Message}'}
    params = {
        'grant_type': "authorization_code",
        "code": code,
        "redirect_uri": USER_REDIRECT_URI,
        #"client_id": USER_CLIENT_ID,
        #"client_secret": USER_CLIENT_SECRET,
        }
    endpoint = "https://accounts.spotify.com/api/token"
    response = requests.post(endpoint, params=params, headers=headers)
    print(response.reason)

link = create_oauth_link()
print(f"Follow the link to start the authentication with Spotify: {link}")
code = input("Spotify Code: ")
exchange_code_token(code)

I'm generating the code successfully but everything goes wrong while trying to exchange it for the access token. I'm getting a bad request response. I have tried passing client_id and client_secret through request parameters as per Spotify's documentation as well through base64 encoding but nothing seems to work. What could be the issue?

aren't client_id and client_secret normally not together in one OAuth Request? In addition, sometimes you need a local token.txt which will be created once you logged_in manually over a website request. This.txt. contains an additional ACCESS TOKEN. Thats where your problem seems to be. This code should redirect you to a spotify page (in case you created your app in spotify) and should ask you to take action (hit a button ore something like that) than your token.txt. will be created in your folder. If not create it yourself.

Here is something with spotipy I once wrote, to create my own Top-100 music list scraped from a website and searched in spotify. You are invited to copy the OAuth strategy:

import spotipy
from spotipy.oauth2 import SpotifyOAuth
import requests
from bs4 import BeautifulSoup

client_id = "your id"
client_secret = "your secret"
time_travel = input("Which year you want to travel to? Insert a format of YYYY-MM-DD: ")

response = requests.get(url=f"https://www.billboard.com/charts/hot-100/{time_travel}")
time_travel = time_travel.split("-")[0]
soup = BeautifulSoup(response.text, "lxml")

interpret = soup.find_all(name="span",
                          class_="chart-element__information__artist text--truncate color--secondary")

title = soup.find_all(name="span",
                      class_="chart-element__information__song text--truncate color--primary")

top_100_interpret = [element.string for element in interpret]
top_100_title = [element.string for element in title]

sp = spotipy.Spotify(
        auth_manager=SpotifyOAuth(
        scope="playlist-modify-private playlist-modify-public",
        redirect_uri="http://localhost:8888/callback",
        client_id=client_id,
        client_secret=client_secret,
        show_dialog=True,
        cache_path="token.txt")
)

uris_artists = []
found_spotify_tracks = []

#search artist
#for artist in top_100_interpret[:10]:
for artist in top_100_interpret:
    try:
        result = sp.search(q=f"artist:{artist} year:{time_travel}", type="artist")
        uri_artist = result["artists"]["items"][0]["uri"]

        #search top ten 10 of artist
        tracks = [sp.artist_top_tracks(uri_artist, country="US")["tracks"][_]["name"] for _ in range(10)]
        tracks_uri = [sp.artist_top_tracks(uri_artist, country="US")["tracks"][_]["uri"] for _ in range(10)]
        found_track = [track in top_100_title for track in tracks]
        index_found_spotify = found_track.index(True)
    except:
        uri_artist = ""
        tracks = ""
        print("Artist or Song not found")
    else:
        found_spotify_tracks.append(tracks_uri[index_found_spotify])


def create_playlist() -> str:
    playlist_name = f"Top 100 in {time_travel}"
    user_id = sp.current_user()["id"]
    playlist_dict = sp.user_playlist_create(user_id,
                         playlist_name,
                         public=True,
                         collaborative=False,
                         description='Auto generated Playlist with Python, if track found')
    return playlist_dict


def add_to_playlist(id_name: str, uris: list) -> None:
    sp.playlist_add_items(id_name, uris, position=None)


playlist_dict = create_playlist()
add_to_playlist(playlist_dict["uri"], found_spotify_tracks)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM