MCPcopy Index your code
hub / github.com/su-kaka/gcli2api / getCredentials

Function getCredentials

front/common.js:1088–1160  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1086}
1087
1088async function getCredentials() {
1089 if (!AppState.authInProgress) {
1090 showStatus('请先获取认证链接并完成授权', 'error');
1091 return;
1092 }
1093
1094 const btn = document.getElementById('getCredsBtn');
1095 btn.disabled = true;
1096 btn.textContent = '等待OAuth回调中...';
1097
1098 try {
1099 showStatus('正在等待OAuth回调,这可能需要一些时间...', 'info');
1100
1101 const requestBody = AppState.currentProjectId ? { project_id: AppState.currentProjectId } : {};
1102
1103 const response = await fetch('./auth/callback', {
1104 method: 'POST',
1105 headers: getAuthHeaders(),
1106 body: JSON.stringify(requestBody)
1107 });
1108
1109 const data = await response.json();
1110
1111 if (response.ok) {
1112 document.getElementById('credentialsContent').textContent = JSON.stringify(data.credentials, null, 2);
1113
1114 const msg = data.auto_detected_project
1115 ? `✅ 认证成功!项目ID已自动检测为: ${data.credentials.project_id},文件已保存到: ${data.file_path}`
1116 : `✅ 认证成功!文件已保存到: ${data.file_path}`;
1117 showStatus(msg, 'success');
1118
1119 document.getElementById('credentialsSection').classList.remove('hidden');
1120 AppState.authInProgress = false;
1121 } else if (data.requires_project_selection && data.available_projects) {
1122 let projectOptions = "请选择一个项目:\n\n";
1123 data.available_projects.forEach((project, index) => {
1124 projectOptions += `${index + 1}. ${project.name} (${project.project_id})\n`;
1125 });
1126 projectOptions += `\n请输入序号 (1-${data.available_projects.length}):`;
1127
1128 const selection = prompt(projectOptions);
1129 const projectIndex = parseInt(selection) - 1;
1130
1131 if (projectIndex >= 0 && projectIndex < data.available_projects.length) {
1132 AppState.currentProjectId = data.available_projects[projectIndex].project_id;
1133 btn.textContent = '重新尝试获取认证文件';
1134 showStatus(`使用选择的项目重新尝试...`, 'info');
1135 setTimeout(() => getCredentials(), 1000);
1136 return;
1137 } else {
1138 showStatus('无效的选择,请重新开始认证', 'error');
1139 }
1140 } else if (data.requires_manual_project_id) {
1141 const userProjectId = prompt('无法自动检测项目ID,请手动输入您的Google Cloud项目ID:');
1142 if (userProjectId && userProjectId.trim()) {
1143 AppState.currentProjectId = userProjectId.trim();
1144 btn.textContent = '重新尝试获取认证文件';
1145 showStatus('使用手动输入的项目ID重新尝试...', 'info');

Callers

nothing calls this directly

Calls 2

showStatusFunction · 0.85
getAuthHeadersFunction · 0.85

Tested by

no test coverage detected