对已安装的应用程序使用 OAuth 2.0

Using OAuth 2.0 for Installed Applications

我正在编写一个命令行工具 (installedApp),它需要访问用户的 Google 表格。为此,我需要从用户那里获取访​​问令牌。

我遵循这些准则 OAuth2InstalledApp。到目前为止,我设法通过使用 redirect_uri 的 http://localhost 类型获得了授权码。我将 http://localhost:7373/authorizationCode 指定为 redirect_uri 并且我有一个本地服务器侦听端口 7373。

但是当我发出获取访问令牌的请求时,我收到了带有

的 HTTP 400 错误请求响应

redirect_uri_mismatch error

。当我发出请求时,redirect_uri 被定义为 "The redirect URI you obtained from the Developers Console"。这是错误的,因为当您创建已安装的应用程序时,您无法指定重定向 uri(仅适用于网络应用程序)。 https://infinit.io/_/i48b2rM

所以我的问题是我做错了什么?

当您在 Google 开发者控制台中创建客户端 ID 时,会为您创建两个 redirect_uris:urn:ietf:wg:oauth:2.0:oobhttp://localhost。您也可以考虑 urn:ietf:wg:oauth:2.0:oob:auto 。您的应用程序使用的值决定了授权代码如何返回到您的应用程序。 choose a redirect uri