如何在不使用API 的情况下获得Google云端硬盘的可用空间?

问题描述 投票:1回答:2

我想在我的谷歌硬盘中获得可用空间/配额。我可以使用Google Drive API实现这一目标。但我不想通过API来做到这一点。

任何人都可以帮助我通过没有API的PowerShell通过Web请求完成此操作。我按照图片查找以下值:

powershell google-drive-api gmail webrequest
2个回答
0
投票

如果您想以编程方式访问Google云端硬盘,那么您将不得不使用Google Drive API。谷歌驱动器api有一个名为About.get的方法

获取有关用户,用户的驱动器和系统功能的信息。

请求

GET https://www.googleapis.com/drive/v3/about

响应

{
 "storageQuota": {
  "limit": "125627793408",
  "usage": "14149023751",
  "usageInDrive": "2661743479",
  "usageInDriveTrash": "838401923"
 }
}

电源外壳

我没有使用与PowerShell的谷歌驱动器API,但我已经使用了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 

我没有时间从我的分析示例中测试这种有根据的猜测。


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 }
© www.soinside.com 2019 - 2024. All rights reserved.