I am trying to use Spotify's API to get song details. My programming language is python and the code I am trying to execute is this:
def spotifysearch(searchterm):
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Bearer OAUTH TOKEN',
}
params = (
('q', searchterm),
('type', 'track'),
('limit', '10'),
)
response = requests.get('https://api.spotify.com/v1/search', headers=headers, params=params)
a=response.json()
return a
However, my OAUTH TOKEN expires after every hour and I can't just manually get a new token every hour. I couldn't understand how to get my refresh token automatically.
Please provide me answer in either python or on curl (Both are fine with me)
Your first step will be to record:
when you got the token
when the token will expire
You can use dataclasses for this. Dataclasses have a post_init func, which is executed
after initialization. You should compute the expiry as a datetime there.
import dataclasses
#dataclasses.dataclass
class TToken:
access_token: str
refresh_token: str
created: datetime.datetime
expires: datetime.datetime
def __init_post__(self, data):
if data: # Not required
self.access_token = data['access_token']
self.created = datetime.datetime.utcnow()
self.expires = self.created + datetime.timedelta(minutes=59)
self.refresh_token = data['refresh_token']
def is_expired(self):
return datetime.datetime.utcnow() > self.expires
def as_header(self):
return {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': f'Bearer self.access_token',
}
For your tokens.
BASE64_CID_CSEC = <base64 encoded clientid:clientsecret>
def token_get(authorization_code: str):
data = {'form':{'grant_type': 'authorization_code',
'code': authorization_code,
'redirect_uri': <your redirect uri>},
'header':{'Authorization': f'Basic {BASE64_CID_CSEC}'}}
resp = requests.get('https://accounts.spotify.com/api/token', data=data['form'], headers=data['header'])
return TToken(resp)
def token_refresh(refresh_token: str):
data = {'form': {'grant_type': 'refresh_token', 'refresh_token': refresh_token},
'header': {'Authorization': f'Basic {BASE64_CID_CSEC}'}}
resp = requests.get('https://accounts.spotify.com/api/token', data=data['form'], headers=data['header'])
return TToken(resp)
Finally
token = token_get(authorization_code)
def spotify_search(searchterm: str):
if token.is_expired():
token = token_refresh(token.refresh_token)
params = (('q', searchterm),('type', 'track'),('limit', '10'))
resp = requests.get('https://api.spotify.com/v1/search', headers=token.as_header(), params=params)
return resp.json()
Related
Using Python, how do I make a request to the shopee API to get a list of products on offer with my affiliate link?
I've made several scripts, but they all have a signature issue or unsupported authentication attempt. Does anyone have a working example of how to do this?
Below are two code examples I made, but they don't work.
Query: productOfferV2 and shopeeOfferV2
code1:
import requests
import time
import hashlib
appID = '18341090114'
secret = 'XMAEHHWQD3OEGQX5P33AFRREJEDSQX76'
# Set the API endpoint URL
url = "https://open-api.affiliate.shopee.com.my/graphql"
payload = """
{
"query": "query Fetch($page:2){
productOfferV2(
listType: 0,
sortType: 2,
page: $page,
limit: 50
) {
nodes {
commissionRate
commission
price
productLink
offerLink
}
}
}",
"operationName": null,
"variables":{
"page":0
}
}
"""
payload = payload.replace('\n', '').replace(':0', f':{2}')
timestamp = int(time.time())
factor = appID+str(timestamp)+payload+secret
signature = hashlib.sha256(factor.encode()).hexdigest()
# Set the request headers
headers = {
'Content-type': 'application/json',
'Authorization': f'SHA256 Credential={appID},Timestamp={timestamp},Signature={factor}'
}
# Send the POST request
response = requests.post(url, payload, headers=headers)
data = response.json()
print(data)
return = error-invalid-signature-python
code2:
import requests
import json
import hmac
import hashlib
appID = '18341090114'
secret = 'XMAEHHWQD3OEGQX5P33AFRREJEDSQX76'
query = '''query { productOfferV2(item_id: ALL) {
offers {
shop_id
item_price
discount_price
offer_id
shop_location
shop_name
}
}
}'''
def generate_signature(query, secret):
signature = hmac.new(secret.encode(), query.encode(), hashlib.sha256).hexdigest()
return signature
signature = generate_signature(query, secret)
url = 'https://open-api.affiliate.shopee.com.my/graphql'
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Bearer ' + appID + ':' + signature
}
payload = {
'query': query
}
response = requests.post(url, data=json.dumps(payload), headers=headers)
data = response.json()
print(data)
I am trying to send a POST request to Walmart API to request reports. But it is returning me 'Report request failed.' message with json. What am I doing wrong? I don't really have that much experience with API calls in python. Here is my code.
I have a class called "Walmart" to interact with the Walmart Marketplace API. It has methods to authenticate with the API, send requests and handle responses.
class Walmart(object):
def __init__(self, client_id, client_secret):
"""To get client_id and client_secret for your Walmart Marketplace
visit: https://developer.walmart.com/#/generateKey
"""
self.client_id = client_id
self.client_secret = client_secret
self.token = None
self.token_expires_in = None
self.base_url = "https://marketplace.walmartapis.com/v3"
session = requests.Session()
session.headers.update({
"WM_SVC.NAME": "Walmart Marketplace",
"WM_QOS.CORRELATION_ID": uuid.uuid4().hex,
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "application/json",
})
session.auth = HTTPBasicAuth(self.client_id, self.client_secret)
self.session = session
# Get the token required for API requests
self.authenticate()
def authenticate(self):
data = self.send_request(
"POST", "{}/token".format(self.base_url),
body={
"grant_type": "client_credentials",
},
)
self.token = data["access_token"]
self.token_expires_in = data["expires_in"]
self.session.headers["WM_SEC.ACCESS_TOKEN"] = self.token
#property
def report(self):
return Report(connection=self)
def send_request(
self, method, url, params=None, body=None, json=None,
request_headers=None
):
# A unique ID which identifies each API call and used to track
# and debug issues; use a random generated GUID for this ID
headers = {
"WM_QOS.CORRELATION_ID": uuid.uuid4().hex,
"WM_SVC.NAME": "Walmart Marketplace",
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "application/json",
}
if request_headers:
headers.update(request_headers)
response = None
if method == "GET":
response = self.session.get(url, params=params, headers=headers)
elif method == "PUT":
response = self.session.put(
url, params=params, headers=headers, data=body
)
elif method == "POST":
request_params = {
"params": params,
"headers": headers,
}
if json is not None:
request_params["json"] = json
else:
request_params["data"] = body
response = self.session.post(url, **request_params)
if response is not None:
try:
response.raise_for_status()
except requests.exceptions.HTTPError:
if response.status_code == 401:
raise WalmartAuthenticationError((
"Invalid client_id or client_secret. Please verify "
"your credentials from https://developer.walmart."
"com/#/generateKey"
))
elif response.status_code == 400:
data = response.json()
if "error" in data and data["error"][0]["code"] == \
"INVALID_TOKEN.GMP_GATEWAY_API":
# Refresh the token as the current token has expired
self.authenticate()
return self.send_request(
method, url, params, body, request_headers
)
raise
try:
return response.json()
except ValueError:
# In case of reports, there is no JSON response, so return the
# content instead which contains the actual report
return response.content
And here goes authentication and the request itself. I think I am doing it wrong with the send_request method, should I do it in a different way?
api_key = '<key>'
api_secret='<secret>'
wm = Walmart(api_key, api_secret)
wm.authenticate()
url = "https://marketplace.walmartapis.com/v3/reports/reportRequests"
headers = {
"WM_QOS.CORRELATION_ID": uuid.uuid4().hex,
"WM_SVC.NAME": "Walmart Marketplace",
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "application/json",
}
data= {
"reportType": "ITEM_PERFORMANCE",
"reportVersion": "v1",
}
method="POST"
response_dict = wm.send_request(method, url, request_headers=headers, params=data)
if 'status_code' in response_dict and response_dict['status_code'] == 200:
response_json = response_dict.get('json')
request_id = response_json.get('requestId')
print(f'Report request submitted. Request ID: {request_id}')
else:
print('Report request failed.')
if 'json' in response_dict:
print(response_dict['json'])
else:
print(response_dict)
The response that I got was in the following form.
Report request failed.
{'requestId': '46a864e8-80e8-4019-86f0-d7a1575349a4', 'requestStatus': 'RECEIVED', 'requestSubmissionDate': '2023-02-15T18:55:03Z', 'reportType': 'ITEM_PERFORMANCE', 'reportVersion': 'v1'}
Any help is appreciated
You can try to print the response to see his contents, and there may be some additional info that will help you to fix your code. Also, you can try to debug your code line by line using this https://pypi.org/project/ipdb/
The response you get seems to be a successful response, as the property requestStatus is RECEIVED, and not ERROR (which is a possible value according to the API docs you linked).
So, the issue is probably with your response checks.
Based on your checks for response:
if 'status_code' in response_dict and response_dict['status_code'] == 200:
response_json = response_dict.get('json')
request_id = response_json.get('requestId')
print(f'Report request submitted. Request ID: {request_id}')
else:
print('Report request failed.')
if 'json' in response_dict:
print(response_dict['json'])
else:
print(response_dict)
either 'status_code' in response_dict or response_dict['status_code'] == 200 is false, what makes the else block to be executed. I recommend you to print(response_dict) before the if-else block, to see the whole content and see which of those 2 conditions is false and handle it accordingly.
I think that the issue is that the object you get from wm.send_request() does not contain status_code, since you get the content of the response (when you return response.json()) and not the session.Response object from the requests lib (see the docs).
I am trying to use pagination the way it is instructed in the Pinterest API Documentation, by passing 'bookmark' as a parameter to the next GET request in order to get the next batch of data.
However, the data returned is the EXACT same as the initial data I had received (without passing 'bookmark') and the value of 'bookmark' is also the same!
With this issue present, I keep receiving the same data over and over and can't get the entirety of the data. In my case I'm trying to list all campaigns.
Here is my python code:
url = f'https://api.pinterest.com/v5/ad_accounts/{ad_account_id}/campaigns'
payload = f"page_size=25"
headers = {
"Accept": "text/plain",
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": f"Bearer {access_token}"
}
response = requests.request("GET", url, data=payload, headers=headers)
print(response)
feed = response.json()
print(feed)
bookmark=''
if 'bookmark' in feed:
bookmark = feed['bookmark']
print(bookmark)
while(bookmark != '' and bookmark != None and bookmark != 'null'):
url = f'https://api.pinterest.com/v5/ad_accounts/{ad_account_id}/{level}s'
payload = f"page_size=25&bookmark={bookmark}"
headers = {
"Accept": "text/plain",
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": f"Bearer {access_token}"
}
response = requests.request("GET", url, data=payload, headers=headers)
print(response)
feed = response.json()
print(feed)
bookmark = feed['bookmark']
print(bookmark)
I think your condition in while is wrong, therefore you end up in the same loop. I'm currently also working with Pinterest API and below is my modified implementation how to get a list of ad accounts.
Basically you're testing if the bookmark is None. If yes, then you can return the result, otherwise you append the bookmark into query parameters and call the endpoint again.
from app.api.http_client import HttpClient
class PinterestAccountsClient():
def get_accounts(self, credentials) -> list[dict]:
headers = {
'Authorization': f"Bearer {credentials('access_token')}"
}
params = {
'page_size': 25
}
accounts = []
found_last_page = False
while not found_last_page:
try:
response = HttpClient.get(self.listing_url, headers=headers, params=params)
items = response.get('items', [])
bookmark = response.get('bookmark')
if bookmark is None:
found_last_page = True
else:
params['bookmark'] = bookmark
accounts.extend([{
'id': account['id'],
'name': account['name']
} for account in items])
return accounts
I am sending the post request to the TAP PAYMENT GATEWAY in order to save the card, the url is expecting two parameters like one is the source (the recently generated token) and inside the url the {customer_id}, I am trying the string concatenation, but it is showing the error like Invalid JSON request.
views.py:
ifCustomerExits = CustomerIds.objects.filter(email=email)
totalData = ifCustomerExits.count()
if totalData > 1:
for data in ifCustomerExits:
customerId = data.customer_id
print("CUSTOMER_ID CREATED ONE:", customerId)
tokenId = request.session.get('generatedTokenId')
payload = {
"source": tokenId
}
headers = {
'authorization': "Bearer sk_test_**********************",
'content-type': "application/json"
}
# HERE DOWN IS THE url of TAP COMPANY'S API:
url = "https://api.tap.company/v2/card/%7B"+customerId+"%7D"
response = requests.request("POST", url, data=payload, headers=headers)
json_data3 = json.loads(response.text)
card_id = json_data3["id"]
return sponsorParticularPerson(request, sponsorProjectId)
Their expected url = https://api.tap.company/v2/card/{customer_id}
Their documentation link: https://tappayments.api-docs.io/2.0/cards/create-a-card
Try this..
First convert dict. into JSON and send post request with request.post:
import json
...
customerId = str(data.customer_id)
print("CUSTOMER_ID CREATED ONE:", customerId)
tokenId = request.session.get('generatedTokenId')
payload = {
'source': tokenId
}
headers = {
'authorization': "Bearer sk_test_**************************",
'content-type': "application/json"
}
pd = json.dumps(payload)
# HERE DOWN IS THE url of TAP COMPANY'S API:
url = "https://api.tap.company/v2/card/%7B"+customerId+"%7D"
response = requests.post(url, data=pd, headers=headers)
json_data3 = json.loads(response.text)
card_id = json_data3["id"]
return sponsorParticularPerson(request, card_id)
Please tell me this works or not...
I'm trying to GET Microsoft calendar events. Default timezone of my outlook account is US Eastern Time Zone. But the response I'm getting from rest api call is all in UTC. How can I get my default time zone i.e. US Eastern Time?
Here is my code:
def make_api_call(method, url, token, payload = None, parameters = None):
headers = { 'User-Agent' : 'python_tutorial/1.0',
'Authorization' : 'Bearer {0}'.format(token),
'Accept' : 'application/json'}
request_id = str(uuid.uuid4())
instrumentation = { 'client-request-id' : request_id,
'return-client-request-id' : 'true' }
headers.update(instrumentation)
response = None
if (method.upper() == 'GET'):
response = requests.get(url, headers = headers, params = parameters)
elif (method.upper() == 'POST'):
headers.update({ 'Content-Type' : 'application/json' })
response = requests.post(url, headers = headers, data = json.dumps(payload), params = parameters)
return response
def get_my_events(access_token, start_date_time, end_date_time):
get_events_url = graph_endpoint.format('/me/calendarView')
query_parameters = {'$top': '10',
'$select': 'subject,start,end,location',
'$orderby': 'start/dateTime ASC',
'startDateTime': start_date_time,
'endDateTime': end_date_time}
r = make_api_call('GET', get_events_url, access_token, parameters = query_parameters)
if (r.status_code == requests.codes.ok):
return r.json()
else:
return "{0}: {1}".format(r.status_code, r.text)
UPDATE:
Anyone else coming here for this kind of question, you need to update the headers to send any specific time zone. Here are the update headers, make sure you surround timezone in double quotes:
headers = { 'User-Agent' : 'python_tutorial/1.0',
'Authorization' : 'Bearer {0}'.format(token),
'Accept' : 'application/json',
'Prefer': 'outlook.timezone="Eastern Standard Time"'}
You need to specify the time zone using the Prefer: outlook.timezone header.
From the documentation:
Prefer: outlook.timezone
Use this to specify the time zone for start and end times in the response. If not specified, those time values are returned in UTC.
For example, to set it to US Eastern you would send
Prefer: outlook.timezone="Eastern Standard Time"