Time Zone API キーを Android アプリに制限することはできますか?
パッケージ名と SHA-1 ハッシュを定義することで、Google Maps Android API と Google Places API キーを特定の Android アプリに制限できます。
これは、Maps and Places API では問題なく機能しますが、Time Zone API にまったく同じ設定を使用すると、常に " REQUEST_DENIED
" が返されます。アプリケーションの制限を「なし」に戻すとクエリは成功しますが、API キーを保護しないままにしたくありません。
API を呼び出す方法は次のとおりです。
double lat = 51.1789;
double lon = -1.8262;
long time = 1523092938;
String Api_key = "#API_KEY#";
String query = "https://maps.googleapis.com/maps/api/timezone/json?location="+String.format(Locale.ROOT, "%.4f",lat)+","+String.format(Locale.ROOT, "%.4f",lon)+"×tamp="+time+"&key="+ Api_key;
protected JSONObject doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(query);
connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(5000);
connection.connect();
System.out.println(query);
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line+"\n");
Log.d("Response: ", "> " + line);
}
return new JSONObject(buffer.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
受け取った応答は次のとおりです。
{
"errorMessage" : "This IP, site or mobile application is not authorized to use this API key. Request received from IP address XX.XX.XX.XX, with empty referer",
"status" : "REQUEST_DENIED"
}
アプリケーションの制限をなしに設定すると、すべてが機能します。
{
"dstOffset" : 3600,
"rawOffset" : 0,
"status" : "OK",
"timeZoneId" : "Europe/London",
"timeZoneName" : "British Summer Time"
}