Browse our guides and interact with our API reference for more information about Discovery API. Try Discovery API and learn about core concepts.

 

 

 

 

Clone a Code Sample

After you explore our OAuth2.0 grant guides, API reference and code samples, you can clone one of our code samples to get you started. Our documentation contains a code sample for each resource in four different languages. Whatever language that you are using, you can use our Python code sample below as a model and tweak the parameters to get your desired request.

First, you must create the code in your app to acquire an access token.  OAuth 2.0 describes a number of grants (methods) for a client application to acquire the access token needed.  It must be decided which OAuth 2.0 grant type you are going to use in order to receive the access token.  The grant type used depends on the use case.  The most common OAuth 2.0 grant types are listed below.

  • Authorization Code - Used for Apps running a web server, browser based and mobile apps.
  • Password - Used for loggin in with a username and password.
  • Client Credentials - Used for application access.
  • Implicit - Originally recommended for clients without a secret, but has been superceded by the Authorization Code grant type with no secret.

The authorization flow, or process, for obtaining the access token differs depending on the grant type.  Regardless of the grant type used, the result of the OAuth 2.0 authorization flow is an access token.  Please refer to the Use the Authorization Code Grant Type or Use the Client Credentials Grant Type guides for more information.

After your client app is able to receive an access token, you are ready to create the code to use the API endpoints.  In the following code sample, we create a code sample for Discovery API.  We execute a search preview request using the production environment.  The process of creating the code sample for the resource is described below.  For this particular example, we used windows and installed Python 3.6.0 prior to creating the sample.

Note: If you need to look up the URL or parameters for a resource, refer back to the API reference or code sample documentation.

To Create a Code Sample:

  1. Note the request URL for your desired resource.

Request URL


https://apis.ebsco.com/discoveryapi/searchPreviews

  1. Determine the parameters that you would like to use for your request.

Parameters

context = {A valid customer profile in the form of customerid.groupid.profileid}
query = {Desired search parameter}
lang = {Language}
Authorization = {Access token obtained from the OAuth2.0 protocol}

Please refer to our API Reference for more information on the parameters.

  1. Create a new sample.py file for your sample code.
  2. Add the following code to sample.py.  The code below defines your parameters, forms the request URL, forms the request headers, sends the request and gets the response back.

# File: sample.py
 
# HTTP Library
import http.client
import urllib
import urllib.parse
import urllib.request

# Parameters
CGP = {your profile}
QUERY = {your desired search preview}
LANGUAGE = {your desired language}	

# Form the URL and authorization string
url = ''.join(['/discoveryapi', '/searchPreviews'])
authorization_str = ''.join(['Bearer',' ', ACCESS_TOKEN])
	 
# Create Connection Instance
conn = http.client.HTTPSConnection("apis.ebsco.com")
 
# Set the Query Parameters
params = urllib.parse.urlencode({'context': CGP, 'query': QUERY, 'lang': LANGUAGE})

# Create the Request Headers
headers = {
	'accept': "application/json",
        'Authorization': authorization_str
}
 
# Send Request 
conn.request("GET", url + '?' + params, headers=headers) 
	
# Get Response
res = conn.getresponse()
data = res.read()

  1. Set CGP, QUERY and LANGUAGE to your desired search parameters in the sample.py script.  Also, change the  ACCESS_TOKEN parameter to the token that you gathered using the OAuth 2.0 protocol.
  2. Save your sample.py file.
  3. Run your script by entering python sample.py in a Command Prompt.  If successful, a JSON response with the requested search will be returned and displayed on the screen.

{
  "totalItems": 692,
  "estimatedTotalItems": 692,
  "items": [
    {
      "id": "YTloLTExODk1MjkyMw==",
      "title": "Partners rally for BMX bike park at Enoch.",
      "db": {
        "name": "Academic Search Complete"
      },
      "attributes": {
        "publicationDate": "20161101",
        "contributors": [
          {
            "name": "Laskaris, Sam"
          }
        ],
        "isPeerReviewed": false,
        "collectionTitle": null
      }
    },
    {
      "id": "YTloLTEzODQxMTcwMQ==",
      "title": "\"Parents, Me & X-Sports\": Mapping the BMX Culture in Contemporary China.",
      "db": {
        "name": "Academic Search Complete"
      },
      "attributes": {
        "publicationDate": "20191001",
        "contributors": [
          {
            "name": "Ding, Yiyin"
          },
          {
            "name": "Evers, Clifton"
          },
          {
            "name": "Doering, Adam"
          }
        ],
        "isPeerReviewed": true,
        "collectionTitle": null
      }
    },
    {
      "id": "YTloLTQ5MDY4NDM=",
      "title": "BMX bike injuries: the latest epidemic.",
      "db": {
        "name": "Academic Search Complete"
      },
      "attributes": {
        "publicationDate": "19841013",
        "contributors": [
          {
            "name": "Soysa, S.M."
          },
          {
            "name": "Grover, M.L."
          },
          {
            "name": "McDonald, P.J."
          }
        ],
        "isPeerReviewed": true,
        "collectionTitle": null
      }
    },
    {
      "id": "YTloLTEzNTIxOTIz",
      "title": "Dave Mirra.",
      "db": {
        "name": "Academic Search Complete"
      },
      "attributes": {
        "publicationDate": "20040628",
        "contributors": [
          {
            "name": "Adato, Allison"
          },
          {
            "name": "Mandel, Kwala"
          },
          {
            "name": "Meltzer, Dana"
          },
          {
            "name": "Daly, Sean"
          },
          {
            "name": "Hammel, Sara"
          },
          {
            "name": "Schou, Solvej"
          },
          {
            "name": "Baker, K. C."
          },
          {
            "name": "Debono, Fiona Galea"
          }
        ],
        "isPeerReviewed": false,
        "collectionTitle": null
      }
    },
    {
      "id": "YTloLTM2OTIzNTk1",
      "title": "BMX bike injuries.",
      "db": {
        "name": "Academic Search Complete"
      },
      "attributes": {
        "publicationDate": "19841103",
        "contributors": [],
        "isPeerReviewed": true,
        "collectionTitle": null
      }
    },
    {
      "id": "YTloLTc2MTMzMjg2",
      "title": "The Best and the Brashest.",
      "db": {
        "name": "Academic Search Complete"
      },
      "attributes": {
        "publicationDate": "20120529",
        "contributors": [
          {
            "name": "Bishop, Greg"
          }
        ],
        "isPeerReviewed": false,
        "collectionTitle": null
      }
    },
    {
      "id": "YTloLTUyNTI4Mzcx",
      "title": "Up, Up and a Way Up.",
      "db": {
        "name": "Academic Search Complete"
      },
      "attributes": {
        "publicationDate": "20100729",
        "contributors": [
          {
            "name": "HIGGINS, MATT"
          }
        ],
        "isPeerReviewed": false,
        "collectionTitle": null
      }
    },
    {
      "id": "YTloLTMxNjA3Mjk2",
      "title": "PEDAL TO THE MEDALS.",
      "db": {
        "name": "Academic Search Complete"
      },
      "attributes": {
        "publicationDate": "20080414",
        "contributors": [
          {
            "name": "Gatehouse, Jonathon"
          }
        ],
        "isPeerReviewed": false,
        "collectionTitle": null
      }
    },
    {
      "id": "YTloLUowRTM4MTg3MzUxNDkwOA==",
      "title": "BMX debut a smashing success",
      "db": {
        "name": "Academic Search Complete"
      },
      "attributes": {
        "publicationDate": "20080821",
        "contributors": [
          {
            "name": "Sal Ruibal"
          }
        ],
        "isPeerReviewed": false,
        "collectionTitle": null
      }
    },
    {
      "id": "YTloLTExMTYzMjg0MA==",
      "title": "Let's Give It a Spin.",
      "db": {
        "name": "Academic Search Complete"
      },
      "attributes": {
        "publicationDate": "20160101",
        "contributors": [
          {
            "name": "Lynch, Wayne"
          }
        ],
        "isPeerReviewed": false,
        "collectionTitle": null
      }
    }
  ]
}

You have completed your first code sample consuming Discovery API. You are on your way. Have fun!