Accessing Bing Maps Web services with Perl and Python

A colleague of mine, Mike Spendlove, recently investigated connecting to the Bing Map web services from Perl and Python for a project he was working on. Currently there appears to be no information out there on how to do this yet so I asked if I could post the code. I have no experience with these languages so I post these purely for information purposes, I’ll be unlikely to answer any language specific questions that you may have.

For Python, the SUDS soap library ( was used to make the Python/SOAP communication easier. Python has limited support for HTTP digest authentication which made retrieving a client token nearly impossible, however with the new Bing Maps application key connecting to the Bing Map Web services is easy. In this example a connection to the Geocoding service is done.

Python to Bing Maps Geocoding Service
import sys
from suds.client import Client
from suds.client import WebFault

def main(*args):
application_key = <YOUR APP KEY>
url = ''
c = Client(url)

#To view service methods and objects, print the client:
#print c

greq = c.factory.create('GeocodeRequest')

cred = c.factory.create('ns0:Credentials')
cred.ApplicationId = application_key
greq.Credentials = cred

address = c.factory.create('ns0:Address')
address.AddressLine = "119 Spadina Avenue"
address.AdminDistrict = "Ontario"
address.CountryRegion = "Canada"
address.Locality = "Toronto"
greq.Address = address

#Select 1st port and set request options:

response = c.service.Geocode(greq)
except WebFault, e:
print "ERROR!"
print e

locations = response['Results']['GeocodeResult'][0]['Locations']['GeocodeLocation']

print "Found %s locations!" % len(locations)

for loc in locations:
print ("Lat: %s, Lon: %s, Alt: %s, CalcMethod: %s" %

if __name__ == '__main__':


In this example you can see how to use Pearl to connect to the Bing Maps common service in order to retrieve a client token.

Pearl to Bing Maps Common Service


## This requires installing SOAP-WSDL 2.00.10 and its dependencies

## First generate the appropriate interface classes at the command line:
## perl -b c:\dev\perl\BingMaps
##  (+ prompt for username/password)

my ($user,$password) = (‘XXX’,’XXX’);
my $clientIP = ‘’;
my $tokenDuration = 180;
#Include the interface files
use lib "C:\\dev\\perl\\BingMaps";
use MyInterfaces::CommonService::CommonServiceSoap;
#Create the service object
my $service = MyInterfaces::CommonService::CommonServiceSoap->new();
#Add digest authentication credentials (assuming transport is LWP::UserAgent)
*SOAP::WSDL::Transport::HTTP::get_basic_credentials = sub {
return ($user, $password);
# Get Token
my $response = $service->GetClientToken( {
    specification =>  {
      ClientIPAddress =>  $clientIP,
      TokenValidityDurationMinutes =>  $tokenDuration,
die $response if not $response;
print $response;


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s