我想在我的Google驱动器中获得可用空间/配额。我可以使用Google Drive API来实现。但是我不想通过API来做到这一点。
任何人都可以帮助我通过不带API的PowerShell通过Web请求完成此任务。我正在按图像查找以下值:
答案 0 :(得分:0)
如果您要以编程方式访问Google Drive,则必须使用Google Drive API,这是唯一的方法。 Google驱动器api的方法名为About.get
获取有关用户,用户的云端硬盘和系统功能的信息。
请求
响应
{
"storageQuota": {
"limit": "125627793408",
"usage": "14149023751",
"usageInDrive": "2661743479",
"usageInDriveTrash": "838401923"
}
}
Powershell
我尚未将Google Drive API与Powershell一起使用,但已将google analytics api与它结合使用。最困难的部分是身份验证,完整的代码在这里Google Oauth Powershell
Set-PSDebug -Off
Clear-Host
##########################################################################################################################
#
# Using refresh token to get new access token
# The access token is used to access an api by sending the access_token parm with any request.
# Access tokens are only valid for about an hour after that you will need to request a new one using your refresh_token
#
##########################################################################################################################
$clientId = "-9c6a8mimeradap3nn24ac76urlpdvhoe.apps.googleusercontent.com";
$secret = "jUFTGhA8Z7FelntdvUz10fP5";
$redirectURI = "urn:ietf:wg:oauth:2.0:oob";
$refreshToken = "1/t8yW_v0gnqudE3y0_J6RKOqV5ek25Whogp49leCGqt8";
$refreshTokenParams = @{
client_id=$clientId;
client_secret=$secret;
refresh_token=$refreshToken;
grant_type='refresh_token';
}
$token = Invoke-RestMethod -Uri "https://accounts.google.com/o/oauth2/token" -Method POST -Body $refreshTokenParams
"Response:"
"Access Token: $($token.access_token)"
之后,应该只是
的问题$data = Invoke-RestMethod -ContentType 'application/json' -Uri "https://www.googleapis.com/drive/v3/about?access_token=$($token.access_token)" -Method GET
我没有时间从我的分析示例中测试这种有根据的猜测。
答案 1 :(得分:0)
感谢您的回答,但是我已经使用Google Drive API完成了。但是我的问题是,我需要从100多个帐户中获取此数据,我无法配置每个帐户以启用API并创建每个帐户的凭据。这就是我问是否可以通过使用简单的gmail URL或任何其他方法调用web-request而无需API即可实现的原因。
#GDrive variable
$ClientID = "<clientID>"
$ClientSecret = "<Secret>"
$RefreshToken = "<refreshtoken>"
function Get-GdriveAccessToken {
$params = @{
Uri = 'https://accounts.google.com/o/oauth2/token'
Body = @(
"refresh_token=$RefreshToken", # Replace $RefreshToken with your refresh token
"client_id=$ClientID", # Replace $ClientID with your client ID
"client_secret=$ClientSecret", # Replace $ClientSecret with your client secret
"grant_type=refresh_token"
) -join '&'
Method = 'Post'
ContentType = 'application/x-www-form-urlencoded'
}
$accessToken = (Invoke-RestMethod @params).access_token
}
function get-percentage {
# Set the authentication headers
$headers = @{
'Authorization' = "Bearer $accessToken"
'Content-type' = 'application/json'
}
$storage = Invoke-RestMethod -Uri "https://www.googleapis.com/drive/v2/about" -Headers $headers
$total = $storage.quotaBytesTotal
$used = $storage.quotaBytesUsed
$percentage = [math]::round($used/$total * 100)
}
try { Get-GDriveAccessToken }
CATCH { write-host " Error Occured duirng Access token grant, see below for the error details "
$Gerror_1 = $_.exception
write-host $Gerror_1.status -ForegroundColor Red
write-host $Gerror_1.Response -ForegroundColor Red
write-host $Gerror_1.Message -ForegroundColor Red
write-host
break }
try { get-percentage }
CATCH { write-host " Error Occured duirng GDrive Access, see below for the error details "
$Gerror_2 = $_.exception
write-host $Gerror_2.status -ForegroundColor Red
write-host $Gerror_2.Response -ForegroundColor Red
write-host $Gerror_2.Message -ForegroundColor Red
write-host
break }