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 (https://fedorahosted.org/suds/) 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 = 'http://dev.virtualearth.net/webservices/v1/geocodeservice/geocodeservice.svc?wsdl'
c = Client(url)

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

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

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

#Address
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:
c.set_options(port='BasicHttpBinding_IGeocodeService')

try:
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" %
(loc['Latitude'],loc['Longitude'],loc['Altitude'],loc['CalculationMethod']))

if __name__ == '__main__':
sys.exit(main(*sys.argv))

 

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

#!/usr/bin/perl

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

## First generate the appropriate interface classes at the command line:
## perl wsdl2perl.pl -b c:\dev\perl\BingMaps http://staging.common.virtualearth.net/find-30/common.asmx?wsdl
##  (+ prompt for username/password)

my ($user,$password) = (‘XXX’,’XXX’);
my $clientIP = ‘69.77.183.118’;
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;

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s