如何使用服务密钥 Google Api restful 端点?
How can I a Google Api restful endpoint using service key?
我正在使用邮递员记录 restful api 调用并尝试访问 google 工作表 API 端点。当我尝试访问我的端点时,它 returns:
{
"error": {
"code": 403,
"message": "The request is missing a valid API key.",
"status": "PERMISSION_DENIED"
}
}
这很公平,因为我没有使用我的 API 密钥。我创建了一个服务帐户并获得了一个 json 文件,但我计划使用休息端点进行访问,因此需要在 header 中传递令牌,但我不确定如何传递。
我查看了 json 文件,但不确定要提取什么内容才能将其传递给我的休息电话。
有没有人能成功做到这一点?
在从 Postman 调用 Google 服务之前,您需要 re-create 获取访问令牌表单服务帐户凭据的流程:
- 根据凭证文件中的数据构建和编码 JWT 负载(以填充 aud、iss、sub、iat 和 exp)
- 使用该 JWT 请求访问令牌
- 使用此访问令牌向 API 发出请求
您可以在此处找到此流程的完整指南:https://developers.google.com/identity/protocols/oauth2/service-account#authorizingrequests
这是 python 中的示例。您需要将 pycrypto 和 pyjwt 安装到 运行 这个脚本:
import requests
import json
import jwt
import time
#for RS256 you may need this
#from jwt.contrib.algorithms.pycrypto import RSAAlgorithm
#jwt.register_algorithm('RS256', RSAAlgorithm(RSAAlgorithm.SHA256))
token_url = "https://oauth2.googleapis.com/token"
credentials_file_path = "./google.json"
#build and sign JWT
def build_jwt(config):
iat = int(time.time())
exp = iat + 3600
payload = {
'iss': config["client_email"],
'sub': config["client_email"],
'aud': token_url,
'iat': iat,
'exp': exp,
'scope': 'https://www.googleapis.com/auth/spreadsheets'
}
jwt_headers = {
'kid': config["private_key_id"],
"alg": 'RS256',
"typ": 'JWT'
}
signed_jwt = jwt.encode(
payload,
config["private_key"],
headers = jwt_headers,
algorithm = 'RS256'
)
return signed_jwt
with open(credentials_file_path) as conf_file:
config = json.load(conf_file)
# 1) build and sign JWT
signed_jwt = build_jwt(config)
# 2) get access token
r = requests.post(token_url, data= {
"grant_type": "urn:ietf:params:oauth:grant-type:jwt-bearer",
"assertion": signed_jwt.decode("utf-8")
})
token = r.json()
print(f'token will expire in {token["expires_in"]} seconds')
at = token["access_token"]
print(at)
注意范围的值:https://www.googleapis.com/auth/spreadsheets
可能,您可以使用 Google API 库完成上述所有流程,具体取决于什么
你喜欢的编程语言
上面的脚本将打印访问令牌:
ya29.AHES67zeEn-RDg9CA5gGKMLKuG4uVB7W4O4WjNr-NBfY6Dtad4vbIZ
然后就可以在Authorization
header的Postman中使用了Bearer {TOKEN}
.
或使用curl:
curl "https://sheets.googleapis.com/v4/spreadsheets/$SPREADSHEET_ID" \
-H "Authorization: Bearer $ACCESS_TOKEN"
注意:您可以找到使用服务帐户密钥调用 Google 翻译 API here
的示例
我正在使用邮递员记录 restful api 调用并尝试访问 google 工作表 API 端点。当我尝试访问我的端点时,它 returns:
{
"error": {
"code": 403,
"message": "The request is missing a valid API key.",
"status": "PERMISSION_DENIED"
}
}
这很公平,因为我没有使用我的 API 密钥。我创建了一个服务帐户并获得了一个 json 文件,但我计划使用休息端点进行访问,因此需要在 header 中传递令牌,但我不确定如何传递。
我查看了 json 文件,但不确定要提取什么内容才能将其传递给我的休息电话。
有没有人能成功做到这一点?
在从 Postman 调用 Google 服务之前,您需要 re-create 获取访问令牌表单服务帐户凭据的流程:
- 根据凭证文件中的数据构建和编码 JWT 负载(以填充 aud、iss、sub、iat 和 exp)
- 使用该 JWT 请求访问令牌
- 使用此访问令牌向 API 发出请求
您可以在此处找到此流程的完整指南:https://developers.google.com/identity/protocols/oauth2/service-account#authorizingrequests
这是 python 中的示例。您需要将 pycrypto 和 pyjwt 安装到 运行 这个脚本:
import requests
import json
import jwt
import time
#for RS256 you may need this
#from jwt.contrib.algorithms.pycrypto import RSAAlgorithm
#jwt.register_algorithm('RS256', RSAAlgorithm(RSAAlgorithm.SHA256))
token_url = "https://oauth2.googleapis.com/token"
credentials_file_path = "./google.json"
#build and sign JWT
def build_jwt(config):
iat = int(time.time())
exp = iat + 3600
payload = {
'iss': config["client_email"],
'sub': config["client_email"],
'aud': token_url,
'iat': iat,
'exp': exp,
'scope': 'https://www.googleapis.com/auth/spreadsheets'
}
jwt_headers = {
'kid': config["private_key_id"],
"alg": 'RS256',
"typ": 'JWT'
}
signed_jwt = jwt.encode(
payload,
config["private_key"],
headers = jwt_headers,
algorithm = 'RS256'
)
return signed_jwt
with open(credentials_file_path) as conf_file:
config = json.load(conf_file)
# 1) build and sign JWT
signed_jwt = build_jwt(config)
# 2) get access token
r = requests.post(token_url, data= {
"grant_type": "urn:ietf:params:oauth:grant-type:jwt-bearer",
"assertion": signed_jwt.decode("utf-8")
})
token = r.json()
print(f'token will expire in {token["expires_in"]} seconds')
at = token["access_token"]
print(at)
注意范围的值:https://www.googleapis.com/auth/spreadsheets
可能,您可以使用 Google API 库完成上述所有流程,具体取决于什么 你喜欢的编程语言
上面的脚本将打印访问令牌:
ya29.AHES67zeEn-RDg9CA5gGKMLKuG4uVB7W4O4WjNr-NBfY6Dtad4vbIZ
然后就可以在Authorization
header的Postman中使用了Bearer {TOKEN}
.
或使用curl:
curl "https://sheets.googleapis.com/v4/spreadsheets/$SPREADSHEET_ID" \
-H "Authorization: Bearer $ACCESS_TOKEN"
注意:您可以找到使用服务帐户密钥调用 Google 翻译 API here
的示例