首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >访问时出错401

访问时出错401
EN

Stack Overflow用户
提问于 2021-12-24 21:56:41
回答 1查看 2.5K关注 0票数 0

在Powershell脚本中访问Google时,我面临一个401错误。我在云控制台中创建了一个桌面项目,启用了所有Photos,创建了OAuth凭据和同意屏幕。虽然脚本将通过初始的OAuth授权,但当调用对Photos的请求时,它将失败。

代码:

代码语言:javascript
运行
复制
    $clientId = "xxxx.apps.googleusercontent.com";
    $clientSecret = "XXXXX-YYYYYYYYYYYYY";
    
    $scopes = "https://www.googleapis.com/auth/photoslibrary https://www.googleapis.com/auth/photoslibrary.readonly https://www.googleapis.com/auth/photoslibrary.readonly.appcreateddata"
    # Open browser and authorise
    Start-Process "https://accounts.google.com/o/oauth2/v2/auth?client_id=$clientId&scope=$([string]::Join("%20", $scopes))&access_type=offline&response_type=code&redirect_uri=urn:ietf:wg:oauth:2.0:oob"    
     
    # paste auth code from browser
    $code = Read-Host "Please enter the code"
       
    application/x-www-form-urlencoded -Method POST -Body "client_id=$clientid&client_secret=$clientSecret&redirect_uri=urn:ietf:wg:oauth:2.0:oob&code=$code&grant_type=authorization_code"
    $token_uri = "https://www.googleapis.com/oauth2/v4/token"
    $redirect_uri = "urn:ietf:wg:oauth:2.0:oob";
    $grant_type = "authorization_code";
    $body = @{
        code = $code;
        client_id = $clientId;
        client_secret = $clientSecret;
        redirect_uri = $redirect_uri;
        grant_type = $grant_type;
      };
    $token = Invoke-RestMethod -Uri $token_uri -Method POST -Body $body -ContentType "application/x-www-form-urlencoded"
    $token.refresh_token
    # Working good, we get token correctly !!!

    # Now the faulty block
    $requestUri = "https://photoslibrary.googleapis.com/v1/albums?pagesize=20"  
    $method = "GET"
    $Headers = @{
        Authorization = "Bearer $($token.refresh_token)";
        ContentType = "application/json";
        }
    $body = $null

    # Throws 401
    try {
        $Response = Invoke-RestMethod -Headers $Headers -Uri $requestUri -Method $method
    }
    catch {
        $streamReader = [System.IO.StreamReader]::new($_.Exception.Response.GetResponseStream())
        $ErrResp = $streamReader.ReadToEnd() | ConvertFrom-Json
        $streamReader.Close()
    }
 $ErrResp  | ConvertTo-Json 

这将产生以下结果:

代码语言:javascript
运行
复制
{
    "error":  {
                  "code":  401,
                  "message":  "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
                  "status":  "UNAUTHENTICATED"
              }
}
EN

回答 1

Stack Overflow用户

发布于 2021-12-24 22:44:47

开始工作了!我使用刷新令牌来代替auth令牌。此外,还需要修改ContentType = "application/x-www-form-urlencoded"

新代码:

代码语言:javascript
运行
复制
$clientId = "xxxx.apps.googleusercontent.com";
    $clientSecret = "XXXXX-YYYYYYYYYYYYY";
    
    $scopes = "https://www.googleapis.com/auth/photoslibrary https://www.googleapis.com/auth/photoslibrary.readonly https://www.googleapis.com/auth/photoslibrary.readonly.appcreateddata"
    # Open browser and authorise
    Start-Process "https://accounts.google.com/o/oauth2/v2/auth?client_id=$clientId&scope=$([string]::Join("%20", $scopes))&access_type=offline&response_type=code&redirect_uri=urn:ietf:wg:oauth:2.0:oob"    
     
    # paste auth code from browser
    $code = Read-Host "Please enter the code"
       
    application/x-www-form-urlencoded -Method POST -Body "client_id=$clientid&client_secret=$clientSecret&redirect_uri=urn:ietf:wg:oauth:2.0:oob&code=$code&grant_type=authorization_code"
    $token_uri = "https://www.googleapis.com/oauth2/v4/token"
    $redirect_uri = "urn:ietf:wg:oauth:2.0:oob";
    $grant_type = "authorization_code";
    $body = @{
        code = $code;
        client_id = $clientId;
        client_secret = $clientSecret;
        redirect_uri = $redirect_uri;
        grant_type = $grant_type;
      };
    $token = Invoke-RestMethod -Uri $token_uri -Method POST -Body $body -ContentType "application/x-www-form-urlencoded"
    $token.refresh_token
    $access_token = $token.access_token
$requestUri = "https://photoslibrary.googleapis.com/v1/albums"  
$method = "GET"
$Headers = @{
    Authorization = "Bearer $access_token";
    ContentType = "application/x-www-form-urlencoded";
    }
    $body = $null

    # Throws 401
    try {
        $Response = Invoke-RestMethod -Headers $Headers -Uri $requestUri -Method $method
    }
    catch {
        $streamReader = [System.IO.StreamReader]::new($_.Exception.Response.GetResponseStream())
        $ErrResp = $streamReader.ReadToEnd() | ConvertFrom-Json
        $streamReader.Close()
    }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70476727

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档