Geovisualization Developer's Guide

Authentication with Python

This topic is dedicated to handling Geovisualization authentication when using Python.

Prerequisite: Getting Authentication Credentials

The code below can be run as a command-line script to:

  • obtain an access token
  • perform an authenticated request by adding an Authorization header

The script requires Python 2.7 and the requests module.

import json
import sys
import getpass
import requests

app_id = '{YOUR_APP_ID}'
app_code = '{YOUR_APP_CODE}'

# This function obtains an access token.
#
# @param user_name User name associated with HERE Account
# @param password Password associated with HERE Account
# @param app_id The value of app_id
# @param app_code The value of app_code
#
# @return A string containing the HERE access token

def get_access_token(user_name, password, app_id, app_code):
  url = 'https://datalens.api.here.com/v1/sign_in/?%s=%s&%s=%s' % ('app_id', app_id, 'app_code', app_code)
  payload = json.dumps({'email': user_name, 'password': password})
  r = requests.post(url,
            headers={'Content-Type': 'application/json'},
            data=payload)

  if r.status_code == 200:
    response = r.json()
    return response['access_token']

  raise Exception('Cannot get Data Lens access_token. Status code: %s ' % r.status_code)

# This function runs a request to retrieve data sets.
#
# @param access_token The HERE access token
# @param app_id The value of app_id
# @param app_code The value of app_code
#
# @return A JSON string containing the retrieved data sets

def get_datasets(acess_token, app_id, app_code):
  url = 'https://datalens.api.here.com/v1/datasets/?%s=%s&%s=%s'% ('app_id', app_id, 'app_code', app_code)
  r = requests.get(url,
           headers={'Content-Type': 'text/plain',
                'Authorization': 'Bearer %s' % access_token})
  if r.status_code == 200:
    return r.json()

  raise Exception('Cannot get retrieve datasets: %s' % r.status_code)

# The main function of the script expects two command line arguments:
# the HERE Account user name and the HERE Account password. It uses
# them, along with the application-specific app_id and app_code, to
# obtain an access token and request datasets.

if __name__ == '__main__':
  if len(sys.argv) != 2:
    print 'get_token.py - provides an Auth token (valid for 1 hour)'
    print 'usage: get_token.py username'
    sys.exit()
  user_name = sys.argv[1]
  password = getpass.getpass()
  access_token = get_access_token(user_name, password, app_id, app_code)
  if access_token:
    print get_datasets(access_token, app_id, app_code)

You cannot use this account to purchase a commercial plan on Developer Portal, as it is already associated to plans with different payment methods.

To purchase a commercial plan on Developer Portal, please register for or sign in with a different HERE Account.

Something took longer than expected.

The project should be available soon under your projects page.

Sorry, our services are not available in this region.

Something seems to have gone wrong. Please try again later.

We've detected that your account is set to Australian Dollars (AUD).
Unfortunately, we do not offer checkouts in AUD anymore.
You can continue using your current plan as normal, but to subscribe to one of our new plans,
please register for a new HERE account or contact us for billing questions on selfservesupport@here.com.