alv*_*vas 6 oauth google-api google-translate google-cloud-platform
从https://cloud.google.com/translate/docs/basic/setup-basic,需要设置环境变量:
export GOOGLE_APPLICATION_CREDENTIALS='/path/to/credential.json'
Run Code Online (Sandbox Code Playgroud)
然后可以发出 curl 请求:
curl -s -X POST -H "Content-Type: application/json" \
-H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
--data "{
'q': 'The Great Pyramid of Giza (also known as the Pyramid of Khufu or the
Pyramid of Cheops) is the oldest and largest of the three pyramids in
the Giza pyramid complex.',
'source': 'en',
'target': 'es',
'format': 'text'
}" "https://translation.googleapis.com/language/translate/v2"
Run Code Online (Sandbox Code Playgroud)
在Bearer来自gcloud auth application-default print-access-token命令值的请求标头中有一个身份验证密钥。
在尝试多次调用 之后gcloud auth application-default print-access-token,每次调用似乎都会为每个调用创建一个唯一的令牌。
我的问题是,
身份验证密钥print-access-token在到期之前持续多长时间?
有没有办法创建一个不是动态生成的修复密钥,gcloud auth application-default print-access-token不需要设置环境变量?
有没有办法在print-access-token不调用gcloud命令行可执行文件的情况下以编程方式生成?
似乎还有一种方法可以创建静态密钥并按照https://cloud.google.com/docs/authentication/api-keys和例如https://github.com/eddiesigner/sketch- 中所述使用它translate-me/wiki/Generate-a-Google-API-Key
如何在 curl 调用中使用静态键代替Authorization: Bearer?
print-access-token 中的身份验证密钥在到期之前持续多长时间?
print-access-token为您提供一个持续时间为 1 小时的 Google 访问令牌。您可以使用令牌信息端点检查过期时间:
https://www.googleapis.com/oauth2/v3/tokeninfo?access_token=YOUR_ACCESS_TOKEN
Run Code Online (Sandbox Code Playgroud)
请注意,令牌信息既可用于检查access_token,也可用于id_token
有没有办法创建一个不是从 gcloud auth application-default print-access-token 动态生成的修复密钥,而无需设置环境变量?
您在向翻译 API 发出请求时下载的凭证文件是为服务帐户(即服务器到服务器的交互)制作的
可以gcloud使用以下小脚本重新创建CLI 使用的流程:
aud,iss,sub,iat和exp)此流程的完整指南位于此处:https : //developers.google.com/identity/protocols/oauth2/service-account#authorizingrequests
这是python 中的一个例子。您将需要安装pycrypto并pyjwt运行此脚本:
https://www.googleapis.com/oauth2/v3/tokeninfo?access_token=YOUR_ACCESS_TOKEN
Run Code Online (Sandbox Code Playgroud)
请注意,在附录中,提到某些 Google API 不需要访问令牌,只需在Authorization标头中使用 JWT 即可工作,但不适用于此翻译 API
另外,我猜您想使用谷歌客户端库来执行上述步骤,具体取决于您使用的语言
如何在 curl 调用中使用静态密钥代替 Authorization: Bearer?
您需要在 Google 控制台中生成 API 密钥(并启用翻译 API)。然后你可以直接使用:
https://translation.googleapis.com/language/translate/v2?key=YOUR_API_KEY&q=Hello%20world&target=es&alt=json&source=en
Run Code Online (Sandbox Code Playgroud)
请注意,使用www.googleapis.com也有效:
https://www.googleapis.com/language/translate/v2?key=YOUR_API_KEY&q=Hello%20world&target=es&alt=json&source=en
Run Code Online (Sandbox Code Playgroud)
使用蟒蛇:
import requests
import json
import jwt
import time
#for RS256
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/cloud-platform'
}
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"]
# 3) call translate API
r = requests.post(
"https://translation.googleapis.com/language/translate/v2",
headers = {
"Authorization": f'Bearer {at}'
},
json= {
"q": "The Great Pyramid of Giza (also known as the Pyramid of Khufu or the Pyramid of Cheops) is the oldest and largest of the three pyramids in the Giza pyramid complex.",
"source": "en",
"target": "es",
"format": "text"
})
print(r.json())
Run Code Online (Sandbox Code Playgroud)
请注意,您也可以在文档中使用POST而不是GET像这样但key作为查询参数传递:
https://translation.googleapis.com/language/translate/v2?key=YOUR_API_KEY&q=Hello%20world&target=es&alt=json&source=en
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1460 次 |
| 最近记录: |