ComenzarEmpieza gratis

Set request headers

Many APIs require users provide an API key, obtained by registering for the service. Keys typically are passed in the request header, rather than as parameters.

The Yelp API documentation says "To authenticate API calls with the API Key, set the Authorization HTTP header value as Bearer api_key."

You'll set up a dictionary to pass this information to get(), call the API for the highest-rated cafes in NYC, and parse the response.

pandas (as pd) and requests have been loaded. The API endpoint is stored as api_url, and the key is api_key. Parameters are in the dictionary params.

Este ejercicio forma parte del curso

Streamlined Data Ingestion with pandas

Ver curso

Instrucciones del ejercicio

  • Create a dictionary, headers, that passes the formatted key string to the "Authorization" header value.
  • Query the Yelp API (api_url) with get() and the necessary headers and parameters. Save the result as response.
  • Extract the JSON data from response. Save the result as data.
  • Load the "businesses" values in data to the dataframe cafes and print the names column.

Ejercicio interactivo práctico

Prueba este ejercicio completando el código de muestra.

# Create dictionary that passes Authorization and key string
headers = {____: "Bearer {}".format(____)}

# Query the Yelp API with headers and params set
response = ____



# Extract JSON data from response
data = ____

# Load "businesses" values to a dataframe and print names
cafes = ____
print(cafes.name)
Editar y ejecutar código