Bab*_*Dan 6 android google-api google-translate
我一直在互联网上搜索谷歌翻译API使用,但我找不到下降教程或解释.这就是我所做的:
在我的Google API控制台中,我使用此答案使用SHA1指纹在Public API访问下生成了一个键.以下是我的API控制台的外观:

在Android工作室中,我使用以下代码使用OkHttp库构建并发送我的请求:
OkHttpClient client = new OkHttpClient();
String apiKey = "My API key";
String apiLangSource = "en";
String apiLangTarget = "de";
String apiWord = "Hello";
String googleApiUrl = "https://www.googleapis.com/language/translate/v2?key=" + apiKey + "&source=" + apiLangSource + "&target=" + apiLangTarget + "&q=" + apiWord;
Request request = new Request.Builder().url(googleApiUrl).build();
Log.d(TAG, "API STRING" + googleApiUrl);
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
Log.d(TAG , "HTTP CALL FAIL");
}
@Override
public void onResponse(Response response) throws IOException {
Log.d(TAG , response.body().string());
}
});
Run Code Online (Sandbox Code Playgroud)
它运行正常但响应我得到:
{
"error": {
"errors": [
{
"domain": "usageLimits",
"reason": "ipRefererBlocked",
"message": "There is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your API key configuration if request from this IP or referer should be allowed.",
"extendedHelp": "https://console.developers.google.com"
}
],
"code": 403,
"message": "There is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your API key configuration if request from this IP or referer should be allowed."
}
}
Run Code Online (Sandbox Code Playgroud)
这里有什么问题?我的API设置正确吗?我是否正确拨打电话(我看过一些图书馆,但有导游)?这是使用这个库的合理方式吗?这意味着什么?
"There is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your API key configuration if request from this IP or referer should be allowed."
Run Code Online (Sandbox Code Playgroud)
我认为有一些免费的演示调用,这不是问题所在.
问题是在为 Android 应用程序设置 API 密钥限制时,您指定了包名称和 SHA-1 证书指纹。因此,您的 API 密钥将仅接受来自您的应用程序且指定了包名称和 SHA-1 证书指纹的请求。
那么谷歌如何知道该请求是从您的 Android 应用程序发送的呢?您必须使用以下键在每个请求的标头中添加应用程序的包名称和 SHA-1:
键:"X-Android-Package",值:您的应用程序包名称
键:"X-Android-Cert",值:您的 apk 的 SHA-1 证书
首先,获取您的应用程序 SHA 签名(您将需要Guava库):
/**
* Gets the SHA1 signature, hex encoded for inclusion with Google Cloud Platform API requests
*
* @param packageName Identifies the APK whose signature should be extracted.
* @return a lowercase, hex-encoded
*/
public static String getSignature(@NonNull PackageManager pm, @NonNull String packageName) {
try {
PackageInfo packageInfo = pm.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
if (packageInfo == null
|| packageInfo.signatures == null
|| packageInfo.signatures.length == 0
|| packageInfo.signatures[0] == null) {
return null;
}
return signatureDigest(packageInfo.signatures[0]);
} catch (PackageManager.NameNotFoundException e) {
return null;
}
}
private static String signatureDigest(Signature sig) {
byte[] signature = sig.toByteArray();
try {
MessageDigest md = MessageDigest.getInstance("SHA1");
byte[] digest = md.digest(signature);
return BaseEncoding.base16().lowerCase().encode(digest);
} catch (NoSuchAlgorithmException e) {
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
然后,在请求头中添加包名和SHA证书签名:
java.net.URL url = new URL(REQUEST_URL);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
try {
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
connection.setRequestProperty("Accept", "application/json");
// add package name to request header
String packageName = mActivity.getPackageName();
connection.setRequestProperty("X-Android-Package", packageName);
// add SHA certificate to request header
String sig = getSignature(mActivity.getPackageManager(), packageName);
connection.setRequestProperty("X-Android-Cert", sig);
connection.setRequestMethod("POST");
// ADD YOUR REQUEST BODY HERE
// ....................
} catch (Exception e) {
e.printStackTrace();
} finally {
connection.disconnect();
}
Run Code Online (Sandbox Code Playgroud)
希望这有帮助!:)
| 归档时间: |
|
| 查看次数: |
1361 次 |
| 最近记录: |