【发布时间】:2014-07-22 10:24:10
【问题描述】:
我一遍又一遍地尝试让这件事发挥作用,但我似乎无法做到。
我隐藏了 apikey...
我已在我的帐户中启用了 Google Translate API,并发表了这篇文章:
public class handleStuff extends AsyncTask<String, Integer, String>
{
@Override
protected String doInBackground(String... arg0) {
translateTest();
return null;
}
}
private static byte[] buff = new byte[1024];
void translateTest()
{
String apiKey = "HIDDEN...";
String url = "https://www.googleapis.com/language/translate/v2?key="+apiKey+"&source=en&target=de&q=Hello%20world";
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
try {
//et.append("Try \n");
HttpResponse response = client.execute(request);
//et.append("Pass");
StatusLine status = response.getStatusLine();
//et.append(response.toString());
if(status.getStatusCode() != 200) {
//et.append("Error !200\n");
}
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
ByteArrayOutputStream content = new ByteArrayOutputStream();
int readCount = 0;
while( (readCount = is.read(buff)) != -1 )
{
content.write(buff, 0, readCount);
}
String retVal = new String(content.toByteArray());
Log.d("Output", retVal);
otp = retVal;
//et.append("Translate" + retVal);
} catch (Exception e)
{
//et.append("Exception:\n" + e.getMessage() + "\n");
Log.d("Output", "Err:" + e.getMessage());
e.printStackTrace();
//throw new ApiException("Server Connect Problem" + e.getMessage(), e);
}
}
我已经为 android 添加了公共 API 访问权限并输入了我的 SHA1;com.... (我也去了 window>preferences>android>build> 并添加了调试 SHA1)... 但是,当我运行它时,我得到以下响应:
07-21 22:07:07.264: D/Output(18472): {
07-21 22:07:07.264: D/Output(18472): "error": {
07-21 22:07:07.264: D/Output(18472): "errors": [
07-21 22:07:07.264: D/Output(18472): {
07-21 22:07:07.264: D/Output(18472): "domain": "usageLimits",
07-21 22:07:07.264: D/Output(18472): "reason": "accessNotConfigured",
07-21 22:07:07.264: D/Output(18472): "message": "Access Not Configured. Please use Google Developers Console to activate the API for your project."
07-21 22:07:07.264: D/Output(18472): }
07-21 22:07:07.264: D/Output(18472): ],
07-21 22:07:07.264: D/Output(18472): "code": 403,
07-21 22:07:07.264: D/Output(18472): "message": "Access Not Configured. Please use Google Developers Console to activate the API for your project."
07-21 22:07:07.264: D/Output(18472): }
07-21 22:07:07.264: D/Output(18472): }
我知道我拥有正确的 API 密钥,因为它知道我的应用程序不被允许。所以它一定是 SHA1 的问题;com ......我做错了,任何帮助都会很有用
【问题讨论】: