【问题标题】:Google API - Oauth Refresh Token - PowershellGoogle API - Oauth 刷新令牌 - Powershell
【发布时间】:2021-12-17 17:02:22
【问题描述】:

我正在编写一个脚本,该脚本将使用 Oauth 访问令牌下载 Google 表格。该脚本运行良好,但我在刷新我的访问令牌时遇到问题。

我在网上找到的每个指南都向我展示了以下内容的一些迭代:

 $refreshTokenParams = @{ 
      client_id=$clientId;
      client_secret=$secret;
          refresh_token=$refreshToken;
      grant_type='refresh_token';
    }

$refreshedToken = Invoke-WebRequest -Uri "https://accounts.google.com/o/oauth2/token" -Method POST -Body $refreshTokenParams 

$accesstoken = $refreshedToken.access_token 

当我运行此脚本时,它会返回以下内容:

    StatusCode        : 200
StatusDescription : OK
Content           : <!doctype html><html lang="en" dir="ltr"><head><base href="https://accounts.google.com/"><script data-id="_gd" nonce="<Hidden just in case>">window.WIZ_global_data =
                    {"Mo6CHc":-<Hidden just in case>,"O...
RawContent        : HTTP/1.1 200 OK
                    X-Frame-Options: DENY
                    Vary: Sec-Fetch-Dest, Sec-Fetch-Mode, Sec-Fetch-Site
                    google-accounts-embedded: 1
                    Pragma: no-cache
                    Transfer-Encoding: chunked
                    Strict-Transport-Security: max-...
Forms             : {}
Headers           : {[X-Frame-Options, DENY], [Vary, Sec-Fetch-Dest, Sec-Fetch-Mode, Sec-Fetch-Site], [google-accounts-embedded, 1], [Pragma, no-cache]...}
Images            : {}
InputFields       : {}
Links             : {@{innerHTML=Learn more; innerText=Learn more; outerHTML=<A href="https://developers.google.com/identity/protocols/oauth2" target=_blank jsname="erTfTe">Learn more</A>;
                    outerText=Learn more; tagName=A; href=https://developers.google.com/identity/protocols/oauth2; target=_blank; jsname=erTfTe}, @{innerHTML=Help; innerText=Help; outerHTML=<A
                    href="https://support.google.com/accounts?hl=en" target=_blank>Help</A>; outerText=Help; tagName=A; href=https://support.google.com/accounts?hl=en; target=_blank},
                    @{innerHTML=Privacy; innerText=Privacy; outerHTML=<A href="https://accounts.google.com/TOS?loc=US&amp;hl=en&amp;privacy=true" target=_blank>Privacy</A>; outerText=Privacy;
                    tagName=A; href=https://accounts.google.com/TOS?loc=US&amp;hl=en&amp;privacy=true; target=_blank}, @{innerHTML=Terms; innerText=Terms; outerHTML=<A
                    href="https://accounts.google.com/TOS?loc=US&amp;hl=en" target=_blank>Terms</A>; outerText=Terms; tagName=A; href=https://accounts.google.com/TOS?loc=US&amp;hl=en; target=_blank}}
ParsedHtml        : System.__ComObject
RawContentLength  : 1759969

当我将此输出保存到 HTML 文件时,我得到了这个 Error 400: invalid_request

错误提示“缺少必需参数:response_type”

This Google doc 提到 response_type='code' 我已将其添加到我的数组中,但没有任何影响。

我觉得this section in the guide 应该工作,但它没有。除非我实施错误?

我在将内容类型指定为 json/application 时尝试使用“Invoke-restmethod”,我使用了替代 URI,并且我已经四次检查了我的客户端 ID 和密码。我不知道我做错了什么。

如果有人有使用 Powershell 刷新 Oauth 访问令牌的经验,我将非常感谢您的帮助。

提前致谢

【问题讨论】:

    标签: powershell google-api google-oauth


    【解决方案1】:

    我不久前为 gmail api GmailSendMail.psi 写了这篇文章

    问题在于您如何发送帖子正文。需为查询参数格式。

    function RefreshAccessToken([string]$clientId,[string]$secret, [string]$refreshToken){
    
       $data = "client_id=$clientId&client_secret=$secret&refresh_token=$refreshToken&grant_type=refresh_token"
       try {
    
           $response = Invoke-RestMethod -Uri https://www.googleapis.com/oauth2/v4/token -Method POST -Body $data 
    
          return $response.access_token;
           
           
    
       } catch {
        # Dig into the exception to get the Response details.
        # Note that value__ is not a typo.
        Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__ 
        Write-Host "StatusDescription:" $_.Exception.Response.StatusDescription
        }
    
       
    }
    

    如果您有任何问题,请告诉我,如果我不能为您更新,请告诉我。

    【讨论】:

    • 嘿@Daimto 谢谢你的回复。在过去的几天里,我实际上多次引用了您链接到的该指南。因此,我尝试使用该函数并收到此错误:StatusCode: 400 StatusDescription: Bad Request 我更新了 URI 以使用 Oauth (accounts.google.com/o/oauth2/v2/auth) 提供的授权端点,然后该函数没有给我任何输出。然后我将输出保存到一个 html 文件,它与以前一样出现“缺少必需参数:response_type”错误。
    • 这段代码真的很旧,端点可能已经改变,你应该检查Discovery doc以获取更新的端点。如果您的请求仍然很糟糕,则它没有正确发送您的帖子变量。注意我的婴儿车是如何在它们之间使用 & 构建的,就像查询字符串一样。你不这样做,所以它没有检测到你的参数
    猜你喜欢
    • 2015-06-27
    • 1970-01-01
    • 2015-02-25
    • 2017-12-20
    • 2021-11-01
    • 1970-01-01
    • 2014-07-15
    • 1970-01-01
    • 2016-01-26
    相关资源
    最近更新 更多