简体   繁体   English

Python Spotify OAuth流程

[英]Python Spotify OAuth Flow

So I'm trying to build an authorization flow with with the Spotify Web API. 因此,我正在尝试使用Spotify Web API构建授权流程。 So the first call is just a GET against a URL and I'm passing the credentials and other stuff necessary to authorize my app as parameters. 因此,第一个调用只是针对URL的GET,我正在传递凭据和其他必要的东西来授权我的应用程序为参数。 After that the user is prompted to authorize my app then is redirected to a callback URL. 之后,提示用户授权我的应用程序,然后将其重定向到回调URL。

The url I'm redirected to contains some response data as parameters and I need that data to then POST against the API and retrieve the token. 我重定向到的url包含一些响应数据作为参数,我需要该数据然后针对API进行POST并检索令牌。

How do I retrieve the response URL and access those parameters? 如何检索响应URL并访问这些参数? Sorry for the naive question. 很抱歉这个幼稚的问题。 Thanks in advance. 提前致谢。

I don't believe you can do this with just requests. 我不相信您可以仅凭请求就可以做到这一点。

I would check out a package like: https://flask-oauthlib.readthedocs.org/en/latest/ 我会签出一个像这样的包: https : //flask-oauthlib.readthedocs.org/en/latest/

from flask import Flask, redirect, url_for, session, request
from flask_oauthlib.client import OAuth, OAuthException


SPOTIFY_APP_ID = 'REGULAR_CODE'
SPOTIFY_APP_SECRET = 'SECRET_CODE'


app = Flask(__name__)
app.debug = True
app.secret_key = 'development'
oauth = OAuth(app)

spotify = oauth.remote_app(
    'spotify',
    consumer_key=SPOTIFY_APP_ID,
    consumer_secret=SPOTIFY_APP_SECRET,
    # Change the scope to match whatever it us you need
    # list of scopes can be found in the url below
    # https://developer.spotify.com/web-api/using-scopes/
    request_token_params={'scope': 'user-read-email'},
    base_url='https://accounts.spotify.com',
    request_token_url=None,
    access_token_url='/api/token',
    authorize_url='https://accounts.spotify.com/authorize'
)


@app.route('/')
def index():
    return redirect(url_for('login'))


@app.route('/login')
def login():
    callback = url_for(
        'spotify_authorized',
        next=request.args.get('next') or request.referrer or None,
        _external=True
    )
    return spotify.authorize(callback=callback)


@app.route('/login/authorized')
def spotify_authorized():
    resp = spotify.authorized_response()
    if resp is None:
        return 'Access denied: reason={0} error={1}'.format(
            request.args['error_reason'],
            request.args['error_description']
        )
    if isinstance(resp, OAuthException):
        return 'Access denied: {0}'.format(resp.message)

    session['oauth_token'] = (resp['access_token'], '')
    me = spotify.get('/me')
    return 'Logged in as id={0} name={1} redirect={2}'.format(
        me.data['id'],
        me.data['name'],
        request.args.get('next')
    )


@spotify.tokengetter
def get_spotify_oauth_token():
    return session.get('oauth_token')


if __name__ == '__main__':
    app.run()

You can GET a JSON response with all parameters that you need, example. 您可以使用所需的所有参数来获取JSON响应,例如。

You login to a www.example.com/login/ 您登录到www.example.com/login/

GET http://www.example.com/login/ (With header and Basic access authentication or the auth process that you use) and this URL return a JSON Response with the data: GET http://www.example.com/login/ (具有标头和基本访问身份验证或您使用的身份验证过程),并且此URL返回带有数据的JSON响应:

{
  'status': 'OK',
  'token': {
    'public_token': 'blabla',
    'private_token': 'blabla'},
  'created_at': '2014-11-03'
}

That data you can save in database for future use. 您可以将这些数据保存在数据库中以备将来使用。

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

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