当Android应用程序获得Web后端的离线访问时,如何处理redirect_uri_mismatch错误?

Moh*_*deh 14 android oauth-2.0 google-oauth

我想实现这个问题中描述的流程:

使用OAuth2为app*和*网站进行身份验证

此处描述了针对此要求的Google解决方案:

https://developers.google.com/identity/protocols/CrossClientAuth#offlineAccess

我遵循了本指南,但不幸的是,当我在后端使用授权代码来访问令牌并从谷歌刷新令牌时,谷歌抛出此错误:

{
  "error": "invalid_request",
  "error_description": "Missing parameter: redirect_uri"
}
Run Code Online (Sandbox Code Playgroud)

redirect_uri 对于这种情况没有任何意义,因为客户端是android.

还有一些建议,他们说使用"postmessage"作为重定向uri或使用http:// localhost作为重定向uri或在谷歌应用程序控制台中的重定向URI中添加服务器地址,但没有一个适用于此流程.

小智 7

在深入研究AppAuth库(https://openid.github.io/AppAuth-iOS/https://openid.github.io/AppAuth-Android/)之前,花了两天时间研究了stackoverflow和google上的几乎所有问题,最后找到了答案。

所以,我发现了两个规则:

  1. redirect_uri 应该是以下之一:

    • 您的应用程序bundle_idpackage_name+:/://+ *your_path*(例如com.example.myapp://google_auth
    • client_id的反向 DNS 形式 +:/://+ *your_path*(例如com.googleusercontent.apps.*account_id*:/my_path
  2. redirect_uri初始授权码请求 ( https://accounts.google.com/o/oauth2/v2/auth) 和授权码交换请求 ( https://www.googleapis.com/oauth2/v4/token) 必须完全相同