如何将范围作为 POST 主体的一部分而不是 URL 参数?

How to supply scopes as part of POST body rather than URL params?

一些背景知识

我正在研究 Installed Application project that handles possibly very many Google APIs and their scopes for each user. One major issue 我遇到的问题是当用户选择大量范围进行身份验证时,它们作为参数在 URL 中提供并被切断。例如,url 可能会这样终止(为便于阅读而换行):

...%20https://www.googleapis.com/auth/admin.directory.userschema.
readonly%20https://www.googleapis.com/auth/admin.reports.audit.readon
ly%20https://www.googleapis.com/auth/admin.rep

这是因为我希望我的用户只需进行一次身份验证,但我意识到,在他们使用的每个 API 中都必须进行一次身份验证并不会太糟糕。所以我打破了它,但后来发现再次验证后它会覆盖他们的访问权限。所以,我查看了 Incremental Authorization mentioned on the docs, only to find out that no it won't work for Installed Apps and there is apparently no intention 以获取它:

I think the problem here is that you are using the installed application OAuth2 flow, which contrary to the documentation doesn't support the include_granted_scopes parameter.

好的,现在看来我有两个选择。第一个和 obvious option is to maintain a separate token for each API. However, before I commit myself to that route I looked in to how the Python-based Google Apps Manager 处理它,因为在验证时我没有看到 URL 中的任何范围,所以我假设它是作为 POST 正文发送的。

主要问题

对安装的应用程序使用 Google API Client Library for .NET,是否可以在不提供范围作为 URL 参数的情况下进行身份验证?我已经尝试使用 PowerShell 和 Invoke-RestMethod 来尝试让 POST 请求工作,但没有任何运气。

我认为这不是客户端库的问题。问题是最初的授权请求。向用户弹出列表的是 HTTP get。

https://accounts.google.com/o/oauth2/auth?client_id={clientid}.apps.googleusercontent.com&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=https://www.googleapis.com/auth/analytics.readonly&response_type=code

据我所知,http get 的最大长度是 2000 个字符。如果它更多,那么它会被某些浏览器切断。

选项:

我读到了 incremental authorization

Google supports incremental authorization, which enables your app to request initial permissions at sign-in and later can request additional permissions, typically just before they are needed. For example, if your app allows users to save music playlists to Google Drive, you can ask for basic user information at sign-in, and later ask just for Google Drive permissions when the user is ready to save their first playlist. At this point, the consent dialog box asks the user only for the new permissions, which gives them a simpler, in-context decision to make.

我只能找到关于 web, Android, or iOS. This does not mean that its only supported by them. All those are Googles SDKs so it could be something that they only allow internally. However this may be something that we can add to the client library. I suggest you add an issue request. Google-api-dotnet-client issues

的信息

看起来应该是可以的。有一个 http/rest 例子 here

我认为唯一的其他选择就是您现在正在做的事情,并为每个 API 或应用程序的每个部分设置一个刷新令牌。

更新

经过一些挖掘 include_granted_scopes 不在客户端库中。这意味着必须添加它,否则您将不得不在没有客户端库的情况下以困难的方式进行操作。问题请求可能是要走的路。

我确定这是不可能的。在抓取我认为工作方式不同的 python 代码后,我使用 Fiddler 捕获发送到浏览器的初始 URL 并发现它确实对查询参数执行完全相同的操作。由于某种原因,我无法直接使用浏览器开发人员工具捕捉范围,似乎只是在重定向中丢失了范围。

也就是说,我不再有比较依据或任何理由认为其他项目的工作方式与记录的方式不同,所以我认为我的问题是无效的。

编辑:在撰写本文时,尽管有文档,但已安装的应用程序不支持增量身份验证。