使用 PowerShell 获取 Google 工作表的 OAuth2 授权码

Using PowerShell to get OAuth2 Authorization Code for Google Sheets

我正在尝试自动执行获取 OAuth2 授权码以用于 Google 表格的过程。

我创建了 Google 项目并使用 http://localhost/oauth2callback

的重定向 URI 注册了它

我已经通过在 Chrome:

中输入此 URL 来手动获得授权码
https://accounts.google.com/o/oauth2/v2/auth
  ?redirect_uri=http%3A%2F%2Flocalhost%2Foauth2callback
  &response_type=code
  &client_id=##CLIENTID##
  &scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fspreadsheets
  &access_type=offline
  &include_granted_scopes=true
  &state=state_parameter_passthrough_value

...然后通过从 Chrome:

中显示的结果 URL 中复制它来提取授权码
http://localhost/oauth2callback
  ?state=state_parameter_passthrough_value
  &code=##AUTHCODE##
  &scope=https://www.googleapis.com/auth/spreadsheets#

我用 PowerShell 复制这个过程的尝试惨遭失败。我想我缺少对所有这些 HTTP 重定向的工作原理的一些基本理解。

我已经尝试了该主题的几种变体,包括获取第一个重定向结果并通过另一个 Invoke-WebRequest 调用提交它,但无论我做什么,我只会在第一次调用时得到一个值,并且只有当我将 -MaximumRedirection 设置为 0 时——我得到的结果对我没有帮助:

Function getAuthorizationCode($cliids) {
    $auturl = "https://accounts.google.com/o/oauth2/v2/auth?redirect_uri=http`%3A`%2F`%2Flocalhost`%2Foauth2callback`&response_type=code`&client_id=$cliids`&scope=https`%3A`%2F`%2Fwww.googleapis.com`%2Fauth`%2Fspreadsheets`&access_type=offline`&include_granted_scopes=true`&state=state_parameter_passthrough_value";
    #$autrsp = Invoke-WebRequest -UseBasicParsing -Uri $auturl -UseDefaultCredentials -TimeoutSec 180 -ErrorAction SilentlyContinue;
    $autrsp = Invoke-WebRequest -UseBasicParsing -Uri $auturl -UseDefaultCredentials -TimeoutSec 180 -MaximumRedirection 0 -ErrorAction Ignore;
    $autrdi = "";
    if ($autrsp.StatusCode -ge 300 -and $autrsp.StatusCode -lt 400) {
        $autrdi = $autrsp.Headers.Location;
    }   
    return $autrdi;
}
# Force main variables to be non-global
Function myMain($arglis) {
    $cliids = "##CLIENTID##";
    getAuthorizationCode $cliids;
}
# Force main variables to be non-global
myMain $Args;

这导致:

https://accounts.google.com/ServiceLogin
  ?passive=1209600
  &continue=https://accounts.google.com/o/oauth2/v2/auth
    ?redirect_uri%3Dhttp://localhost/oauth2callback
    %26response_type%3Dcode
    %26client_id%3D##CLIENTID##
    %26scope%3Dhttps://www.googleapis.com/auth/spreadsheets
    %26access_type%3Doffline
    %26include_granted_scopes%3Dtrue
    %26state%3Dstate_parameter_passthrough_value
    %26from_login%3D1
    %26as%3D##LOGINCODE##
  &oauth=1
  &sarp=1
  &scc=1

生成的重定向值中没有任何内容是授权码,而且 none 我努力使用返回的信息也获得了授权码。

我仔细研究了一堆 Google 和 Whosebug 的搜索结果,但我不是在搜索错误的东西,就是不理解我在找什么。

我很难过。有人可以帮我弄清楚我需要在这里转向什么方向吗?

谢谢!

如果您使用浏览器,浏览器会让您登录 google,然后验证您的身份并向您发送授权码。如果您想使用 powershell 或 shell 脚本自动执行该过程,请使用 Resource Owner Password Credentials Grant 作为请求的一部分发送用户名和密码,服务器 returns Auth code.But 不幸的是 google 不支持它。 https://developers.google.com/identity/protocols/OAuth2InstalledApp

因此您必须通过您的电源动态打开浏览器shell 并让用户进行身份验证,一旦通过身份验证,用户应手动复制粘贴授权代码到您的控制台以生成访问令牌。为了让它变得简单,您可以重定向到一个 javascript 页面,该页面会打开一个带有授权代码的弹出窗口。例如 redirect_uri=http://localhost:8899/getcode.html 将打开 http://localhost:8899/getcode.html?code=xyz 如果您在 getcode.html 中编写一些 javascript 代码,它会读取代码查询参数并在弹出窗口中显示给用户 window 它会便于用户复制。

另一种方法是自定义重定向方案。在这里你必须安装一个自定义方案。这作为另一个应用程序(您可以调用 Shell 或批处理脚本)在验证后打开。您可以编写自己的自定义代码以将授权码复制到剪贴板并在 powershell 中读取它。 (这样可以避免手动复制粘贴)。参考这个 http://edoceo.com/howto/xfce-custom-uri-handler