在Powershell脚本中访问Google时,我面临一个401错误。我在云控制台中创建了一个桌面项目,启用了所有Photos,创建了OAuth凭据和同意屏幕。虽然脚本将通过初始的OAuth授权,但当调用对Photos的请求时,它将失败。
代码:
$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
这将产生以下结果:
{
"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"
}
}
发布于 2021-12-24 22:44:47
开始工作了!我使用刷新令牌来代替auth令牌。此外,还需要修改ContentType = "application/x-www-form-urlencoded"
。
新代码:
$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()
}
https://stackoverflow.com/questions/70476727
复制相似问题