简体   繁体   English

授权代码流中的错误请求 Spotify

[英]Bad Request at Authorization Code Flow Spotify

I am trying to built a script that creates a playlist on a user's spotify profile.我正在尝试构建一个在用户的 Spotify 个人资料上创建播放列表的脚本。 To learn spotipy I decided to try the examples they have on the documentation page.为了学习 spotipy,我决定尝试他们在文档页面上提供的示例。

The code I run is:我运行的代码是:

import sys
import spotipy
import spotipy.util as util

token = util.prompt_for_user_token('idxxx',
                           'user-library-read',
                           client_id='axxx',
                           client_secret='Bxxx',
                           redirect_uri='http://localhost')

scope = 'user-library-read'

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

token = util.prompt_for_user_token(username, scope)

if token:
    sp = spotipy.Spotify(auth=token)
    results = sp.current_user_saved_tracks()
    for item in results['items']:
        track = item['track']
        print(track['name'] + ' - ' + track['artists'][0]['name'])
else:
    print("Can't get token for", username)

The problem occurs when i run the code.当我运行代码时会出现问题。 I get redirected on my redirect uri and after i paste it back on the terminal i get this:我在我的重定向 uri 上被重定向,在我将它粘贴回终端后,我得到了这个:

Traceback (most recent call last):
  File "spot01.py", line 9, in <module>
    redirect_uri='http://localhost')
  File "/home/user/.local/lib/python3.6/site-packages/spotipy/util.py", line 92, in prompt_for_user_token
    token = sp_oauth.get_access_token(code, as_dict=False)
  File "/home/user/.local/lib/python3.6/site-packages/spotipy/oauth2.py", line 434, in get_access_token
    raise SpotifyOauthError(response.reason)
spotipy.oauth2.SpotifyOauthError: Bad Request

I tried to access the oauth2.py from the File Manager and the terminal but it says that this repository does not exist.我试图从文件管理器和终端访问 oauth2.py,但它说这个存储库不存在。 Also i tried to install spotipy through the github page they have where the neccessary files exist but still nothing.我还尝试通过 github 页面安装 spotipy,他们在其中存在必要的文件但仍然没有。

Any ideas?有任何想法吗? Thanks a lot.非常感谢。

I solved the problem by downloading the required files from here https://github.com/plamere/spotipy/tree/master/spotipy我通过从这里下载所需文件解决了这个问题https://github.com/plamere/spotipy/tree/master/spotipy

Then I changed some things inside of each.py file and i run every code I wanted to inside there.然后我更改了 each.py 文件中的一些内容,并在其中运行了我想要的所有代码。 It must be another more fancy solution but this one worked for me.这一定是另一种更花哨的解决方案,但这个对我有用。

Oh before running any command I typed this set of commands on the terminal:哦,在运行任何命令之前,我在终端上输入了这组命令:

$ bash
$ export SPOTIPY_CLIENT_ID='xxx'
$ export SPOTIPY_CLIENT_SECRET='xxx'
$ export SPOTIPY_REDIRECT_URI='http://localhost/'

where xxx you put your credentials and at the refirect uri your localhost or your github profile link.在哪里 xxx 你把你的凭据和 refirect uri 你的本地主机或你的 github 个人资料链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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