streamline authentication (closes #376, closes #380)

This commit is contained in:
sigma67 2023-04-19 20:45:50 +02:00
commit e457402633
No known key found for this signature in database
GPG key ID: 1C314227DF0103D0
4 changed files with 245 additions and 177 deletions

View file

@ -1,12 +1,27 @@
import json
import time
from typing import Dict, Optional
import requests
import json
from ytmusicapi.constants import OAUTH_CLIENT_ID, OAUTH_CLIENT_SECRET, OAUTH_SCOPE, OAUTH_CODE_URL, OAUTH_TOKEN_URL, OAUTH_USER_AGENT
import requests
from requests.structures import CaseInsensitiveDict
from ytmusicapi.constants import (OAUTH_CLIENT_ID, OAUTH_CLIENT_SECRET,
OAUTH_CODE_URL, OAUTH_SCOPE, OAUTH_TOKEN_URL,
OAUTH_USER_AGENT)
from ytmusicapi.helpers import initialize_headers
def is_oauth(headers: CaseInsensitiveDict) -> bool:
oauth_structure = {
"access_token",
"expires_at",
"expires_in",
"token_type",
"refresh_token",
}
return oauth_structure.issubset(headers.keys())
class YTMusicOAuth:
"""OAuth implementation for YouTube Music based on YouTube TV"""
@ -26,22 +41,25 @@ class YTMusicOAuth:
return response_json
def get_token_from_code(self, device_code: str) -> Dict:
token_response = self._send_request(OAUTH_TOKEN_URL,
data={
"client_secret": OAUTH_CLIENT_SECRET,
"grant_type":
"http://oauth.net/grant_type/device/1.0",
"code": device_code
})
token_response = self._send_request(
OAUTH_TOKEN_URL,
data={
"client_secret": OAUTH_CLIENT_SECRET,
"grant_type": "http://oauth.net/grant_type/device/1.0",
"code": device_code,
},
)
return token_response.json()
def refresh_token(self, refresh_token: str) -> Dict:
response = self._send_request(OAUTH_TOKEN_URL,
data={
"client_secret": OAUTH_CLIENT_SECRET,
"grant_type": "refresh_token",
"refresh_token": refresh_token
})
response = self._send_request(
OAUTH_TOKEN_URL,
data={
"client_secret": OAUTH_CLIENT_SECRET,
"grant_type": "refresh_token",
"refresh_token": refresh_token,
},
)
return response.json()
@staticmethod