2
votes

I'm using Bingmaps API to get the AdminDistrict and CountryRegion of a Latitude and Longitude. It works entering in a browser to this url:

http://dev.virtualearth.net/REST/v1/Locations/-30,-70/?includeEntityTypes=AdminDivision1,CountryRegion&o=xml&c=es-ES&key=myBingmapsApiKey

But in C# on WP7 I can't get get it work. This is the code:

string wsUrl = "http://dev.virtualearth.net/REST/v1/Locations/-30,-70/?includeEntityTypes=AdminDivision1,CountryRegion&o=xml&c=es-ES&key=*myBingmapsApiKey*";

var request = new RestSharp.RestRequest(Method.GET);
var client = new RestSharp.RestClient(wsUrl);

try
{
    RestSharp.RestResponse resource;
    client.ExecuteAsync(request, (response) =>
    {
        resource = response;
        string content = resource.Content;
        string status_code = resource.StatusCode.ToString();
        string response_status = resource.ResponseStatus.ToString();
    });
}
catch (Exception e)
{
    string error = "Error: " + e.ToString() + "\n. Stack Trace: " + e.StackTrace;
}

And the response is:

    <?xml version="1.0" encoding="utf-8"?>
    <Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.microsoft.com/search/local/ws/rest/v1">
<Copyright>Copyright © 2011 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.</Copyright>
<BrandLogoUri>http://dev.virtualearth.net/Branding/logo_powered_by.png</BrandLogoUri>
<StatusCode>401</StatusCode><StatusDescription>Unauthorized</StatusDescription>
<AuthenticationResultCode>InvalidCredentials</AuthenticationResultCode>
<ErrorDetails><string>Access was denied. You may have entered your credentials incorrectly, or you might not have access to the requested resource or operation.</string></ErrorDetails>
<TraceId>59ebcf604bb343d79a6e8b93ad5695fe|MIAM001452|02.00.71.1600|</TraceId>
<ResourceSets />
    </Response>

The url is the same that is working on web browser. What can be wrong?

2

2 Answers

3
votes

probably at this point you already came up with a solution but searching on google i found this topic and the solution is to not send the key through the url like you doing, instead add it as parameter in the request like this:

string wsUrl = "http://dev.virtualearth.net/REST/v1/Locations/-30,-70/";    

var request = new RestSharp.RestRequest(Method.GET);    
request.AddParameter("includeEntityTypes", "AdminDivision1,CountryRegion");
request.AddParameter("key", myLey);
request.AddParameter("o", "xml");
0
votes

If I understand this right then the REST API you use may cost money. Maybe your API key istn't set up for billable transactions?

The page says about billing of Location API:

*This category is not billable if it occurs within the context of an AJAX Control or Silverlight Control session.

Maybe the browser counts as AJAX control and the phone isn't exactly a "Silverlight Control".