[英]403 error with google geolocation api in c#
我正在嘗試在C#(.net 4.6.1)中使用Google Maps地理位置api( https://developers.google.com/maps/documentation/geolocation/intro )。 我已經設置了服務器密鑰,並在api管理器中啟用了Google Maps Geolocation API。 這是我的代碼:
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response = await client.PostAsJsonAsync("https://www.googleapis.com/geolocation/v1/geolocate", new { key = "keyvalue" });
return await response.Content.ReadAsStringAsync();
}
我收到403錯誤。 這是完整的響應:
{
"error": {
"errors": [{
"domain": "usageLimits",
"reason": "dailyLimitExceededUnreg",
"message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.",
"extendedHelp": "https://code.google.com/apis/console"
}],
"code": 403,
"message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."
}
}
我知道他們的鍵值正確,而且我已經啟用了api。 我也知道我還沒有達到每日限制,因為Google控制台將我的每日使用情況列為零。 有人看到我的代碼有問題或有其他建議嗎? 我一定是錯誤地發送了密鑰,但是我不知道代碼中的錯誤在哪里。
更新資料
正確的代碼是:
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var response = await client.PostAsJsonAsync("https://www.googleapis.com/geolocation/v1/geolocate?key=keyvalue", "" );
//Or client.PostAsync could be used this way:
//var response = await client.PostAsync("https://www.googleapis.com/geolocation/v1/geolocate?key=keyvalue", new StringContent("", Encoding.UTF8, "application/json"));
return await response.Content.ReadAsStringAsync();
}
我將密鑰作為json對象傳遞,而不是將密鑰放在url中。
您必須將密鑰作為參數添加到URL
https://www.googleapis.com/geolocation/v1/geolocate?key=keyValue
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.