当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 使用 Powershell 连接到 Google API

作者:精品下载站 日期:2024-12-14 18:46:03 浏览:14 分类:玩电脑

使用 Powershell 连接到 Google API


我们将使用 OAuth2.0 和 Powershell Invoke-RestMethod cmdlet 连接到 Google API。在开始在 Powershell 中编写脚本之前,我们首先需要获取 ClientId、ClientSecret、AuthCode,最后是访问令牌和刷新令牌。我花了半天时间才弄清楚如何连接到哪里才能得到什么,所以我希望你们(和女孩)能够在 10 分钟内上手并运行。

获取 Google API 访问和刷新令牌

转至 Google 开发者控制台,点击顶部栏上的 API 项目,然后点击 + 来创建新项目。为您的项目命名并单击“创建”。完成后,再次单击“API 项目”,单击全部并选择您新创建的项目。

您将看到空的仪表板,我们需要选择要与之交互的 API,我想获取实时分析数据,因此我选择了分析 API。单击启用,然后您将被重定向回仪表板。

如仪表板上所述,单击“创建凭据”并输入以下内容:

  • 您使用的是哪个 API? > 您之前选择的 API

  • 您将从哪里调用 API? > 网络浏览器 (Javascript)

  • 您将访问哪些数据? > 用户数据

  • 授权重定向 URI > http://localhost/oauth2callback !重要

  • 填写您的 OAuth 2.0 客户端 ID 的名称,什么都可以。

  • 创建 OAuth 2.0 同意屏幕并单击完成。我们将从凭据页面获取凭据。

[玩转系统] 使用 Powershell 连接到 Google API

获取授权码

通过授权码,我们可以获得访问刷新令牌。我们只需要这些,所以我采取了简单的方法,只需在浏览器中打开端点,验证并从地址栏中删除代码。

首先,我们需要 ClientId,因此请转到 Credential 页面并复制 clientId。它看起来像 864312357012-ftkcasd2f23123plh0tfivvsvasd32123k6b.apps.googleusercontent.com,将其粘贴到以下网址并在浏览器中打开:

# Replace <CLIENT_ID_HERE> with your client id
https://accounts.google.com/o/oauth2/auth?redirect_uri=<CLIENT_ID_HERE>&scope=https://www.googleapis.com/auth/analytics.readonly&approval_prompt=force&access_type=offline

# In the comments below, Phani mentioned that the above url is wrong and should be:
https://accounts.google.com/o/oauth2/auth?client_id=<replacemewithclientid>&scope=https://www.googleapis.com/auth/analytics.readonly&response_type=code&redirect_uri=<replacemewithredirecturi>&access_type=offline&approval_prompt=force

# I have no time to test it, but if you get an error with the original URL, then check the one mentioned by Phani.

确保您使用与我们创建凭据时设置的相同的重定向 URL。 http://localhost/oauth2callback

通过身份验证后,您将被重定向到 http://localhost/oauth2callback?code=4/QhUXhB*********_************u8jKZkjhsd2# 。复制末尾不带#的代码并将其存储在某处,稍后我们将需要它。
此代码只有一小时有效,如果访问令牌请求稍后失败,您将需要获取新的身份验证代码。所以记住这一点。

获取访问和刷新令牌

为了用授权代码交换令牌,我们将使用 Powershell 调用 https://www.googleapis.com/oauth2/v4/token。填写授权码、ClienId、客户端密钥并从 Google 开发者控制台重定向 Uri,然后运行脚本。它将把两个标记存储在文本文件中,以便您稍后可以使用它们

$requestUri = "https://www.googleapis.com/oauth2/v4/token"

$body = @{
	code=<authcode>;
	client_id=<clientId>;
	client_secret=<clientSecret>;
	redirect_uri=<redirectUrl>;
	grant_type="authorization_code"; # Fixed value
};

$tokens = Invoke-RestMethod -Uri $requestUri -Method POST -Body $body;

# Store refreshToken
Set-Content $PSScriptRoot"\refreshToken.txt" $tokens.refresh_token

# Store accessToken
Set-Content $PSScriptRoot"\accessToken.txt" $tokens.access_token

刷新访问令牌

访问令牌的有效期仅为一小时,因此您需要使用刷新令牌获取新的访问令牌。来自 Google 的刷新令牌不会过期,因此请将其存储在安全的地方。使用下面的调用来获取新的访问令牌:

$refreshTokenParams = @{
	client_id=$clientId;
  	client_secret=$clientSecret;
	refresh_token=$refreshToken;
	grant_type="refresh_token"; # Fixed value
}

$tokens = Invoke-RestMethod -Uri $requestUri -Method POST -Body $refreshTokenParams

从 Google API 检索数据

现在我们有了可以从 Google 获取数据的令牌。要开始使用,请查看 https://developers.google.com/oauthplayground/ 和 https://developers.google.com/apis-explorer/

举个例子,以下是从 Analytics 检索当前(实时)用户的方法:

# Request URL for Analytics Realtime and requesting the activeUsers
$viewId = 12345678 (Get it from the analytics admin page > view settings)
$requestUri = "https://www.googleapis.com/analytics/v3/data/realtime?ids=ga:$viewId&metrics=rt:activeUsers"

Invoke-RestMethod -Headers @{Authorization = "Bearer $accessToken"} -Uri $requestUri -Method GET -ContentType 'application/json'

更新:检查此 Google Api Explorer:https://ga-dev-tools.appspot.com/query-explorer/。它会为您生成一个 API 查询 URI。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯