cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Python SDK Library for RESTful APIs?

https://github.com/timotheus/ebaysdk-python

 

The library above was super helpful in getting started with the eBay's deprecated APIs. I'm using the Finding API.

 

I don't see an example SDK python library for the RESTful APIs. eBay is emailing me with their deprecation of the finding API and to move to the Browse API.

 

Is there a similar SDK for the RESTful APIs that I am unaware of? If so, could you post a link below?

 

If not, will there be a similar SDK for the RESTfuls?

Message 1 of 2
latest reply
1 REPLY 1

Python SDK Library for RESTful APIs?

@bamorris 

 

The API  examples for sending and receiving content should be fairly easy to follow.  The Browse API uses a query string that is somewhat similar to the Finding API, but you will need to look at filters differently.

https://developer.ebay.com/api-docs/buy/browse/resources/item_summary/methods/search 

 

The biggest difference between the Finding API and the Browse API will be the additional OAuth requirement for each request.

https://developer.ebay.com/api-docs/static/oauth-client-credentials-grant.html 

 

Your app will need to remint an OAuth token every two hours. There is a library here: 

https://github.com/eBay/ebay-oauth-python-client 

Since I don't use Python, I can't vouch for its ease of use.

 

I wrote an OAuth routine for PHP that I've been using for the past few years without issues:

https://community.ebay.com/t5/Token-Messaging-Sandbox-related/Sample-PHP-for-generating-and-renewing... 

 

I've recently asked  ChatGPT to convert that to Python. My server does not have Python installed, so I am unable to test it and it probably needs more work. Perhaps someone with Python skills can make this work:

 

 

# WARNING - code written by ChatGPT is untested. 
#=============================
# This Python code mimics the behavior of the PHP code 
# by checking if the token exists and if it has expired. 
# If it exists and hasn't expired, it reads the token from the file. 
# Otherwise, it requests a new token from eBay's OAuth API and 
# writes the token to the file for future use.
#=============================


import os
import time
import base64
import requests

# Credentials are stored in text files behind the public wall. 
private_space = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 

# get the folder for OAuth credentials that is in private space.
oauth_root = os.path.join(private_space, "/ebay_oauth/")

# Set up file paths.

oauth_clientIdfile = os.path.join(oauth_root, "ebay_apiuser.txt") # appID  = clientID
oauth_secretIdfile = os.path.join(oauth_root, "ebay_apisecret.txt") # certID = clientSecret
oauth_basictokenfile = os.path.join(oauth_root, "ebay_token_basic.txt") # will update every 2 hours

def get_basic_oauth_token():
    global oauth_basictokenfile
    # Look at timestamp to see if it has expired.
    now = time.time()
    duration = 7200  # Life of the token, 2 hours
    margin = 60  # Remaining time before we request a new token, 1 minute
    if os.path.exists(oauth_basictokenfile):
        tstamp = os.path.getmtime(oauth_basictokenfile) # get last modified time 
        if tstamp + duration - now > margin:  # Some time remains on token
            with open(oauth_basictokenfile, 'r') as f:
                return f.read().strip()
        else:
            return create_basic_oauth_token()
    else:
        return create_basic_oauth_token()

def create_basic_oauth_token():
    global oauth_clientIdfile, oauth_secretIdfile, oauth_basictokenfile

    url = 'https://api.ebay.com/identity/v1/oauth2/token'
    clientID = open(oauth_clientIdfile, 'r').read().strip()
    clientSecret = open(oauth_secretIdfile, 'r').read().strip()

    headers = {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Authorization': 'Basic ' + base64.b64encode((clientID + ':' + clientSecret).encode()).decode()
    }

    body = {
        'grant_type': 'client_credentials',
        'scope': 'https://api.ebay.com/oauth/api_scope'
    }

    response = requests.post(url, headers=headers, data=body)
    
    if response.ok:
        token = response.json()
        if 'access_token' in token:
            with open(oauth_basictokenfile, 'w') as f:
                f.write(token['access_token'])
            return token['access_token']
    else:
        response.raise_for_status() # write some code to deal with error

# Example usage:
# token = get_basic_oauth_token()
# print(token)

 

 

It should also be noted that I use a single token for all of my public applications and merely check to see if it will expire soon. eBay recommends waiting until the token actually expires, returning an error that the token has expired, before minting a replacement.  So that logic would change this code a bit.

 

 

ShipScript has been an eBay Community volunteer since 2003, specializing in HTML, CSS, Scripts, Photos, Active Content, Technical Solutions, and online Seller Tools.
Message 2 of 2
latest reply