![](/img/trans.png)
[英]Modify frequency of Speech generated using Google Text-to-Speech API
[英]Where put API-key for Google text-to-speech?
我想在我的Android應用程序中使用REST調用Google文本語音轉換功能。 但是,我每次都會收到com.android.volley.AuthFailureError
。 我所看到的為我的項目啟用了API,並且也啟用了計費。 在我的應用中,我已經向Google Translate請求了,因此我一直在使用相同的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-Key
。 我不知道這是否正確。
感謝Marged的提示,我發現與其使用getParams()
,不如使用getHeaders()
,它可以與X-Goog-Api-Key
和相應的API-Server-Keyvalue
.headers.add
似乎不使用時,工作JsonRequest
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.