gpt4 book ai didi

oauth-2.0 - 如何在 Rust 中使用 google_speech1 将语音发送到文本请求?

转载 作者:行者123 更新时间:2023-11-29 08:15:33 28 4
gpt4 key购买 nike

我正在尝试将 google_speech1 用于 Rust,但文档提供的示例不完整,这让我很难弄清楚如何将语音发送到文本请求。

更具体地说,我希望能够发送本地音频文件,指明源语言并检索转录。

这是我在官方文档中找到的最接近的(https://docs.rs/google-speech1/1.0.8+20181005/google_speech1/struct.SpeechRecognizeCall.html):

use speech1::RecognizeRequest;

// As the method needs a request, you would usually fill it with the desired information
// into the respective structure. Some of the parts shown here might not be applicable !
// Values shown here are possibly random and not representative !
let mut req = RecognizeRequest::default();

// You can configure optional parameters by calling the respective setters at will, and
// execute the final call using `doit()`.
// Values shown here are possibly random and not representative !
let result = hub.speech().recognize(req)
.doit();

更新退一步说,即使网站上提供的简单示例似乎也无法正常运行。这是一些非常基本的示例代码:

pub mod speech_api_demo {
extern crate google_speech1 as speech1;
extern crate hyper;
extern crate hyper_rustls;
extern crate yup_oauth2 as oauth2;
use oauth2::{ApplicationSecret, Authenticator, DefaultAuthenticatorDelegate, MemoryStorage};
use speech1::Speech;
use speech1::{Error, Result};
use std::fs::File;
use std::io::Read;

#[derive(Deserialize, Serialize, Default)]
pub struct ConsoleApplicationSecret {
pub web: Option<ApplicationSecret>,
pub installed: Option<ApplicationSecret>,
}

pub fn speech_sample_demo() {
/*
Custom code to generate application secret
*/

let mut file =
File::open("C:\\Users\\YOURNAME\\.google-service-cli\\speech1-secret.json").unwrap();
let mut data = String::new();
file.read_to_string(&mut data).unwrap();

use serde_json as json;
let my_console_secret = json::from_str::<ConsoleApplicationSecret>(&data);

assert!(my_console_secret.is_ok());
let unwrappedConsoleSecret = my_console_secret.unwrap();
assert!(unwrappedConsoleSecret.installed.is_some() && unwrappedConsoleSecret.web.is_none());

let secret: ApplicationSecret = unwrappedConsoleSecret.installed.unwrap();

/*
Custom code to generate application secret - END
*/

// Instantiate the authenticator. It will choose a suitable authentication flow for you,
// unless you replace `None` with the desired Flow.
// Provide your own `AuthenticatorDelegate` to adjust the way it operates and get feedback about
// what's going on. You probably want to bring in your own `TokenStorage` to persist tokens and
// retrieve them from storage.
let auth = Authenticator::new(
&secret,
DefaultAuthenticatorDelegate,
hyper::Client::with_connector(hyper::net::HttpsConnector::new(
hyper_rustls::TlsClient::new(),
)),
<MemoryStorage as Default>::default(),
None,
);
let mut hub = Speech::new(
hyper::Client::with_connector(hyper::net::HttpsConnector::new(
hyper_rustls::TlsClient::new(),
)),
auth,
);

let result = hub.operations().get("name").doit();

match result {
Err(e) => match e {
// The Error enum provides details about what exactly happened.
// You can also just use its `Debug`, `Display` or `Error` traits
Error::HttpError(_)
| Error::MissingAPIKey
| Error::MissingToken(_)
| Error::Cancelled
| Error::UploadSizeLimitExceeded(_, _)
| Error::Failure(_)
| Error::BadRequest(_)
| Error::FieldClash(_)
| Error::JsonDecodeError(_, _) => (println!("{}", e)),
},
Ok(res) => println!("Success: {:?}", res),
}
}
}

运行此代码(调用 speech_sample_demo)会出现以下错误:

Token retrieval failed with error: Invalid Scope: 'no description provided'

我还尝试了一些非常丑陋的代码来强制范围进入请求,但没有任何区别。我很难理解这个错误的含义。我是不是在我的请求中遗漏了某些东西,还是在另一端有其他东西妨碍了我?或者也许那个 api 代码库刚刚坏了?

另请注意,默认提供的客户端 ID 和客户端密码不再有效,当我使用它们时,它会说帐户已被删除。

然后我设置了一个 OAuth 2.0 客户端并生成了我将其复制到默认位置的 json 文件,然后开始出现上述错误。也许只是我没有正确设置 Google Api 帐户,但无论如何,如果其他人可以尝试看看我是否是唯一遇到这些问题的人,那就太好了。

一旦我不再运行这样一个简单的请求,我就有更多代码准备好进行测试以发送音频文件,但目前它在流程的早期就失败了。

最佳答案

您得到的错误源自 here意味着 OAuth scope在生成凭证文件时使用的不允许您访问 Google 语音 API。所以问题不在您的 Rust 代码中,而是在您用于生成 OAuth 访问 token 的脚本中。

基本上,这意味着当您生成 OAuth json 文件时,您请求以一般方式访问 Google API,但您没有说明您打算使用哪些特定 API。根据this document ,您需要请求访问 https://www.googleapis.com/auth/cloud-platform 范围。

关于oauth-2.0 - 如何在 Rust 中使用 google_speech1 将语音发送到文本请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55565561/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com