Skip to content

Commit

Permalink
Fix spotify ssl issue (#2364)
Browse files Browse the repository at this point in the history
* patch spotify.py SSL with certifi

* remove ssl_context from global scope
  • Loading branch information
itsTheFae committed Dec 4, 2023
1 parent 330b111 commit dce4dad
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions musicbot/spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import asyncio
import asyncio.exceptions
import base64
import certifi
import logging
import ssl
import time

from .exceptions import SpotifyError
Expand Down Expand Up @@ -30,6 +32,7 @@ def __init__(self, client_id, client_secret, aiosession=None, loop=None):
self.client_secret = client_secret
self.guest_mode = client_id is None or client_secret is None

self.ssl_context = ssl.create_default_context(cafile=certifi.where())
self.aiosession = aiosession if aiosession else aiohttp.ClientSession()
self.loop = loop if loop else asyncio.get_event_loop()

Expand Down Expand Up @@ -66,7 +69,7 @@ async def make_spotify_req(self, url):

async def make_get(self, url, headers=None):
"""Makes a GET request and returns the results"""
async with self.aiosession.get(url, headers=headers) as r:
async with self.aiosession.get(url, headers=headers, ssl=self.ssl_context) as r:
if r.status != 200:
raise SpotifyError(
"Issue making GET request to {0}: [{1.status}] {2}".format(
Expand All @@ -77,7 +80,9 @@ async def make_get(self, url, headers=None):

async def make_post(self, url, payload, headers=None):
"""Makes a POST request and returns the results"""
async with self.aiosession.post(url, data=payload, headers=headers) as r:
async with self.aiosession.post(
url, data=payload, headers=headers, ssl=self.ssl_context
) as r:
if r.status != 200:
raise SpotifyError(
"Issue making POST request to {0}: [{1.status}] {2}".format(
Expand Down Expand Up @@ -136,7 +141,8 @@ async def request_guest_token(self):
"""Obtains a web player token from Spotify and returns it"""
try:
async with self.aiosession.get(
"https://open.spotify.com/get_access_token?reason=transport&productType=web_player"
"https://open.spotify.com/get_access_token?reason=transport&productType=web_player",
ssl=self.ssl_context,
) as r:
if r.status != 200:
try:
Expand Down

0 comments on commit dce4dad

Please sign in to comment.