- Python Programming Blueprints
- Daniel Furtado Marcus Pennington
- 288字
- 2021-06-24 18:53:49
Authenticating with Spotify's web API
Now that we have the code to load the configuration file for us, we are going to start coding the authentication part of our framework. Spotify currently supports three kinds of authentication: authorization code, client credentials, and implicitly grant. We are going to implement authorization code and client credentials in this chapter, and we will start by implementing the client credentials flow, which is the easiest to start with.
The client credentials flow has some disadvantages over the authorization code flow because the flow does not include authorization and cannot access the user's private data as well as control playback. We will implement and use this flow for now, but we will change to authorization code when we start implementing the terminal player.
First, we are going to create a file called authorization.py in the musicterminal/pytify/auth directory with the following contents:
from collections import namedtuple
Authorization = namedtuple('Authorization', [
'access_token',
'token_type',
'expires_in',
'scope',
'refresh_token',
])
This is going to be the authentication model and it will contain the data we get after requesting an access token. In the following list, you can see a description of every property:
- access_token: The token that has to be sent together with every request to the Web API
- token_type: The type of the token, which is usually Bearer
- expires_in: The access_token expiration time, which is 3600 seconds (1 hour)
- scope: The scope is basically the permissions that Spotify's user granted to our application
- refresh_token: The token that can be used to refresh the access_token after the expiration
The last touch is to create a __init__.py file in the musicterminal/pytify/auth directory and import the Authorization , which is a namedtuple:
from .authorization import Authorization
- Truffle Quick Start Guide
- 物聯(lián)網(wǎng)概論(第2版)
- 大話社交網(wǎng)絡(luò)
- 物聯(lián)網(wǎng)通信技術(shù)
- 網(wǎng)絡(luò)基礎(chǔ)與網(wǎng)絡(luò)管理項(xiàng)目化教程
- INSTANT KineticJS Starter
- 5G技術(shù)與標(biāo)準(zhǔn)
- 網(wǎng)絡(luò)AI+:2030后的未來(lái)網(wǎng)絡(luò)
- 高級(jí)網(wǎng)絡(luò)技術(shù)
- 5G時(shí)代的大數(shù)據(jù)技術(shù)架構(gòu)和關(guān)鍵技術(shù)詳解
- 工業(yè)以太網(wǎng)技術(shù):AFDX/TTE網(wǎng)絡(luò)原理、接口、互連與安全
- 物聯(lián)網(wǎng),So Easy!
- SEO攻略:搜索引擎優(yōu)化策略與實(shí)戰(zhàn)案例詳解
- Python Web Scraping Cookbook
- 網(wǎng)絡(luò)信息安全工程技術(shù)與應(yīng)用分析