Google 文本转语音的 API 键放在哪里?
Where put API-key for Google text-to-speech?
我想在我的 Android 应用程序中使用 REST 调用 Google 文本转语音功能。但是,我每次都会得到 com.android.volley.AuthFailureError
。我可以看到 API 已为我的项目启用,并且还启用了计费。在我的应用程序中,我已经请求 Google 翻译,所以我一直在使用相同的 API-key。
我不确定在执行请求时如何提供密钥,因为我找不到任何示例。这是代码:
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-Key
here.不知道对不对
感谢 Marged 的提示,我发现应该使用 getHeaders()
而不是 getParams()
,然后它可以与键 X-Goog-Api-Key
和对应的 API-Server-Keyvalue
.headers.add
在使用 JsonRequest
时似乎不起作用。
我想在我的 Android 应用程序中使用 REST 调用 Google 文本转语音功能。但是,我每次都会得到 com.android.volley.AuthFailureError
。我可以看到 API 已为我的项目启用,并且还启用了计费。在我的应用程序中,我已经请求 Google 翻译,所以我一直在使用相同的 API-key。
我不确定在执行请求时如何提供密钥,因为我找不到任何示例。这是代码:
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-Key
here.不知道对不对
感谢 Marged 的提示,我发现应该使用 getHeaders()
而不是 getParams()
,然后它可以与键 X-Goog-Api-Key
和对应的 API-Server-Keyvalue
.headers.add
在使用 JsonRequest
时似乎不起作用。