Python: Spotify API Post call? -
using python, i'm trying post call spotify api following instructions under paragraph client credentials flow @ link https://developer.spotify.com/web-api/authorization-guide/#client_credentials_flow , code have come with. however, response [415] when run it. can tell me wrong?
import pprint import requests import urllib2 import json import base64 client_id='b040c4e03217489aa647c055265d0ac' client_secret='2c2928bb7d3e43278424002d2e8bda46b' authorization_param='basic ' + base64.standard_b64encode(client_id + ':' + client_secret) grant_type='client_credentials' #request based on client credentials flow https://developer.spotify.com/web-api/authorization-guide/ #header must base 64 encoded string contains client id , client secret key. #the field must have format: authorization: basic <base64 encoded client_id:client_secret> header_params={'authorization' : authorization_param} #request body parameter: grant_type value: required. set client_credentials body_params = {'grant_type' : grant_type} url='https://accounts.spotify.com/api/token' response=requests.post(url, header_params, body_params) # post request takes both headers , body parameters print response
the type of authentication spotify requesting basic http authentication. standardised form of authentication can read more here. requests library supports basic authentication without requiring create headers yourself. see python requests documentation information.
the code below uses request library authentication connect spotify api.
import requests client_id = # enter client id here client_secret = # enter client secret here grant_type = 'client_credentials' #request based on client credentials flow https://developer.spotify.com/web-api/authorization-guide/ #request body parameter: grant_type value: required. set client_credentials body_params = {'grant_type' : grant_type} url='https://accounts.spotify.com/api/token' response=requests.post(url, data=body_params, auth = (client_id, client_secret)) print response
i created test account spotify , created test client id , client secret worked find this. got response 200 using python 2.7.6 , requests 2.2.1.
Comments
Post a Comment