When I try to run cretin modules in Spotipy I get an Insufficient client scope error message. Spotify gives a 403 client error saying "No Token Provided"
Tried multiple modules only a few work such as the current_user() module but others spit back the Insufficient client scope error.
Tried multiple modules only a few work such as the current_user() module but others spit back the Insufficient client scope error.
import spotipy
import os
import json
import sys
import webbrowser
import spotipy.util as util
from json.decoder import JSONDecodeError
username = sys.argv[0]
try:
token = util.prompt_for_user_token(username)
except:
os.remove(f".cache-{username}")
token = util.prompt_for_user_token(username)
spotify = spotipy.Spotify(auth=token)
albums = spotify.current_user_saved_albums(limit= 100, offset = 0)
userplaylist = spotify.current_user_playlists()
spuser= spotify.current_user()
class spotifyUser:
def __init__(self, userid, followers, playlist):
self.userid = userid
self.followers = followers
self.playlist = playlist
self.albums
#list all user playlists
def all_playlists(playlist):
for r in range(len(playlist['items'])):
print(playlist['items'][r]['name'])
currentuser = spotifyUser(spuser['display_name'],spuser['followers']
['total'], userplaylist)
#all_playlists(currentuser.playlist)
print(albums)
When ran results in:
Traceback (most recent call last):
File "C:\Users\cesse\PycharmProjects\Spotipy\venv\lib\site-packages\spotipy\client.py", line 119, in _internal_call
r.raise_for_status()
File "C:\Users\cesse\PycharmProjects\Spotipy\venv\lib\site-packages\requests\models.py", line 940, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: https://api.spotify.com/v1/me/albums?limit=100&offset=0
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Python37/codes/Testspotipy.py", line 20, in <module>
albums = spotify.current_user_saved_albums(limit= 100, offset = 0)
File "C:\Users\cesse\PycharmProjects\Spotipy\venv\lib\site-packages\spotipy\client.py", line 585, in current_user_saved_albums
return self._get('me/albums', limit=limit, offset=offset)
File "C:\Users\cesse\PycharmProjects\Spotipy\venv\lib\site-packages\spotipy\client.py", line 146, in _get
return self._internal_call('GET', url, payload, kwargs)
File "C:\Users\cesse\PycharmProjects\Spotipy\venv\lib\site-packages\spotipy\client.py", line 124, in _internal_call
headers=r.headers)
spotipy.client.SpotifyException: http status: 403, code:-1 - https://api.spotify.com/v1/me/albums?limit=100&offset=0:
Insufficient client scope
Pass an extra arg into your prompt for token call, scope as a string separated by spaces:
token = util.prompt_for_user_token(username, scope="user-library-read <etc>")
List of available Scopes
Related
I am using below python authentication script to connect to ADLS using service principal details but it keeps throwing exception:azure.core.exceptions.HttpResponseError: (AuthorizationPermissionMismatch) This request is not authorized to perform this operation using this permission.
The role assigned to me is "Storage blob data owner" and not sure what is missing?
Python Code:
from azure.storage.filedatalake import DataLakeServiceClient
from azure.identity import ClientSecretCredential
TENANT_ID = 'XXXXXXXXXX'
CLIENT_ID = 'XXXXXXXXXX'
CLIENT_SECRET = 'XXXXXXXXXX'
STORAGE_ACCOUNT_NAME = 'XXXXXXXXXX'
credential = ClientSecretCredential(TENANT_ID, CLIENT_ID, CLIENT_SECRET)
service_client = DataLakeServiceClient(account_url="{}://{}.dfs.core.windows.net".format(
"https", STORAGE_ACCOUNT_NAME), credential=credential)
print(service_client.primary_endpoint) # Can see the primary endpoint.
file_system_client = service_client.get_file_system_client("my-container")
file_system_client.create_directory("test-dir") #Throwing the (AuthorizationPermissionMismatch) error.
print("test directory created.")
Complete Trace:
Traceback (most recent call last):
File "/home//lib/python3.5/site-packages/azure/storage/filedatalake/_path_client.py", line 200, in _create
return self._client.path.create(**options)
File "/home/lib/python3.5/site-packages/azure/storage/filedatalake/_generated/operations/_path_operations.py", line 248, in create
raise HttpResponseError(response=response, model=error)
azure.core.exceptions.HttpResponseError: (AuthorizationPermissionMismatch) This request is not authorized to perform this operation using this permission.
RequestId:80605399-e01f-0038-2cd2-0a4210000000
Time:2021-02-24T17:25:49.0969802Z
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "adls_client.py", line 30, in <module>
file_system_client.create_directory("test-dir")
File "/home/lib/python3.5/site-packages/azure/storage/filedatalake/_file_system_client.py", line 540, in create_directory
directory_client.create_directory(metadata=metadata, **kwargs)
File "/home/lib/python3.5/site-packages/azure/storage/filedatalake/_data_lake_directory_client.py", line 160, in create_directory
return self._create('directory', metadata=metadata, **kwargs)
File "/home/lib/python3.5/site-packages/azure/storage/filedatalake/_path_client.py", line 202, in _create
process_storage_error(error)
File "/home/lib/python3.5/site-packages/azure/storage/filedatalake/_deserialize.py", line 150, in process_storage_error
raise error
azure.core.exceptions.HttpResponseError: (AuthorizationPermissionMismatch) This request is not authorized to perform this operation using this permission.
I can reproduce your error:
I am pretty sure your code is no problem, and, Storage blob data owner is the right RBAC role of your AD app. I think maybe the problem comes from the RBAC role does not take effect immediately, you need to wait a while. And then it should work.
I'm trying to get calendar data from Outlook, so I have the following code:
from O365 import Account, MSGraphProtocol
import datetime as dt
CLIENT_ID = 'f8384621-1095-4519-8999-0c7e616b7bc8'
SECRET_ID = '.s03m-t9u2q_9.Lpim5F.DTL-F1u4MBQy6'
credentials = (CLIENT_ID, SECRET_ID)
protocol = MSGraphProtocol()
#protocol = MSGraphProtocol(defualt_resource='<sharedcalendar#domain.com>')
scopes = ['User.Read']
account = Account(credentials, protocol=protocol)
if account.authenticate(scopes=scopes):
print('Authenticated!')
schedule = account.schedule()
calendar = schedule.get_default_calendar()
events = calendar.get_events(include_recurring=False)
# events = calendar.get_events(query=q, include_recurring=True)
for event in events:
print(event)
But unfortunately also getting these errors after successful authentication:
Client Error: 403 Client Error: Forbidden for url: https://graph.microsoft.com/v1.0/me/calendar | Error Message: Access is denied. Check credentials and try again.
Traceback (most recent call last):
File "C:/Users/username/Documents/pycharmproba/main.py", line 18, in <module>
calendar = schedule.get_default_calendar()
File "C:\Users\username\Documents\pycharmproba\venv\lib\site-packages\O365\calendar.py", line 1942, in get_default_calendar
response = self.con.get(url)
File "C:\Users\username\Documents\pycharmproba\venv\lib\site-packages\O365\connection.py", line 805, in get
return self.oauth_request(url, 'get', params=params, **kwargs)
File "C:\Users\username\Documents\pycharmproba\venv\lib\site-packages\O365\connection.py", line 794, in oauth_request
return self._internal_request(self.session, url, method, **kwargs)
File "C:\Users\username\Documents\pycharmproba\venv\lib\site-packages\O365\connection.py", line 756, in _internal_request
raise HTTPError('{} | Error Message: {}'.format(e.args[0], error_message), response=response) from None
requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: https://graph.microsoft.com/v1.0/me/calendar | Error Message: Access is denied. Check credentials and try again.
You haven't added the scopes to access your calendar events. Add the following scopes to your code and try again.
protocol = MSGraphProtocol()
scopes = ['User.Read']
calendar_scopes = protocol.get_scopes_for('calendar_all')
scopes.extend(calendar_scopes)
account = Account(credentials, protocol=protocol)
if account.authenticate(scopes=scopes):
print('Authenticated!')
I'm trying to interface Firebase with the official Admin SDK for Python (https://firebase.google.com/docs/database/admin/start).
However, I'm doing something wrong, as I'm not authorized somehow
This is my code:
import firebase_admin
from firebase_admin import credentials
from firebase_admin import db
# Fetch the service account key JSON file contents
cred = credentials.Certificate('./ServiceAccountKey.json')
# Initialize the app with a None auth variable, limiting the server's access
firebase_admin.initialize_app(cred, {
'databaseURL': 'https://*[database name]*.firebaseio.com',
'databaseAuthVariableOverride': None
})
# The app only has access to public data as defined in the Security Rules
ref = db.reference('/public_resource')
print(ref.get())
This is the error I get:
python3 firebase_test.py
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/firebase_admin/db.py", line 943, in request
return super(_Client, self).request(method, url, **kwargs)
File "/usr/local/lib/python3.6/site-packages/firebase_admin/_http_client.py", line 117, in request
resp.raise_for_status()
File "/usr/local/lib/python3.6/site-packages/requests/models.py", line 941, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://*[database name]*.firebaseio.com/public_resource.json?auth_variable_override=null
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "firebase_test.py", line 16, in <module>
print(ref.get())
File "/usr/local/lib/python3.6/site-packages/firebase_admin/db.py", line 222, in get
return self._client.body('get', self._add_suffix(), params=params)
File "/usr/local/lib/python3.6/site-packages/firebase_admin/_http_client.py", line 129, in body
resp = self.request(method, url, **kwargs)
File "/usr/local/lib/python3.6/site-packages/firebase_admin/db.py", line 945, in request
raise _Client.handle_rtdb_error(error)
firebase_admin.exceptions.UnauthenticatedError: Unauthorized request.
I'm using a Raspberry Pi 4 B with installed Python 3.6.
Can somebody point me into the right direction of why this happens and how to fix it?
Found it out!
Under database, go to rules and change it to:
{
/* Visit https://firebase.google.com/docs/database/security to learn more about security rules. */
"rules": {
".read": true,
".write": true
}
}
It was automatically set to false after I've waited for a too long period since my last access.
I'm trying to build an app using the spotipy python library to access the spotify api.
My oauth code looks like this and it seems to work except for initialising the client with the right auth parameter.
self.sp_auth=spotipy.oauth2.SpotifyOAuth(secrets.sp_auth_id,
secrets.sp_auth_pw, secrets.sp_callback_url,
scope="playlist-modify-public user-library-read", state=state)
...
url = self.sp_auth.get_authorize_url()
send url to user.
after user said she/he has given permission:
auth code is fetched from webserver and used to generate a token.
self.auth_token=self.sp_auth.get_access_token(self.auth_code)
self.auth_token then looks like this:
{'access_token' : 'BQD ... qE7K3PBZKB6iZFU3_4p',
'token_type' : 'Bearer',
'expires_in' : 3600,
'refresh_token' : 'AQCOS2Xo ... MK09ry7-a-fl61OwhuO1Q',
'scope' : 'playlist-modify-public user-library-read',
'expires_at' : 1548247835}
then I initialize the spotipy client module like this:
self.sp = spotipy.Spotify(auth=self.auth_token)
then I try the following:
playlists = self.sp.current_user_playlists(limit=10)
which raises this Exception:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/spotipy/client.py", line 119, in _internal_call
r.raise_for_status()
File "/usr/local/lib/python3.6/dist-packages/requests/models.py", line 940, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://api.spotify.com/v1/me/playlists?limit=10&offset=0
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/telegram/ext/dispatcher.py", line 279, in process_update
handler.handle_update(update, self)
File "/usr/local/lib/python3.6/dist-packages/telegram/ext/callbackqueryhandler.py", line 143, in handle_update
return self.callback(dispatcher.bot, update, **optional_args)
File "spotify_playlist_bot_v2.py", line 140, in button_auth_done
User.data[user_id].msg_start(bot, update)
File "spotify_playlist_bot_v2.py", line 84, in msg_start
self.msg_choose_playlist()
File "spotify_playlist_bot_v2.py", line 90, in msg_choose_playlist
playlists = self.sp.current_user_playlists(limit=10)
File "/usr/local/lib/python3.6/dist-packages/spotipy/client.py", line 355, in current_user_playlists
return self._get("me/playlists", limit=limit, offset=offset)
File "/usr/local/lib/python3.6/dist-packages/spotipy/client.py", line 146, in _get
return self._internal_call('GET', url, payload, kwargs)
File "/usr/local/lib/python3.6/dist-packages/spotipy/client.py", line 124, in _internal_call
headers=r.headers)
spotipy.client.SpotifyException: http status: 400, code:-1 - https://api.spotify.com/v1/me/playlists?limit=10&offset=0:
Only valid bearer authentication supported
It looks like I'm not passing the token to the spotipy client correctly. For example self.sp = spotipy.Spotify(auth="random_bullshit") gives me the same Exception. I also tried passing the token like this auth=self.auth_token['access_token'] with the same result. The documentation doesn't say anything about what the auth parameter should be exactly and I'm not really understanding the source code. But I'd say it suggests that auth=self.auth_token['access_token'] is the right thing to do.
Thanks!
As I already suggested in my last edit auth=self.auth_token['access_token'] was the right thing I just had a typo in it. Anyway since the Documentation doesn't say a lot about the auth parameter this might help some people.
When I run this simple code from Spotify's Docs:
import spotipy
birdy_uri = 'spotify:artist:2WX2uTcsvV5OnS0inACecP'
spotify = spotipy.Spotify()
results = spotify.artist_albums(birdy_uri, album_type='album')
albums = results['items']
while results['next']:
results = spotify.next(results)
albums.extend(results['items'])
for album in albums:
print(album['name'])
I got this exception:
Traceback (most recent call last):
File "/Users/haodong/Documents/Projects/python-workspace/env3/lib/python3.4/site-packages/spotipy/client.py", line 119, in _internal_call
r.raise_for_status()
File "/Users/haodong/Documents/Projects/python-workspace/env3/lib/python3.4/site-packages/requests/models.py", line 844, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://api.spotify.com/v1/search?q=artist%3ARadiohead&offset=0&type=artist&limit=10
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "test.py", line 19, in <module>
results = spotify.search(q='artist:' + name, type='artist')
File "/Users/haodong/Documents/Projects/python-workspace/env3/lib/python3.4/site-packages/spotipy/client.py", line 339, in search
return self._get('search', q=q, limit=limit, offset=offset, type=type, market=market)
File "/Users/haodong/Documents/Projects/python-workspace/env3/lib/python3.4/site-packages/spotipy/client.py", line 146, in _get
return self._internal_call('GET', url, payload, kwargs)
File "/Users/haodong/Documents/Projects/python-workspace/env3/lib/python3.4/site-packages/spotipy/client.py", line 124, in _internal_call
headers=r.headers)
spotipy.client.SpotifyException: http status: 401, code:-1 - https://api.spotify.com/v1/search?q=artist%3ARadiohead&offset=0&type=artist&limit=10:
No token provided
You need to use your Spotify app credentials (Client ID and Client Secret) from www.developer.spotify.com, assign it to a variable and use that as your object.
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
cid ="Your-client-ID"
secret = "Your-client-secret"
client_credentials_manager = SpotifyClientCredentials(client_id=cid, client_secret=secret)
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
#Then run your query
results = sp.artist_albums(birdy_uri, album_type='album'))
#### ETC ######
More info here: Client Credential Flow
It seems that Spotify Web API has been updated recently and requires authorization for all kinds of requests.
Use authorized requests would solve this problem.