使用 reqwest crate 进行 oauth2 身份验证 - redirect_uri_mismatch

时间:2021-06-05 03:52:05

标签: rust oauth-2.0 google-oauth google-authentication reqwest

我正在使用 reqwest 通过 google oauth 对用户进行身份验证。我已正确设置凭据(client_id、secret、redirect_uris),但仍然无法交换从 google 获取的代码以获取令牌。

let mut params = HashMap::new();
params.insert("client_id", &config.web.client_id);
params.insert("client_secret", &config.web.client_secret);
params.insert("grant_type", &grant_type);
params.insert("redirect_uri", &redirect_uri);
params.insert("code", code);

let client = reqwest::blocking::Client::new();
if let Ok(response) = client.post("https://oauth2.googleapis.com/token")
  .form(&params)
  .send() {
  println!("{:?}", response);
}

它总是返回 redirect_uri_mismatch。 用于在身份验证期间检索代码的 redirect_uri 有效,但用于检索访问和刷新令牌的 redirect_uri 无效。 我在 php 中有一个类似的 webapp,使用相同的重定向 uri,它可以工作。我就是不能让它生锈。

我在从 https://accounts.google.com/o/oauth2/v2/auth 端点获取授权代码时使用了不同的 redirect_uri,而在发布到 https://oauth2.googleapis.com/token 以获取访问和刷新令牌时使用了另一个 redirect_uri。

1 个答案:

答案 0 :(得分:0)

事实证明,在检索授权代码和检索访问令牌时,我无法使用不同的重定向 uri。