【发布时间】:2019-09-01 14:52:18
【问题描述】:
我想在我的 Android 应用中使用 REST 调用 Google 文字转语音功能。但是,我每次都得到com.android.volley.AuthFailureError。我可以看到为我的项目启用了 API,并且也启用了计费。在我的应用中,我已经收到了对 Google 翻译的请求,所以我一直在使用相同的 API 密钥。
由于找不到任何示例,因此我不确定在执行请求时如何提供密钥。代码如下:
public CloudSpeak(final Context context){
RequestQueue queue = Volley.newRequestQueue(context);
String url_base = "https://texttospeech.googleapis.com";
String synthesize_text = "/v1/text:synthesize";
StringBuilder sb = new StringBuilder();
sb.append(url_base);
sb.append(synthesize_text);
String url = sb.toString();
JSONObject input = new JSONObject();
JSONObject voice = new JSONObject();
JSONObject audioConfig = new JSONObject();
JSONObject jsonData = new JSONObject();
try {
input.put("text", "Jag kan prata svenska!");
voice.put("languageCode", "sv-SV");
audioConfig.put("audioEncoding", "OGG_OPUS");
jsonData.put("input", input);
jsonData.put("voice", voice);
jsonData.put("audioConfig", audioConfig);
} catch (JSONException e){
e.printStackTrace();
}
JsonRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, url, jsonData,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Toast.makeText(context, "Result obtained", Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context, "Some error", Toast.LENGTH_SHORT).show();
}
}){
@Override
protected Map<String, String> getParams(){
Map<String, String> params = new HashMap<>();
params.put("X-Goog-Api-Key", "API-Key");
return params;
}
};
queue.add(jsonRequest);
}
找到了键名X-Goog-Api-Keyhere.不知道对不对。
【问题讨论】:
-
这听起来像一个 http 标头。它在链接器代码中处理:
httpRequest.headers.add('X-Goog-Api-Key', _apiKey);
标签: android google-cloud-platform google-text-to-speech