C# 从地图上的纬度和经度获取位置数据时出错

C# 从地图上的纬度和经度获取位置数据时出错,c#,windows-phone-7,bing-maps,bing-api,restsharp,C#,Windows Phone 7,Bing Maps,Bing Api,Restsharp,我正在使用Bingmaps API获取经纬度的AdminDistrict和CountryRegion。 它可以在浏览器中输入此url: myBingmapsApiKey 但在WP7上的C#中,我无法让它工作。 代码如下: string wsUrl = "http://dev.virtualearth.net/REST/v1/Locations/-30,-70/?includeEntityTypes=AdminDivision1,CountryRegion&o=xml&c=es-E

我正在使用Bingmaps API获取经纬度的AdminDistrict和CountryRegion。 它可以在浏览器中输入此url:

myBingmapsApiKey

但在WP7上的C#中,我无法让它工作。 代码如下:

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;
}
答复是:

    <?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>

版权所有©2011微软及其供应商。版权所有。未经微软公司明确书面许可,不得访问本API,不得以任何方式使用、复制或传播本API的内容和任何结果。
http://dev.virtualearth.net/Branding/logo_powered_by.png
401未经授权
无效证件
访问被拒绝。您可能输入的凭据不正确,或者您可能无权访问请求的资源或操作。
59ebcf604bb343d79a6e8b93ad5695fe | MIAM001452 | 02.00.71.1600|
url与在web浏览器上工作的url相同。
有什么问题吗?

如果我理解正确,那么您使用的REST API。可能您的API密钥未设置为可计费交易

该页面介绍了位置API的计费:

*如果发生在AJAX控件或Silverlight控件会话的上下文中,则该类别不可计费


也许浏览器算作AJAX控件,而手机并不是一个“Silverlight控件”。

也许此时您已经想出了一个解决方案,但在google上搜索时我发现了这个主题,解决方案是不要像您那样通过url发送密钥,而是将其作为参数添加到请求中,如下所示:

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");