כדי לאפשר לסוכנים לבצע אימות בכלים חיצוניים כמו Google Maps API או Weather API, צריך להגדיר אימות יוצא באמצעות ספקי אימות של מפתחות API במנהל האימות של Agent Identity.
ספקי אימות של מפתחות API מנהלים בשבילכם את המפתחות הקריפטוגרפיים. היכולת הזו מייתרת את הצורך להטמיע מפתחות בקוד של הנציג או לנהל אותם באופן ידני.
תהליך עבודה של מפתח API
ספקי אימות של מפתחות API משתמשים בזהות של הנציג ולא דורשים הסכמה מהמשתמש. Google נוקטת אמצעים כדי לאבטח את מפתח ה-API בזמן האחסון. כשמשתמשים בערכה לפיתוח סוכנים (ADK), היא מאחזרת באופן אוטומטי את מפתח ה-API ומזינה אותו בכותרות של הפעלת הכלים.
לפני שמתחילים
- מוודאים שבחרתם בשיטת האימות הנכונה.
מפעילים את Agent Identity API, אם הוא עדיין לא מופעל.
התפקידים שנדרשים כדי להפעיל ממשקי API
כדי להפעיל ממשקי API, צריך את ההרשאה
serviceusage.services.enable. אם יצרתם את הפרויקט, סביר להניח שכבר יש לכם את ההרשאה הזו דרך התפקיד "בעלים" (roles/owner). אחרת, תוכלו לקבל את ההרשאה הזו דרך התפקיד "אדמין בממשק 'שימוש בשירות'" (roles/serviceusage.serviceUsageAdmin). איך מקצים תפקידיםמקבלים מפתח API מהשירות של הצד השלישי שאליו רוצים להתחבר.
התפקידים הנדרשים
כדי לקבל את ההרשאות שדרושות ליצירה של ספק אימות באמצעות מפתח API ולשימוש בו, בקשו מהאדמין להקצות לכם את תפקידי ה-IAM הבאים בפרויקט:
-
כדי ליצור ספקי אימות:
- אדמין של Agent Identity (
roles/agentidentity.admin) - עריכת זהות הסוכן (
roles/agentidentity.editor)
- אדמין של Agent Identity (
-
כדי להשתמש בספקי אימות:
- משתמש ב-Agent Identity (
roles/agentidentity.user) - Service Usage Consumer (
roles/serviceusage.serviceUsageConsumer)
- משתמש ב-Agent Identity (
להסבר על מתן תפקידים, ראו איך מנהלים את הגישה ברמת הפרויקט, התיקייה והארגון.
התפקידים המוגדרים מראש האלה מכילים את ההרשאות שנדרשות כדי ליצור ספק אימות של מפתח API ולהשתמש בו. כדי לראות בדיוק אילו הרשאות נדרשות, אפשר להרחיב את הקטע ההרשאות הנדרשות:
ההרשאות הנדרשות
כדי ליצור ספק אימות של מפתח API ולהשתמש בו, נדרשות ההרשאות הבאות:
-
כדי ליצור ספקי אימות:
agentidentity.authProviders.create -
כדי להשתמש בספקי אימות:
-
agentidentity.authProviders.retrieveCredentials -
aiplatform.endpoints.predict -
aiplatform.sessions.create
-
יכול להיות שתקבלו את ההרשאות האלה באמצעות תפקידים בהתאמה אישית או תפקידים מוגדרים מראש אחרים.
קבלת מפתח API מהשירות של הצד השלישי
לפני שיוצרים ספק אימות, צריך לקבל מפתח API מהשירות של הצד השלישי שאליו רוצים שהנציג יתחבר.
אם אתם מתחברים לשירות של צד שלישי מחוץ ל- Google Cloud, אתם צריכים לקבל את מפתח ה-API מפורטל המפתחים של אותו שירות ולדלג על השלבים בקטע הזה.
אם אתם מתחברים לשירותי Google Cloud (כמו Cloud Translation או מפות Google), אתם יכולים ליצור ולהגדיר מפתח API באופן הבא:
-
במסוף Google Cloud , מפעילים את שירותי ה-API הנדרשים לפרויקט:
- במסוף Google Cloud , נכנסים לדף APIs & Services > Library.
- מחפשים את ממשקי ה-API שהנציג משתמש בהם, כמו Cloud Translation API או Google Maps Weather API, ומפעילים אותם.
- מעתיקים את מחרוזת מפתח ה-API שנוצרה.
מגדירים את מפתח ה-API:
- במסוף Google Cloud , נכנסים לדף APIs & Services > Credentials.
- לוחצים על Create credentials > מפתח API.
- בתיבת הדו-שיח Create API key:
- מזינים שם ייחודי למפתח ה-API.
- כדי להגביל את המפתח לממשקי ה-API הספציפיים שהפעלתם, בוחרים את ממשקי ה-API האלה מהרשימה Select API restrictions.
- אופציונלי: בקטע Restrict your key to reduce security risks, בוחרים סוג אפליקציה כדי להגביל את הגישה.
- לוחצים על יצירה.
-
כדי לאמת את מפתח ה-API, שולחים בקשת בדיקה לנקודת הקצה של השירות.
-
כדי לאמת מפתח Cloud Translation API, מריצים את הפקודה הבאה:
curl -X POST \ -H "Content-Type: application/json" \ -H "X-goog-api-key:
YOUR_API_KEY" \ -d '{"q": "Hello world", "target": "es"}' \ "https://translation.googleapis.com/language/translate/v2"מחליפים את הערך
YOUR_API_KEYבמפתח ה-API שיצרתם. -
כדי לאמת מפתח Google Maps Weather API, מריצים את הפקודה הבאה:
curl -X GET \ "https://weather.googleapis.com/v1/currentConditions:lookup?key=
YOUR_API_KEY&location.latitude=37.4220&location.longitude=-122.0841"מחליפים את הערך
YOUR_API_KEYבמפתח ה-API שיצרתם.
אם מפתח ה-API תקין ומוגדר נכון, השירות מחזיר את הנתונים המבוקשים.
-
יצירת ספק אימות של מפתח API
כדי להגדיר את ההגדרות ופרטי הכניסה של אפליקציות צד שלישי, צריך ליצור ספק אימות.
כדי ליצור ספק אימות של מפתח API, צריך להשתמש במסוף Google Cloud , ב-Google Cloud CLI או ב-API בארכיטקטורת REST.
המסוף
- נכנסים לדף Agent Registry במסוף Google Cloud .
- לוחצים על שם הנציג שרוצים ליצור לו ספק אימות.
- לוחצים על זהות.
- בקטע Auth Providers, לוחצים על Add auth provider.
-
בחלונית Add auth provider, מזינים שם ותיאור.
השם יכול לכלול רק אותיות קטנות, מספרים או מקפים, הוא לא יכול להסתיים במקף והוא חייב להתחיל באות קטנה.
- ברשימה OAuth Type, בוחרים באפשרות מפתח API.
- לוחצים על Create and continue.
- כדי לתת לזהות של הסוכן הרשאה להשתמש בספק האימות, לוחצים על מתן גישה.
בתהליך הזה, התפקיד משתמש ב-Agent Identity (
roles/agentidentity.user) מוקצה באופן אוטומטי לזהות הסוכן במשאב של ספק האימות. - בקטע Auth provider credentials, מזינים את מפתח API.
- לוחצים על Add provider config.
ספק האימות החדש שנוצר מופיע ברשימה Auth Providers.
CLI של gcloud
-
יוצרים את ספק האימות:
gcloud agent-identity auth-providers create
AUTH_PROVIDER_NAME\ --project="PROJECT_ID" \ --location="LOCATION" \ --api-key="API_KEY" - מוודאים שספק האימות מופיע ברשימה והסטטוס שלו הוא
ENABLED:gcloud agent-identity auth-providers list \ --project="
PROJECT_ID" \ --location="LOCATION" -
נותנים הרשאות גישה כדי לאפשר לסוכן ולסביבת הפיתוח המקומית לאחזר את פרטי הכניסה מספק האימות. כדי לאפשר לנציג שנפרס ולחשבון המשתמש האישי שלכם לגשת לספק האימות, מקצים את התפקיד משתמש ב-Agent Identity (
roles/agentidentity.user) במשאב של ספק האימות:-
נותנים גישה למזהה ה-SPIFFE של הסוכן שנפרס (Agent Identity):
gcloud agent-identity auth-providers add-iam-policy-binding
AUTH_PROVIDER_NAME\ --project="PROJECT_ID" \ --location="LOCATION" \ --role="roles/agentidentity.user" \ --member="principal://agents.global.org-ORGANIZATION_ID.system.id.goog/resources/aiplatform/projects/PROJECT_NUMBER/locations/LOCATION/reasoningEngines/ENGINE_ID" -
מתן גישה לחשבון המשתמש האישי שלכם לצורך פיתוח ובדיקה מקומיים (
adk web):gcloud agent-identity auth-providers add-iam-policy-binding
AUTH_PROVIDER_NAME\ --project="PROJECT_ID" \ --location="LOCATION" \ --role="roles/agentidentity.user" \ --member="user:USER_EMAIL"
-
מחליפים את מה שכתוב בשדות הבאים:
-
PROJECT_ID: מזהה הפרויקט ב- Google Cloud . -
LOCATION: המיקום שבו נפרסו ספק האימות והנציג (לדוגמה,us-west1). -
AUTH_PROVIDER_NAME: השם של ספק האימות (לדוגמה,maps-api-key-authprovider). -
API_KEY: מפתח ה-API שיצרתם משירות הצד השלישי. -
ORGANIZATION_ID: מזהה הארגון ב- Google Cloud . -
PROJECT_NUMBER: מספר הפרויקט ב- Google Cloud . -
ENGINE_ID: המזהה של סוכן מנוע החשיבה הרציונלית שנפרס. -
USER_EMAIL: כתובת האימייל של חשבון המשתמש האישי שלכם.
REST
-
יוצרים את ספק האימות:
באמצעות
authProviders.createב-Agent Identity API אפשר ליצור ספק אימות של מפתח API בפרויקט ובמיקום.לפני שמשתמשים בנתוני הבקשה, צריך להחליף את הנתונים הבאים:
-
PROJECT_ID: מזהה הפרויקט ב- Google Cloud . מזהי פרויקטים הם מחרוזות אלפאנומריות, כמוmy-project. -
LOCATION: המיקום שבו נפרסו ספק האימות והנציג (לדוגמה,us-west1). -
AUTH_PROVIDER_NAME: השם של ספק האימות (לדוגמה,maps-api-key-authprovider). השם יכול להכיל רק אותיות קטנות, מספרים או מקפים, הוא לא יכול להסתיים במקף וחייב להתחיל באות קטנה. -
API_KEY: מפתח ה-API שיצרתם בשירות של הצד השלישי.
ה-method של ה-HTTP וכתובת ה-URL:
POST https://agentidentity.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/authProviders?authProviderId=AUTH_PROVIDER_NAME
תוכן בקשת JSON:
{ "authProviderTypeParams": { "apiKey": { "apiKey": "API_KEY" } } }כדי לשלוח את הבקשה צריך להרחיב אחת מהאפשרויות הבאות:
אתם אמורים לקבל תגובת JSON שדומה לזו:
{ "name": "projects/my-project/locations/us-west1/authProviders/maps-api-key-authprovider", "createTime": "2026-04-03T21:15:41.367155Z", "updateTime": "2026-04-03T21:15:41.367155Z", "authProviderTypeParams": { "apiKey": {} }, "state": "ENABLED" } -
-
כדי לאפשר לנציג שפרסתם ולחשבון המשתמש האישי שלכם לאחזר פרטי כניסה מספק האימות, אתם צריכים להקצות את התפקיד משתמש ב-Agent Identity (
roles/agentidentity.user) במשאב של ספק האימות:ה-method
authProviders.setIamPolicyשל Agent Identity API מגדירה את מדיניות ההרשאות במשאב של ספק האימות.לפני שמשתמשים בנתוני הבקשה, צריך להחליף את הנתונים הבאים:
-
PROJECT_ID: מזהה הפרויקט ב- Google Cloud . מזהי פרויקטים הם מחרוזות אלפאנומריות, כמוmy-project. -
LOCATION: המיקום שבו נפרסו ספק האימות והנציג (לדוגמה,us-west1). -
AUTH_PROVIDER_NAME: השם של ספק האימות. -
ORGANIZATION_ID: מזהה הארגון ב- Google Cloud. -
PROJECT_NUMBER: המספר של הפרויקט ב- Google Cloud שבו הנציג פרוס (לדוגמה,987654321098). -
ENGINE_ID: המזהה של סוכן מנוע חשיבה רציונלית שנפרס. -
USER_EMAIL: כתובת האימייל של חשבון המשתמש האישי שלכם.
ה-method של ה-HTTP וכתובת ה-URL:
POST https://agentidentity.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/authProviders/AUTH_PROVIDER_NAME:setIamPolicy
תוכן בקשת JSON:
{ "policy": { "bindings": [ { "role": "roles/agentidentity.user", "members": [ "principal://agents.global.org-ORGANIZATION_ID.system.id.goog/resources/aiplatform/projects/PROJECT_NUMBER/locations/LOCATION/reasoningEngines/ENGINE_ID", "user:USER_EMAIL" ] } ] } }כדי לשלוח את הבקשה צריך להרחיב אחת מהאפשרויות הבאות:
אתם אמורים לקבל תגובת JSON שדומה לזו:
{ "version": 1, "etag": "BwYKmjvelug=", "bindings": [ { "role": "roles/agentidentity.user", "members": [ "principal://agents.global.org-123456789012.system.id.goog/resources/aiplatform/projects/987654321098/locations/us-west1/reasoningEngines/112233445566778899", "user:alex@example.com" ] } ] } -
אימות בקוד של הנציג
כדי לאמת את הנציג, אתם יכולים להשתמש ב-ADK.
ADK
מפנים לספק האימות בקוד של הנציג באמצעות ערכת הכלים של MCP ב-ADK.
from google.adk.agents import Agent from google.adk.auth.credential_manager import CredentialManager from google.adk.integrations.agent_identity import GcpAuthProvider, GcpAuthProviderScheme from google.adk.tools.mcp_tool.mcp_session_manager import StreamableHTTPConnectionParams from google.adk.tools.mcp_tool.mcp_toolset import McpToolset from google.adk.auth.auth_tool import AuthConfig # Register Google Cloud auth provider CredentialManager.register_auth_provider(GcpAuthProvider()) # Create Google Cloud auth provider scheme # Note: If using the legacy V1 API, the resource name uses 'connectors' # instead of 'authProviders': projects/.../connectors/... auth_scheme = GcpAuthProviderScheme( name="projects/PROJECT_ID/locations/LOCATION/authProviders/AUTH_PROVIDER_NAME" ) # Configure an MCP tool with the authentication scheme. toolset = McpToolset( connection_params=StreamableHTTPConnectionParams(url="https://YOUR_MCP_SERVER_URL"), auth_scheme=auth_scheme, ) # Initialize the agent with the authenticated tools. agent = Agent( name="AGENT_NAME", model="gemini-2.5-flash", instruction="AGENT_INSTRUCTIONS", tools=[toolset], )
דוגמה: התחברות ל-MCP של מפות Google
בדוגמה הבאה מוצגת הגדרה של agent.py שמחברת נציג לשרת MCP של מפות Google:
import os from google.adk.agents import Agent from google.adk.apps import App from google.adk.auth.credential_manager import CredentialManager from google.adk.integrations.agent_identity import GcpAuthProvider, GcpAuthProviderScheme from google.adk.models import Gemini from google.adk.tools.mcp_tool.mcp_session_manager import StreamableHTTPConnectionParams from google.adk.tools.mcp_tool.mcp_toolset import McpToolset os.environ["GOOGLE_CLOUD_PROJECT"] = "PROJECT_ID" os.environ["GOOGLE_GENAI_USE_VERTEXAI"] = "True" os.environ["GOOGLE_API_USE_CLIENT_CERTIFICATE"] = "false" # Register Google Cloud auth provider for Agent Identity Credentials service CredentialManager.register_auth_provider(GcpAuthProvider()) maps_auth_scheme = GcpAuthProviderScheme( name="projects/PROJECT_ID/locations/LOCATION/authProviders/AUTH_PROVIDER_NAME" ) maps_tools = McpToolset( connection_params=StreamableHTTPConnectionParams(url="https://mapstools.googleapis.com/mcp"), auth_scheme=maps_auth_scheme, errlog=None, ) root_agent = Agent( name="root_agent", model=Gemini(model="gemini-2.5-flash"), instruction=( "You are a helpful AI assistant designed to provide accurate and useful " "information. You can also use your Google Maps tools to look up " "locations and directions." ), tools=[maps_tools], ) app = App( root_agent=root_agent, name="AGENT_NAME", )
ADK
מפנים לספק האימות בקוד של הנציג באמצעות כלי פונקציה מאומת ב-ADK.
import httpx from google.adk.agents import Agent from google.adk.auth.credential_manager import CredentialManager from google.adk.integrations.agent_identity import GcpAuthProvider from google.adk.integrations.agent_identity import GcpAuthProviderScheme from google.adk.apps import App from google.adk.auth.auth_credential import AuthCredential from google.adk.auth.auth_tool import AuthConfig from google.adk.tools.authenticated_function_tool import AuthenticatedFunctionTool from vertexai import agent_engines # First, register Google Cloud auth provider CredentialManager.register_auth_provider(GcpAuthProvider()) # Create Auth Config # Note: If using the legacy V1 API, the resource name uses 'connectors' # instead of 'authProviders': projects/.../connectors/... spotify_auth_config = AuthConfig( auth_scheme=GcpAuthProviderScheme( name="projects/PROJECT_ID/locations/LOCATION/authProviders/AUTH_PROVIDER_NAME" ) ) # Use the Auth Config in Authenticated Function Tool spotify_search_track_tool = AuthenticatedFunctionTool( func=spotify_search_track, auth_config=spotify_auth_config ) # Sample function tool async def spotify_search_track(credential: AuthCredential, query: str) -> str | list: token = None if credential.http and credential.http.credentials: token = credential.http.credentials.token if not token: return "Error: No authentication token available." async with httpx.AsyncClient() as client: response = await client.get( "https://api.spotify.com/v1/search", headers={"Authorization": f"Bearer {token}"}, params={"q": query, "type": "track", "limit": 1}, ) # Add your own logic here agent = Agent( name="AGENT_NAME", model="gemini-2.5-flash", instruction="AGENT_INSTRUCTIONS", tools=[spotify_search_track_tool], ) app = App( name="APP_NAME", root_agent=agent, ) vertex_app = agent_engines.AdkApp(app_name=app)
דוגמה: התחברות ל-Weather API של מפות Google
בדוגמה הבאה מוצגת הגדרה של agent.py שמחברת נציג ל-Google Maps Weather API באמצעות כלי פונקציה מאומת:
import os import httpx from google.adk.agents import Agent from google.adk.apps import App from google.adk.auth.auth_credential import AuthCredential from google.adk.auth.auth_tool import AuthConfig from google.adk.auth.credential_manager import CredentialManager from google.adk.integrations.agent_identity import GcpAuthProvider, GcpAuthProviderScheme from google.adk.models import Gemini from google.adk.tools.authenticated_function_tool import AuthenticatedFunctionTool os.environ["GOOGLE_CLOUD_PROJECT"] = "PROJECT_ID" os.environ["GOOGLE_GENAI_USE_VERTEXAI"] = "True" os.environ["GOOGLE_API_USE_CLIENT_CERTIFICATE"] = "false" # Register Google Cloud auth provider for Agent Identity Credentials service CredentialManager.register_auth_provider(GcpAuthProvider()) weather_auth_config = AuthConfig( auth_scheme=GcpAuthProviderScheme( name="projects/PROJECT_ID/locations/LOCATION/authProviders/AUTH_PROVIDER_NAME" ) ) async def get_weather(credential: AuthCredential, latitude: float, longitude: float) -> str | dict: """Gets current weather conditions for a location.""" api_key = None if http := credential.http: if http.additional_headers and "X-GOOG-API-KEY" in http.additional_headers: api_key = http.additional_headers["X-GOOG-API-KEY"] elif http.credentials and http.credentials.token: api_key = http.credentials.token if not api_key: return "Error: No API key available from the auth provider." params = {"location.latitude": latitude, "location.longitude": longitude, "key": api_key} async with httpx.AsyncClient() as client: response = await client.get( "https://weather.googleapis.com/v1/currentConditions:lookup", params=params, ) if response.status_code != 200: return f"Error from Weather API: {response.status_code} - {response.text}" return response.json() get_weather_tool = AuthenticatedFunctionTool( func=get_weather, auth_config=weather_auth_config ) root_agent = Agent( name="root_agent", model=Gemini(model="gemini-2.5-flash"), instruction=( "You are a helpful AI assistant. You will use your weather tool to " "look up current conditions." ), tools=[get_weather_tool], ) app = App( root_agent=root_agent, name="AGENT_NAME", )
ADK
מפנים לספק האימות בקוד של הסוכן באמצעות ערכת הכלים של Agent Registry MCP ב-ADK.
אתם לא צריכים ליצור סכימת אימות לתהליך הזה. הקישור היוצא של שרת ה-MCP כבר מתעד באיזה ספק אימות להשתמש.
from google.adk.agents import Agent from google.adk.auth.credential_manager import CredentialManager from google.adk.integrations.agent_identity import GcpAuthProvider from google.adk.integrations.agent_registry import AgentRegistry # First, register Google Cloud auth provider CredentialManager.register_auth_provider(GcpAuthProvider()) # Set Agent Registry. LOCATION must be the same region as the auth provider. registry = AgentRegistry(project_id="PROJECT_ID", location="LOCATION") # The auth provider is resolved from the MCP server's outbound binding. toolset = registry.get_mcp_toolset( mcp_server_name=( "projects/PROJECT_ID/locations/" "LOCATION/mcpServers/" "agentregistry-00000000-0000-0000-0000-000000000000" ), ) agent = Agent( name="AGENT_NAME", model="MODEL_NAME", instruction="AGENT_INSTRUCTIONS", tools=[toolset], )
פריסת הנציג
כשפורסים את הנציג ב- Google Cloud, צריך לוודא ש-Agent Identity מופעל.
Agent CLI
אם אתם משתמשים בערכה לפיתוח סוכנים (ADK) וב-Agent CLI, כדי לפרוס את הסוכן עם Agent Identity:
-
בתיקיית האפליקציה של הנציג, יוצרים קובץ תצורה בשם
.agent_engine_config.jsonכדי להפעיל את Agent Identity:echo '{ "identity_type": "AGENT_IDENTITY" }' >
AGENT_NAME/.agent_engine_config.json -
פורסים את הסוכן ב-Agent Runtime ב-Gemini Enterprise Agent Platform:
uv run adk deploy agent_engine
AGENT_NAME\ --project="PROJECT_ID" \ --region="LOCATION"מחליפים את מה שכתוב בשדות הבאים:
-
AGENT_NAME: השם של תיקיית האפליקציה של הנציג (לדוגמה,maps_agent). -
PROJECT_ID: מזהה הפרויקט ב- Google Cloud. -
LOCATION: האזור הנתמך שבו רוצים לפרוס את הנציג (לדוגמה,us-west1).
-
Python SDK
אם אתם פורסים באופן פרוגרמטי באמצעות Vertex AI Python SDK, השתמשו בדגל identity_type=AGENT_IDENTITY:
import vertexai from vertexai import types from vertexai.agent_engines import AdkApp from google.adk.auth.credential_manager import CredentialManager from google.adk.integrations.agent_identity import GcpAuthProvider # Initialize the Vertex AI client with v1beta1 API for Agent Identity support client = vertexai.Client( project="PROJECT_ID", location="LOCATION", http_options=dict(api_version="v1beta1") ) # Register the auth provider in set_up() so that it runs in the deployed # container. Use the proper wrapper class for your Agent Framework (for example, AdkApp). class AuthenticatedAdkApp(AdkApp): def set_up(self): CredentialManager.register_auth_provider(GcpAuthProvider()) super().set_up() app = AuthenticatedAdkApp(agent=agent) # Deploy the agent with Agent Identity enabled remote_app = client.agent_engines.create( agent=app, config={ "identity_type": types.IdentityType.AGENT_IDENTITY, "requirements": [ "google-cloud-aiplatform[agent_engines,adk]", "google-adk[agent-identity,mcp]>=2.7.1", ], }, )
מחליפים את מה שכתוב בשדות הבאים:
-
PROJECT_ID: מזהה הפרויקט ב- Google Cloud . -
LOCATION: האזור הנתמך שבו רוצים לפרוס את הנציג (לדוגמה,us-west1).