Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.92 KB | None | 0 0
  1. import requests
  2. import json
  3.  
  4.  
  5. def get_token():
  6.     resp = requests.post('https://accounts.spotify.com/api/token',
  7.                          data={'grant_type': 'client_credentials'},
  8.                          auth=('773f0a75f1d040fe96d761d484301b17', '91dea44acb994849bdf8565391e8d8c7'))
  9.     resp = str(resp.content, encoding='utf-8')
  10.     return json.loads(resp)['access_token']
  11.  
  12.  
  13. def main():
  14.     token = get_token()
  15.     data = get_search_data(token, 'where is my mind', 'playlist')
  16.     print(data)
  17.  
  18.  
  19. def get_search_data(token, query, search_type):
  20.     headers = {'Authorization': 'Bearer ' + token}
  21.     resp = requests.get('https://api.spotify.com/v1/search?q={}&type={}'.format(query, search_type), headers=headers)
  22.     if resp.status_code != 200:
  23.         print('bad response code {}'.format(resp))
  24.     data = json.loads(str(resp.content, encoding='utf-8'))
  25.     return data
  26.  
  27.  
  28. if __name__ == '__main__':
  29.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement