如何通过http获取$(gcloud auth print-access-token)这个gcloudaccess-token值?
不安装gcloud如何才能获取这个access-token?
https://cloud.google.com/sdk/docs/install?hl=zh-cn
网友回复
gcloud access-token这个值谷歌cloud官方是通过gcloud 的客户端命令行获取的,需要下载到本地运行后获取
当然也有人根据这套协议写出了不需要命令行的代码,模拟了命令行的执行:
我们以cloudflare的worker为例,其他的编程语言可以通过ai更换:
// 配置变量
const PROJECT_ID = '';
const CLIENT_EMAIL = '';
const PRIVATE_KEY ="";
addEventListener("fetch", (event) => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
const [token, err] = await exchangeJwtForAccessToken(signedJWT)
if (token === null) {
console.log(`Invalid jwt token: ${err}`)
return createErrorResponse(500, "api_error", "invalid authentication credentials");
}
}
function createErrorResponse(status, errorType, message) {
const errorObject = { type: "error", error: { type: errorType, message: message } };
return new Response(JSON.stringify(errorObject), {
status: status,
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
});
}
async function createSignedJWT(email, pkey) {
pkey = pkey.replace(/-----BEGIN PRIVATE KEY-----|-----END PRIVATE KEY-----|\r|\n|\\n/g, "");
let cryptoKey = await crypto.subtle.importKey(
"pkcs8",
str2ab(atob(pkey)),
{
name: "RSASSA-PKCS1-v1_5",
hash: { name: "SHA-256" },
},
false,
["sign"]
);
const authUrl = "https://www.googleapis.com/oauth2/v4/token";
const issued = Math.floor(Date.now() / 1000);
const expires = issued + 600;
const header = {
alg: "RS256",
typ: "JWT",
};
const payload = {
iss: email,
aud: authUrl,
iat: issued,
exp: expires,
scope: "https://www.googleapis.com/auth/cloud-platform",
};
const encodedHeader = urlSafeBase64Encode(JSON.stringify(header));
const encodedPayload = urlSafeBase64Encode(JSON.stringify(payload));
const unsignedToken = `${encodedHeader}.${encodedPayload}`;
const signature = await crypto.subtle.sign(
"RSASSA-PKCS1-v1_5",
cryptoKey,
str2ab(unsignedToken)
);
const encodedSignature = urlSafeBase64Encode(signature);
ret...点击查看剩余70%
如何实现类似google的图片隐形水印添加和识别技术?
linux上如何运行任意windows程序?
ai能写出比黑客还厉害的零日漏洞等攻击工具攻击任意软件系统工程?
js如何获取浏览器的音频上下文指纹、Canvas指纹、WebGL渲染特征?
为啥ai开始抛弃markdown文本,重新偏好html文本了?
网站有没有办法鉴别访问请求是由ai操控chrome-devtools-mcp发出的?
有没有python自动操作浏览器让网站无法鉴别是机器行为?
为啥最新由Meta / 斯坦福 / 哈佛出的ProgramBench基准GPT-5.4、Claude Opus 4.7、Gemini 3.1 Pro 等全部 0% 通过率?
有没有免费的api查询域名是否完成icp工信部备案?
codex用HyperFrames与 Remotion自动做视频那个更好?


