āļŦāļ™āđ‰āļēāļ™āļĩāđ‰āļ­āļ˜āļīāļšāļēāļĒāļ§āļīāļ˜āļĩāļŠāļĢāđ‰āļēāļ‡āđāļĨāļ°āļˆāļąāļ”āļāļēāļĢāļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢāđ‚āļ”āļĒāđƒāļŠāđ‰ Identity and Access Management (IAM) API, Google Cloud Console āđāļĨāļ° āļ„āļģāļŠāļąāđˆāļ‡ gcloud- āđ€āļ„āļĢāļ·āđˆāļ­āļ‡āļĄāļ·āļ­āļšāļĢāļĢāļ—āļąāļ” āļ•āļēāļĄāļ„āđˆāļēāđ€āļĢāļīāđˆāļĄāļ•āđ‰āļ™ āđāļ•āđˆāļĨāļ°āđ‚āļ„āļĢāļ‡āļāļēāļĢāļŠāļēāļĄāļēāļĢāļ–āļĄāļĩāļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢāđ„āļ”āđ‰āļŠāļđāļ‡āļŠāļļāļ” 100 āļšāļąāļāļŠāļĩāļ—āļĩāđˆāļ„āļ§āļšāļ„āļļāļĄāļāļēāļĢāđ€āļ‚āđ‰āļēāļ–āļķāļ‡āļ—āļĢāļąāļžāļĒāļēāļāļĢāļ‚āļ­āļ‡āļ„āļļāļ“ āļ„āļļāļ“āļŠāļēāļĄāļēāļĢāļ–āļ‚āļ­āđ€āļžāļīāđˆāļĄāđ‚āļ„āļ§āļ•āđ‰āļēāđ„āļ”āđ‰āļŦāļēāļāļˆāļģāđ€āļ›āđ‡āļ™ āđ€āļĢāļĩāļĒāļ™āļĢāļđāđ‰āđ€āļžāļīāđˆāļĄāđ€āļ•āļīāļĄāđ€āļāļĩāđˆāļĒāļ§āļāļąāļšāđ‚āļ„āļ§āļ•āđ‰āļēāđāļĨāļ°āļ‚āļĩāļ”āļˆāļģāļāļąāļ” ## āļāđˆāļ­āļ™āļ—āļĩāđˆāļˆāļ°āđ€āļĢāļīāđˆāļĄ āđ€āļ›āļīāļ”āđƒāļŠāđ‰āļ‡āļēāļ™ IAM API āļ—āļģāļ„āļ§āļēāļĄāđ€āļ‚āđ‰āļēāđƒāļˆāļāļąāļšāļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ IAM āļ•āļīāļ”āļ•āļąāđ‰āļ‡ Google Cloud CLI āļšāļ—āļšāļēāļ—āļ—āļĩāđˆāļˆāļģāđ€āļ›āđ‡āļ™ āļŦāļēāļāļ•āđ‰āļ­āļ‡āļāļēāļĢāļĢāļąāļšāļŠāļīāļ—āļ˜āļīāđŒāļ—āļĩāđˆāļˆāļģāđ€āļ›āđ‡āļ™āđƒāļ™āļāļēāļĢāļˆāļąāļ”āļāļēāļĢāļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ āđ‚āļ›āļĢāļ”āļ‚āļ­āđƒāļŦāđ‰āļœāļđāđ‰āļ”āļđāđāļĨāļĢāļ°āļšāļšāļĄāļ­āļšāļšāļ—āļšāļēāļ— IAM āļ•āđˆāļ­āđ„āļ›āļ™āļĩāđ‰āđƒāļŦāđ‰āļāļąāļšāļ„āļļāļ“āđƒāļ™āđ‚āļ›āļĢāđ€āļˆāđ‡āļāļ•āđŒ: - āļŦāļēāļāļ•āđ‰āļ­āļ‡āļāļēāļĢāļ”āļđāļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢāđāļĨāļ°āļ‚āđ‰āļ­āļĄāļđāļĨāđ€āļĄāļ•āļēāļ‚āļ­āļ‡āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ: āļ”āļđāļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ ( āļšāļ—āļšāļēāļ—/iam.serviceAccountViewer) - āļŦāļēāļāļ•āđ‰āļ­āļ‡āļāļēāļĢāļ”āļđāđāļĨāļ°āļŠāļĢāđ‰āļēāļ‡āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ: āļŠāļĢāđ‰āļēāļ‡āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ ( āļšāļ—āļšāļēāļ—/iam.serviceAccountCreator) - āļŦāļēāļāļ•āđ‰āļ­āļ‡āļāļēāļĢāļ”āļđāđāļĨāļ°āļĨāļšāļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ: āļĨāļšāļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ ( āļšāļ—āļšāļēāļ—/iam.serviceAccountDeleter) - āđƒāļ™āļāļēāļĢāļˆāļąāļ”āļāļēāļĢ (āļ”āļđ āļŠāļĢāđ‰āļēāļ‡ āļ­āļąāļ›āđ€āļ”āļ• āļ›āļīāļ”āđƒāļŠāđ‰āļ‡āļēāļ™ āđ€āļ›āļīāļ”āđƒāļŠāđ‰ āļĨāļš āļĒāļāđ€āļĨāļīāļāļāļēāļĢāļĨāļš āđāļĨāļ°āļˆāļąāļ”āļāļēāļĢāļāļēāļĢāđ€āļ‚āđ‰āļēāļ–āļķāļ‡) āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢāļ­āļĒāđˆāļēāļ‡āļŠāļĄāļšāļđāļĢāļ“āđŒ: āļœāļđāđ‰āļ”āļđāđāļĨāļĢāļ°āļšāļšāļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ ( āļšāļ—āļšāļēāļ—/iam.serviceAccountAdmin) āļŠāļģāļŦāļĢāļąāļšāļ‚āđ‰āļ­āļĄāļđāļĨāđ€āļžāļīāđˆāļĄāđ€āļ•āļīāļĄāđ€āļāļĩāđˆāļĒāļ§āļāļąāļšāļāļēāļĢāđƒāļŦāđ‰āļŠāļīāļ—āļ˜āļīāđŒāļšāļ—āļšāļēāļ— āļ”āļđāļˆāļąāļ”āļāļēāļĢāļāļēāļĢāđ€āļ‚āđ‰āļēāļ–āļķāļ‡ āļŦāļēāļāļ•āđ‰āļ­āļ‡āļāļēāļĢāđ€āļĢāļĩāļĒāļ™āļĢāļđāđ‰āđ€āļžāļīāđˆāļĄāđ€āļ•āļīāļĄāđ€āļāļĩāđˆāļĒāļ§āļāļąāļšāļšāļ—āļšāļēāļ—āđ€āļŦāļĨāđˆāļēāļ™āļĩāđ‰ āđ‚āļ›āļĢāļ”āļ”āļđāļšāļ—āļšāļēāļ—āļ‚āļ­āļ‡āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ āļšāļ—āļšāļēāļ—āļžāļ·āđ‰āļ™āļāļēāļ™āļ‚āļ­āļ‡ IAM āļĒāļąāļ‡āļ›āļĢāļ°āļāļ­āļšāļ”āđ‰āļ§āļĒāļŠāļīāļ—āļ˜āļīāđŒāđƒāļ™āļāļēāļĢāļˆāļąāļ”āļāļēāļĢāļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ āļ„āļļāļ“āđ„āļĄāđˆāļ„āļ§āļĢāđƒāļŦāđ‰āļŠāļīāļ—āļ˜āļīāđŒāļšāļ—āļšāļēāļ—āļžāļ·āđ‰āļ™āļāļēāļ™āđƒāļ™āļŠāļ āļēāļžāđāļ§āļ”āļĨāđ‰āļ­āļĄāļāļēāļĢāđƒāļŠāđ‰āļ‡āļēāļ™āļˆāļĢāļīāļ‡ āđāļ•āđˆāļ„āļļāļ“āļŠāļēāļĄāļēāļĢāļ–āđƒāļŦāđ‰āļŠāļīāļ—āļ˜āļīāđŒāđ„āļ”āđ‰āđƒāļ™āļŠāļ āļēāļžāđāļ§āļ”āļĨāđ‰āļ­āļĄāļāļēāļĢāļžāļąāļ’āļ™āļēāļŦāļĢāļ·āļ­āļāļēāļĢāļ—āļ”āļŠāļ­āļš ## āļāļēāļĢāļŠāļĢāđ‰āļēāļ‡āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ āđ€āļĄāļ·āđˆāļ­āļ„āļļāļ“āļŠāļĢāđ‰āļēāļ‡āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ āļ„āļļāļ“āļ•āđ‰āļ­āļ‡āļĢāļ°āļšāļļāļĢāļŦāļąāļŠāļ—āļĩāđˆāđ€āļ›āđ‡āļ™āļ•āļąāļ§āļ­āļąāļāļĐāļĢāđāļĨāļ°āļ•āļąāļ§āđ€āļĨāļ‚āļ„āļĨāļ°āļāļąāļ™ ( āđƒāļ™āļ•āļąāļ§āļ­āļĒāđˆāļēāļ‡āļ”āđ‰āļēāļ™āļĨāđˆāļēāļ‡) āđ€āļŠāđˆāļ™ `SA_NAME` āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢāļ‚āļ­āļ‡āļ‰āļąāļ™ ID āļ•āđ‰āļ­āļ‡āļĄāļĩāļ„āļ§āļēāļĄāļĒāļēāļ§āļĢāļ°āļŦāļ§āđˆāļēāļ‡ 6 āļ–āļķāļ‡ 30 āļ•āļąāļ§āļ­āļąāļāļĐāļĢ āđāļĨāļ°āļŠāļēāļĄāļēāļĢāļ– āļĄāļĩāļ­āļąāļāļ‚āļĢāļ°āļ—āļĩāđˆāđ€āļ›āđ‡āļ™āļ•āļąāļ§āļ­āļąāļāļĐāļĢāļžāļīāļĄāļžāđŒāđ€āļĨāđ‡āļāđāļĨāļ°āļ•āļąāļ§āđ€āļĨāļ‚āļ„āļĨāļ°āļāļąāļ™āđāļĨāļ°āļ‚āļĩāļ”āļāļĨāļēāļ‡ āļŦāļĨāļąāļ‡āļˆāļēāļāļ—āļĩāđˆāļ„āļļāļ“āļŠāļĢāđ‰āļēāļ‡āļšāļĢāļīāļāļēāļĢ āļšāļąāļāļŠāļĩāļ„āļļāļ“āđ„āļĄāđˆāļŠāļēāļĄāļēāļĢāļ–āđ€āļ›āļĨāļĩāđˆāļĒāļ™āļŠāļ·āđˆāļ­āđ„āļ”āđ‰ āļŠāļ·āđˆāļ­āļ‚āļ­āļ‡āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢāļ›āļĢāļēāļāļāđƒāļ™āļ—āļĩāđˆāļ­āļĒāļđāđˆāļ­āļĩāđ€āļĄāļĨāļ—āļĩāđˆāļˆāļąāļ”āđ€āļ•āļĢāļĩāļĒāļĄāđ„āļ§āđ‰ āļĢāļ°āļŦāļ§āđˆāļēāļ‡āļāļēāļĢāļŠāļĢāđ‰āļēāļ‡āđƒāļ™āļĢāļđāļ›āđāļšāļš `SA_NAME`@ `PROJECT_ID`.iam.gserviceaccount.com āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢāđāļ•āđˆāļĨāļ°āļšāļąāļāļŠāļĩāļĒāļąāļ‡āļĄāļĩ ID āļ•āļąāļ§āđ€āļĨāļ‚āļ—āļĩāđˆāđ„āļĄāđˆāļ‹āđ‰āļģāđāļšāļšāļ–āļēāļ§āļĢ āļ‹āļķāđˆāļ‡āļŠāļĢāđ‰āļēāļ‡āļ‚āļķāđ‰āļ™āđ‚āļ”āļĒāļ­āļąāļ•āđ‚āļ™āļĄāļąāļ•āļī āļ„āļļāļ“āļĒāļąāļ‡āļĢāļ°āļšāļļāļ‚āđ‰āļ­āļĄāļđāļĨāļ•āđˆāļ­āđ„āļ›āļ™āļĩāđ‰āđ€āļĄāļ·āđˆāļ­āļ„āļļāļ“āļŠāļĢāđ‰āļēāļ‡āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ: āđ€āļ›āđ‡āļ™āļ„āļģāļ­āļ˜āļīāļšāļēāļĒāļ—āļĩāđˆāđ„āļĄāđˆāļšāļąāļ‡āļ„āļąāļšāļŠāļģāļŦāļĢāļąāļšāļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ SA_DESCRIPTION āđ€āļ›āđ‡āļ™āļŠāļ·āđˆāļ­āļ—āļĩāđˆāđ€āļĢāļĩāļĒāļāļ‡āđˆāļēāļĒāļŠāļģāļŦāļĢāļąāļšāļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ SA_DISPLAY_NAME āļ„āļ·āļ­ ID āļ‚āļ­āļ‡āđ‚āļ„āļĢāļ‡āļāļēāļĢ Google Cloud āļ‚āļ­āļ‡āļ„āļļāļ“ PROJECT_ID āļŦāļĨāļąāļ‡āļˆāļēāļāļ—āļĩāđˆāļ„āļļāļ“āļŠāļĢāđ‰āļēāļ‡āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ āļ„āļļāļ“āļ­āļēāļˆāļ•āđ‰āļ­āļ‡āļĢāļ­āļ­āļĒāđˆāļēāļ‡āļ™āđ‰āļ­āļĒ 60 āļ§āļīāļ™āļēāļ—āļĩāļāđˆāļ­āļ™āļ—āļĩāđˆāļˆāļ°āđƒāļŠāđ‰āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ āļĨāļąāļāļĐāļ“āļ°āļāļēāļĢāļ—āļģāļ‡āļēāļ™āļ™āļĩāđ‰āđ€āļāļīāļ”āļ‚āļķāđ‰āļ™āđ€āļ™āļ·āđˆāļ­āļ‡āļˆāļēāļāļāļēāļĢāļ”āļģāđ€āļ™āļīāļ™āļāļēāļĢāļ­āđˆāļēāļ™āļˆāļ°āļŠāļ­āļ”āļ„āļĨāđ‰āļ­āļ‡āļāļąāļ™āđƒāļ™āļ—āļĩāđˆāļŠāļļāļ” āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢāđƒāļŦāļĄāđˆāļ­āļēāļˆāļ•āđ‰āļ­āļ‡āđƒāļŠāđ‰āđ€āļ§āļĨāļēāļˆāļķāļ‡āļˆāļ°āļĄāļ­āļ‡āđ€āļŦāđ‡āļ™āđ„āļ”āđ‰ āļŦāļēāļāļ„āļļāļ“āļžāļĒāļēāļĒāļēāļĄāļ­āđˆāļēāļ™āļŦāļĢāļ·āļ­āđƒāļŠāđ‰āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢāļ—āļąāļ™āļ—āļĩāļŦāļĨāļąāļ‡āļˆāļēāļāļ—āļĩāđˆāļ„āļļāļ“āļŠāļĢāđ‰āļēāļ‡āļšāļąāļāļŠāļĩāļ™āļąāđ‰āļ™ āđāļĨāļ°āļ„āļļāļ“āđ„āļ”āđ‰āļĢāļąāļšāļ‚āđ‰āļ­āļœāļīāļ”āļžāļĨāļēāļ” āļ„āļļāļ“āļŠāļēāļĄāļēāļĢāļ–āļĨāļ­āļ‡āļ„āļģāļ‚āļ­āļ­āļĩāļāļ„āļĢāļąāđ‰āļ‡āđ‚āļ”āļĒāđƒāļŠāđ‰āļāļēāļĢāļ–āļ­āļĒāļāļĨāļąāļšāđāļšāļšāđ€āļ­āđ‡āļāļ‹āđŒāđ‚āļ›āđ€āļ™āļ™āđ€āļŠāļĩāļĒāļĨ āļ„āļ­āļ™āđ‚āļ‹āļĨ āđƒāļ™āļ„āļ­āļ™āđ‚āļ‹āļĨ Google Cloud āđƒāļŦāđ‰āđ„āļ›āļ—āļĩāđˆ āļŠāļĢāđ‰āļēāļ‡āļŦāļ™āđ‰āļēāļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ āđ„āļ›āļ—āļĩāđˆāļŠāļĢāđ‰āļēāļ‡āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ āđ€āļĨāļ·āļ­āļāđ‚āļ„āļĢāļ‡āļāļēāļĢāļ„āļĨāļēāļ§āļ”āđŒ āļ›āđ‰āļ­āļ™āļŠāļ·āđˆāļ­āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢāļ—āļĩāđˆāļˆāļ°āđāļŠāļ”āļ‡āđƒāļ™ Google Cloud Console Google Cloud Console āļŠāļĢāđ‰āļēāļ‡āļĢāļŦāļąāļŠāļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢāļ•āļēāļĄāļŠāļ·āđˆāļ­āļ™āļĩāđ‰ āđāļāđ‰āđ„āļ‚ ID āļŦāļēāļāļˆāļģāđ€āļ›āđ‡āļ™ āļ„āļļāļ“āđ„āļĄāđˆāļŠāļēāļĄāļēāļĢāļ–āđ€āļ›āļĨāļĩāđˆāļĒāļ™ ID āđ„āļ”āđ‰āđƒāļ™āļ āļēāļĒāļŦāļĨāļąāļ‡ āđ„āļĄāđˆāļšāļąāļ‡āļ„āļąāļš: āļ›āđ‰āļ­āļ™āļ„āļģāļ­āļ˜āļīāļšāļēāļĒāļ‚āļ­āļ‡āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ āļŦāļēāļāļ„āļļāļ“āđ„āļĄāđˆāļ•āđ‰āļ­āļ‡āļāļēāļĢāļ•āļąāđ‰āļ‡āļ„āđˆāļēāļāļēāļĢāļ„āļ§āļšāļ„āļļāļĄāļāļēāļĢāđ€āļ‚āđ‰āļēāļ–āļķāļ‡āļ•āļ­āļ™āļ™āļĩāđ‰ āđƒāļŦāđ‰āļ„āļĨāļīāļ āđ€āļŠāļĢāđ‡āļˆāļŠāļīāđ‰āļ™āđ€āļžāļ·āđˆāļ­āļŠāļĢāđ‰āļēāļ‡āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢāđƒāļŦāđ‰āđ€āļŠāļĢāđ‡āļˆāļŠāļīāđ‰āļ™ āļŦāļēāļāļ•āđ‰āļ­āļ‡āļāļēāļĢāļ•āļąāđ‰āļ‡āļ„āđˆāļēāļāļēāļĢāļ„āļ§āļšāļ„āļļāļĄāļāļēāļĢāđ€āļ‚āđ‰āļēāļ–āļķāļ‡āļ•āļ­āļ™āļ™āļĩāđ‰ āđƒāļŦāđ‰āļ„āļĨāļīāļ āļŠāļĢāđ‰āļēāļ‡āđāļĨāļ°āļ”āļģāđ€āļ™āļīāļ™āļāļēāļĢāļ•āđˆāļ­āđāļĨāļ°āļ”āļģāđ€āļ™āļīāļ™āļāļēāļĢāļ•āđˆāļ­āđ„āļ›āļĒāļąāļ‡āļ‚āļąāđ‰āļ™āļ•āļ­āļ™āļ–āļąāļ”āđ„āļ› āđ„āļĄāđˆāļšāļąāļ‡āļ„āļąāļš: āđ€āļĨāļ·āļ­āļāļšāļ—āļšāļēāļ— IAM āļ­āļĒāđˆāļēāļ‡āļ™āđ‰āļ­āļĒāļŦāļ™āļķāđˆāļ‡āļĢāļēāļĒāļāļēāļĢāđ€āļžāļ·āđˆāļ­āļĄāļ­āļšāđƒāļŦāđ‰āļāļąāļšāļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢāđƒāļ™āđ‚āļ„āļĢāļ‡āļāļēāļĢ āđ€āļĄāļ·āđˆāļ­āļ„āļļāļ“āđ€āļžāļīāđˆāļĄāļšāļ—āļšāļēāļ—āđ€āļŠāļĢāđ‡āļˆāđāļĨāđ‰āļ§ āđƒāļŦāđ‰āļ„āļĨāļīāļ āļ”āļģāđ€āļ™āļīāļ™āļāļēāļĢāļ•āđˆāļ­ āļ—āļēāļ‡āđ€āļĨāļ·āļ­āļ: āđƒāļ™ āļŠāđˆāļ­āļ‡āļšāļ—āļšāļēāļ—āļ‚āļ­āļ‡āļœāļđāđ‰āđƒāļŠāđ‰āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ āđ€āļžāļīāđˆāļĄāļŠāļĄāļēāļŠāļīāļāļ—āļĩāđˆāļŠāļēāļĄāļēāļĢāļ–āđ€āļĨāļĩāļĒāļ™āđāļšāļšāļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢāđ„āļ”āđ‰ āļ—āļēāļ‡āđ€āļĨāļ·āļ­āļ: āđƒāļ™ āļšāļ—āļšāļēāļ—āļ‚āļ­āļ‡āļœāļđāđ‰āļ”āļđāđāļĨāļĢāļ°āļšāļšāļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ āđ€āļžāļīāđˆāļĄāļŠāļĄāļēāļŠāļīāļāļ—āļĩāđˆāļŠāļēāļĄāļēāļĢāļ–āļˆāļąāļ”āļāļēāļĢāļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢāđ„āļ”āđ‰ āļ„āļĨāļīāļ āđ€āļŠāļĢāđ‡āļˆāļŠāļīāđ‰āļ™āđ€āļžāļ·āđˆāļ­āļŠāļĢāđ‰āļēāļ‡āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢāđƒāļŦāđ‰āđ€āļŠāļĢāđ‡āļˆāļŠāļīāđ‰āļ™ gcloud CLI āļŦāļēāļāļ•āđ‰āļ­āļ‡āļāļēāļĢāļŠāļĢāđ‰āļēāļ‡āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ āđƒāļŦāđ‰āđ€āļĢāļĩāļĒāļāđƒāļŠāđ‰ āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ gcloud iam createcommand: āļŠāļĢāđ‰āļēāļ‡āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ gcloud iam SA_NAME\ --description="DESCRIPTION"\ --display-name="DISPLAY_NAME"āđāļ—āļ™āļ—āļĩāđˆāļ„āđˆāļēāļ•āđˆāļ­āđ„āļ›āļ™āļĩāđ‰: : āļŠāļ·āđˆāļ­āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ SA_NAME : āļ„āļģāļ­āļ˜āļīāļšāļēāļĒāļ—āļĩāđˆāđ„āļĄāđˆāļšāļąāļ‡āļ„āļąāļšāļ‚āļ­āļ‡āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ āļ„āļģāļ­āļ˜āļīāļšāļēāļĒ : āļŠāļ·āđˆāļ­āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢāļ—āļĩāđˆāļˆāļ°āđāļŠāļ”āļ‡āđƒāļ™ Google Cloud Console āļŠāļ·āđˆāļ­āļ—āļĩāđˆāđāļŠāļ”āļ‡ - āđ„āļĄāđˆāļšāļąāļ‡āļ„āļąāļš: āđƒāļ™āļāļēāļĢāđƒāļŦāđ‰āļŠāļīāļ—āļ˜āļīāđŒāļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢāļ‚āļ­āļ‡āļ„āļļāļ“āļāļąāļšāļšāļ—āļšāļēāļ— IAM āđƒāļ™āđ‚āļ„āļĢāļ‡āļāļēāļĢāļ‚āļ­āļ‡āļ„āļļāļ“ āđƒāļŦāđ‰āđ€āļĢāļĩāļĒāļāđƒāļŠāđ‰ āđ‚āļ„āļĢāļ‡āļāļēāļĢ gcloud add-iam-policy-bindingcommand: gcloud āđ‚āļ›āļĢāđ€āļˆāđ‡āļāļ•āđŒ add-iam-policy-binding PROJECT_ID\ --member="serviceAccount: SA_NAME@ PROJECT_ID.iam.gserviceaccount.com"\ --role="ROLE_NAME"āđāļ—āļ™āļ—āļĩāđˆāļ„āđˆāļēāļ•āđˆāļ­āđ„āļ›āļ™āļĩāđ‰: : āļĢāļŦāļąāļŠāđ‚āļ„āļĢāļ‡āļāļēāļĢ PROJECT_ID : āļŠāļ·āđˆāļ­āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ SA_NAME : āļŠāļ·āđˆāļ­āļšāļ—āļšāļēāļ— āđ€āļŠāđˆāļ™ ROLE_NAME āļšāļ—āļšāļēāļ—/compute.osāđ€āļ‚āđ‰āļēāļŠāļđāđˆāļĢāļ°āļšāļš - āđ„āļĄāđˆāļšāļąāļ‡āļ„āļąāļš: āļŦāļēāļāļ•āđ‰āļ­āļ‡āļāļēāļĢāļ­āļ™āļļāļāļēāļ•āđƒāļŦāđ‰āļœāļđāđ‰āđƒāļŠāđ‰āļ›āļĨāļ­āļĄāđāļ›āļĨāļ‡āđ€āļ›āđ‡āļ™āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ āđƒāļŦāđ‰āđ€āļĢāļĩāļĒāļāđƒāļŠāđ‰ gcloud iam service-accounts add-iam-policy-bindingcommand āđ€āļžāļ·āđˆāļ­āļĄāļ­āļšāļšāļ—āļšāļēāļ—āļœāļđāđ‰āđƒāļŠāđ‰āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢāđāļāđˆāļœāļđāđ‰āđƒāļŠāđ‰ ( roles/iam.serviceAccountUser) āđƒāļ™āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ: āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ gcloud iam add-iam-policy-binding SA_NAME@ PROJECT_ID.iam.gserviceaccount.com \ --member="user: USER_EMAIL"\ --role="roles/iam.serviceAccountUser"āđāļ—āļ™āļ—āļĩāđˆāļ„āđˆāļēāļ•āđˆāļ­āđ„āļ›āļ™āļĩāđ‰: : āļĢāļŦāļąāļŠāđ‚āļ„āļĢāļ‡āļāļēāļĢ PROJECT_ID : āļŠāļ·āđˆāļ­āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ SA_NAME : āļ—āļĩāđˆāļ­āļĒāļđāđˆāļ­āļĩāđ€āļĄāļĨāļŠāļģāļŦāļĢāļąāļšāļœāļđāđ‰āđƒāļŠāđ‰ USER_EMAIL - āļžāļąāļāļœāđˆāļ­āļ™ āđ€āļ”āļ­āļ° serviceAccounts.create āļ§āļīāļ˜āļĩāļāļēāļĢāļŠāļĢāđ‰āļēāļ‡āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ āļāđˆāļ­āļ™āđƒāļŠāđ‰āļ‚āđ‰āļ­āļĄāļđāļĨāļ„āļģāļ‚āļ­āđƒāļ”āđ† āđƒāļŦāđ‰āļ”āļģāđ€āļ™āļīāļ™āļāļēāļĢāđāļ—āļ™āļ—āļĩāđˆāļ”āļąāļ‡āļ•āđˆāļ­āđ„āļ›āļ™āļĩāđ‰: : āļĢāļŦāļąāļŠāđ‚āļ„āļĢāļ‡āļāļēāļĢ Google Cloud āļ‚āļ­āļ‡āļ„āļļāļ“ āļĢāļŦāļąāļŠāđ‚āļ„āļĢāļ‡āļāļēāļĢāļ„āļ·āļ­āļŠāļ•āļĢāļīāļ‡āļ—āļĩāđˆāđ€āļ›āđ‡āļ™āļ•āļąāļ§āļ­āļąāļāļĐāļĢāđāļĨāļ°āļ•āļąāļ§āđ€āļĨāļ‚āļ„āļĨāļ°āļāļąāļ™ āđ€āļŠāđˆāļ™ PROJECT_ID āđ‚āļ„āļĢāļ‡āļāļēāļĢāļ‚āļ­āļ‡āļ‰āļąāļ™ : ID āļ•āļąāļ§āļ­āļąāļāļĐāļĢāđāļĨāļ°āļ•āļąāļ§āđ€āļĨāļ‚āļ‚āļ­āļ‡āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢāļ‚āļ­āļ‡āļ„āļļāļ“ āļŠāļ·āđˆāļ­āļ™āļĩāđ‰āļ•āđ‰āļ­āļ‡āļĄāļĩāļ„āļ§āļēāļĄāļĒāļēāļ§āļĢāļ°āļŦāļ§āđˆāļēāļ‡ 6 āļ–āļķāļ‡ 30 āļ­āļąāļāļ‚āļĢāļ° āđāļĨāļ°āļŠāļēāļĄāļēāļĢāļ–āļĄāļĩāļ­āļąāļāļ‚āļĢāļ°āļ—āļĩāđˆāđ€āļ›āđ‡āļ™āļ•āļąāļ§āļ­āļąāļāļĐāļĢāļžāļīāļĄāļžāđŒāđ€āļĨāđ‡āļāđāļĨāļ°āļ•āļąāļ§āđ€āļĨāļ‚āļ„āļĨāļ°āļāļąāļ™āđāļĨāļ°āļ‚āļĩāļ”āļāļĨāļēāļ‡āđ„āļ”āđ‰ SA_NAME : āđ„āļĄāđˆāļˆāļģāđ€āļ›āđ‡āļ™. āļ„āļģāļ­āļ˜āļīāļšāļēāļĒāļŠāļģāļŦāļĢāļąāļšāļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ SA_DESCRIPTION : āļŠāļ·āđˆāļ­āļ—āļĩāđˆāļĄāļ™āļļāļĐāļĒāđŒāļŠāļēāļĄāļēāļĢāļ–āļ­āđˆāļēāļ™āđ„āļ”āđ‰āļŠāļģāļŦāļĢāļąāļšāļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ SA_DISPLAY_NAME āļ§āļīāļ˜āļĩāļāļēāļĢ HTTP āđāļĨāļ° URL: āđ‚āļžāļŠāļ•āđŒ httpsiam.googleapis.com/v1/projects/ PROJECT_ID/āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ āļ‚āļ­āđ€āļ™āļ·āđ‰āļ­āļ„āļ§āļēāļĄ JSON: { "āļŦāļĄāļēāļĒāđ€āļĨāļ‚āļšāļąāļāļŠāļĩ": "SA_NAME", "serviceAccount": { "description": "SA_DESCRIPTION", "displayName": "SA_DISPLAY_NAME"} } āļŦāļēāļāļ•āđ‰āļ­āļ‡āļāļēāļĢāļŠāđˆāļ‡āļ„āļģāļ‚āļ­ āđƒāļŦāđ‰āļ‚āļĒāļēāļĒāļ•āļąāļ§āđ€āļĨāļ·āļ­āļāđƒāļ”āļ•āļąāļ§āđ€āļĨāļ·āļ­āļāļŦāļ™āļķāđˆāļ‡āļ•āđˆāļ­āđ„āļ›āļ™āļĩāđ‰ āļ‚āļ” (Linux, macOS āļŦāļĢāļ·āļ­ Cloud Shell) āļšāļąāļ™āļ—āļķāļāđ€āļ™āļ·āđ‰āļ­āļŦāļēāļ„āļģāļ‚āļ­āđƒāļ™āđ„āļŸāļĨāđŒāļ—āļĩāđˆāđ€āļĢāļĩāļĒāļāļ§āđˆāļē āļ„āļģāļ‚āļ­ json āđāļĨāļ°āļĢāļąāļ™āļ„āļģāļŠāļąāđˆāļ‡āļ•āđˆāļ­āđ„āļ›āļ™āļĩāđ‰: curl -X POST -H "āļāļēāļĢāļ­āļ™āļļāļāļēāļ•: Bearer $(gcloud auth print-access-token)"-H "āļ›āļĢāļ°āđ€āļ āļ—āđ€āļ™āļ·āđ‰āļ­āļŦāļē: application/json; charset=utf-8"-d @request.json "httpsiam.googleapis.com /v1/āđ‚āļ„āļĢāļ‡āļāļēāļĢ/ PROJECT_ID/āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ"āļžāļēāļ§āđ€āļ§āļ­āļĢāđŒāđ€āļŠāļĨāļĨāđŒ (āļ§āļīāļ™āđ‚āļ”āļ§āļŠāđŒ) āļšāļąāļ™āļ—āļķāļāđ€āļ™āļ·āđ‰āļ­āļŦāļēāļ„āļģāļ‚āļ­āđƒāļ™āđ„āļŸāļĨāđŒāļ—āļĩāđˆāđ€āļĢāļĩāļĒāļāļ§āđˆāļē āļ„āļģāļ‚āļ­ json āđāļĨāļ°āļĢāļąāļ™āļ„āļģāļŠāļąāđˆāļ‡āļ•āđˆāļ­āđ„āļ›āļ™āļĩāđ‰: $cred = gcloud āļĢāļąāļšāļĢāļ­āļ‡āđ‚āļ—āđ€āļ„āđ‡āļ™āļāļēāļĢāđ€āļ‚āđ‰āļēāļ–āļķāļ‡āļāļēāļĢāļžāļīāļĄāļžāđŒ $headers = @{ "Authorization"= "Bearer $cred"} āđ€āļĢāļĩāļĒāļāđƒāļŠāđ‰ WebRequest ` - āļ§āļīāļ˜āļĩāļāļēāļĢāđ‚āļžāļŠāļ•āđŒ ` -Headers $āđ€āļŪāļ”āđ€āļ”āļ­āļĢāđŒ ` -ContentType: "application/json; charset=utf-8"` -InFile request.json ` -Uri "httpsiam.googleapis.com/v1/projects/ PROJECT_ID/serviceAccounts"| āđ€āļĨāļ·āļ­āļāļ§āļąāļ•āļ–āļļ - āļ‚āļĒāļēāļĒāđ€āļ™āļ·āđ‰āļ­āļŦāļē API Explorer (āđ€āļšāļĢāļēāļ§āđŒāđ€āļ‹āļ­āļĢāđŒ) āļ„āļąāļ”āļĨāļ­āļāđ€āļ™āļ·āđ‰āļ­āļŦāļēāļ„āļģāļ‚āļ­āđāļĨāļ°āđ€āļ›āļīāļ”āđ„āļŸāļĨāđŒ āļŦāļ™āđ‰āļēāļ­āđ‰āļēāļ‡āļ­āļīāļ‡āļ§āļīāļ˜āļĩāļāļēāļĢ āđāļœāļ‡ API Explorer āļˆāļ°āđ€āļ›āļīāļ”āļ‚āļķāđ‰āļ™āļ—āļēāļ‡āļ”āđ‰āļēāļ™āļ‚āļ§āļēāļ‚āļ­āļ‡āļŦāļ™āđ‰āļē āļ„āļļāļ“āļŠāļēāļĄāļēāļĢāļ–āđ‚āļ•āđ‰āļ•āļ­āļšāļāļąāļšāđ€āļ„āļĢāļ·āđˆāļ­āļ‡āļĄāļ·āļ­āļ™āļĩāđ‰āđ€āļžāļ·āđˆāļ­āļŠāđˆāļ‡āļ„āļģāļ‚āļ­ āļ§āļēāļ‡āđ€āļ™āļ·āđ‰āļ­āļŦāļēāļ„āļģāļ‚āļ­āđƒāļ™āđ€āļ„āļĢāļ·āđˆāļ­āļ‡āļĄāļ·āļ­āļ™āļĩāđ‰ āļāļĢāļ­āļāļ‚āđ‰āļ­āļĄāļđāļĨāđƒāļ™āļŸāļīāļĨāļ”āđŒāļ—āļĩāđˆāļˆāļģāđ€āļ›āđ‡āļ™āļ­āļ·āđˆāļ™āđ† āđāļĨāđ‰āļ§āļ„āļĨāļīāļ **āļ”āļģāđ€āļ™āļīāļ™āļāļēāļĢ** āļ„āļļāļ“āļ„āļ§āļĢāđ„āļ”āđ‰āļĢāļąāļšāļāļēāļĢāļ•āļ­āļšāļŠāļ™āļ­āļ‡ JSON āļ„āļĨāđ‰āļēāļĒāļāļąāļšāļ•āđˆāļ­āđ„āļ›āļ™āļĩāđ‰: { "name": "projects/my-project/serviceAccounts/my-service-account@my-project.iam.gserviceaccount.com", "projectId": "my-project", "uniqueId": "123456789012345678901", "email": "my-service-account@my-project.iam.gserviceaccount.com", "displayName": "āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢāļ‚āļ­āļ‡āļ‰āļąāļ™", "etag": "BwUp3rVlzes "description": "āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢāļŠāļģāļŦāļĢāļąāļšāļāļēāļĢāđ€āļĢāļĩāļĒāļāđƒāļŠāđ‰āļ‡āļēāļ™āđƒāļ™āļ‚āļ­āļ‡āļ‰āļąāļ™ āđ‚āļ„āļĢāļ‡āļāļēāļĢ", "oauth2ClientId": "987654321098765432109"} āļ āļēāļĐāļēāļ‹āļĩ++ āļŦāļēāļāļ•āđ‰āļ­āļ‡āļāļēāļĢāđ€āļĢāļĩāļĒāļ™āļĢāļđāđ‰āļ§āļīāļ˜āļĩāļ•āļīāļ”āļ•āļąāđ‰āļ‡āđāļĨāļ°āđƒāļŠāđ‰āđ„āļĨāļšāļĢāļēāļĢāļĩāđ„āļ„āļĨāđ€āļ­āđ‡āļ™āļ•āđŒāļŠāļģāļŦāļĢāļąāļš IAM āđ‚āļ›āļĢāļ”āļ”āļđāđ„āļĨāļšāļĢāļēāļĢāļĩāđ„āļ„āļĨāđ€āļ­āđ‡āļ™āļ•āđŒ IAM āļŠāļģāļŦāļĢāļąāļšāļ‚āđ‰āļ­āļĄāļđāļĨāđ€āļžāļīāđˆāļĄāđ€āļ•āļīāļĄ āđ‚āļ›āļĢāļ”āļ”āļđāđ€āļ­āļāļŠāļēāļĢāļ­āđ‰āļēāļ‡āļ­āļīāļ‡ IAM C++ API āđ€āļ™āļĄāļŠāđ€āļ›āļ‹ iam = ::google::cloud::iam; std::string const& project_id, std::string const& account_id, std::string const& display_name, std::string const& āļ„āļģāļ­āļ˜āļīāļšāļēāļĒ) { iam::IAMClient āļĨāļđāļāļ„āđ‰āļē(iam::MakeIAMConnection google::iam::admin::v1::ServiceAccount service_account; service_account.set_display_name(display_name); service_account.set_description(description); auto response = client.CreateServiceAccount("projects/ "+ project_id, account_id, service_account); āļ–āđ‰āļē (!response) āļŠāđˆāļ‡ std::runtime_error(response.statusmessage std::cout<< "āļŠāļĢāđ‰āļēāļ‡āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢāļŠāļģāđ€āļĢāđ‡āļˆāđāļĨāđ‰āļ§: "<< āļāļēāļĢāļ•āļ­āļšāļŠāļ™āļ­āļ‡->DebugString()<< ""; } āļ„# āļŦāļēāļāļ•āđ‰āļ­āļ‡āļāļēāļĢāđ€āļĢāļĩāļĒāļ™āļĢāļđāđ‰āļ§āļīāļ˜āļĩāļ•āļīāļ”āļ•āļąāđ‰āļ‡āđāļĨāļ°āđƒāļŠāđ‰āđ„āļĨāļšāļĢāļēāļĢāļĩāđ„āļ„āļĨāđ€āļ­āđ‡āļ™āļ•āđŒāļŠāļģāļŦāļĢāļąāļš IAM āđ‚āļ›āļĢāļ”āļ”āļđāđ„āļĨāļšāļĢāļēāļĢāļĩāđ„āļ„āļĨāđ€āļ­āđ‡āļ™āļ•āđŒ IAM āļŠāļģāļŦāļĢāļąāļšāļ‚āđ‰āļ­āļĄāļđāļĨāđ€āļžāļīāđˆāļĄāđ€āļ•āļīāļĄ āđ‚āļ›āļĢāļ”āļ”āļđāđ€āļ­āļāļŠāļēāļĢāļ­āđ‰āļēāļ‡āļ­āļīāļ‡ IAM C# API āđƒāļŠāđ‰āļĢāļ°āļšāļš; āđƒāļŠāđ‰ Google.Apis.Auth.OAuth2; āđƒāļŠāđ‰ Google.Apis.Iam.v1; āđƒāļŠāđ‰ Google.Apis.Iam.v1.Data; ServiceAccounts āļ„āļĨāļēāļŠāļšāļēāļ‡āļŠāđˆāļ§āļ™āļŠāļēāļ˜āļēāļĢāļ“āļ° { ServiceAccount CreateServiceAccount āđāļšāļšāļ„āļ‡āļ—āļĩāđˆāļŠāļēāļ˜āļēāļĢāļ“āļ° (āļĢāļŦāļąāļŠāđ‚āļ„āļĢāļ‡āļāļēāļĢāļŠāļ•āļĢāļīāļ‡, āļŠāļ·āđˆāļ­āļŠāļ•āļĢāļīāļ‡, āļŠāļ·āđˆāļ­āļŠāļ•āļĢāļīāļ‡āļ—āļĩāđˆāđāļŠāļ”āļ‡) { var credential = GoogleCredential.GetApplicationDefault() .CreateScoped (IamService.Scope.CloudPlatform); var service = new IamService(new IamService.Initializer { HttpClientInitializer = credentialvar request = new CreateServiceAccountRequest { AccountId = name, ServiceAccount = new ServiceAccount { DisplayName = displayName } }; var serviceAccount = service.Projects.ServiceAccounts.Create( āļ„āļģāļ‚āļ­ "āđ‚āļ„āļĢāļ‡āļāļēāļĢ/ "+ projectId).Execute Console.WriteLine("āļŠāļĢāđ‰āļēāļ‡āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ: "+ serviceAccount.Email); return serviceAccount; } } āđ„āļ› āļŦāļēāļāļ•āđ‰āļ­āļ‡āļāļēāļĢāđ€āļĢāļĩāļĒāļ™āļĢāļđāđ‰āļ§āļīāļ˜āļĩāļ•āļīāļ”āļ•āļąāđ‰āļ‡āđāļĨāļ°āđƒāļŠāđ‰āđ„āļĨāļšāļĢāļēāļĢāļĩāđ„āļ„āļĨāđ€āļ­āđ‡āļ™āļ•āđŒāļŠāļģāļŦāļĢāļąāļš IAM āđ‚āļ›āļĢāļ”āļ”āļđāđ„āļĨāļšāļĢāļēāļĢāļĩāđ„āļ„āļĨāđ€āļ­āđ‡āļ™āļ•āđŒ IAM āļŠāļģāļŦāļĢāļąāļšāļ‚āđ‰āļ­āļĄāļđāļĨāđ€āļžāļīāđˆāļĄāđ€āļ•āļīāļĄ āđ‚āļ›āļĢāļ”āļ”āļđāđ€āļ­āļāļŠāļēāļĢāļ­āđ‰āļēāļ‡āļ­āļīāļ‡ IAM Go API āļ™āļģāđ€āļ‚āđ‰āļē ( "āļšāļĢāļīāļšāļ—""fmt""io"iam "google.golang.org/api/iam/v1") // createServiceAccount āļŠāļĢāđ‰āļēāļ‡āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ func createServiceAccount(w io.Writer, projectID, name, displayName string) (*iam.ServiceAccount, error) { ctx := context.Background() service, err := iam.NewService(ctx) if err != nil { return nil, fmt.Errorf("iam.NewService: %v", err) } request :=&iam.CreateServiceAccountRequest{ AccountId: āļŠāļ·āđˆāļ­, ServiceAccount:&iam.ServiceAccount{ DisplayName: displayName, }, } āļšāļąāļāļŠāļĩ err := service.Projects.ServiceAccounts.Create("projectsprojectID, request).Do() if err != nil { return nil, fmt.Errorf("Projects.ServiceAccounts.Create: %v", err) } fmt.Fprintf(w, "āļŠāļĢāđ‰āļēāļ‡āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ: %v", āļšāļąāļāļŠāļĩ) āļ„āļ·āļ™āļšāļąāļāļŠāļĩ āđ„āļĄāđˆāļĄāļĩ } āļŠāļ§āļē āļŦāļēāļāļ•āđ‰āļ­āļ‡āļāļēāļĢāđ€āļĢāļĩāļĒāļ™āļĢāļđāđ‰āļ§āļīāļ˜āļĩāļ•āļīāļ”āļ•āļąāđ‰āļ‡āđāļĨāļ°āđƒāļŠāđ‰āđ„āļĨāļšāļĢāļēāļĢāļĩāđ„āļ„āļĨāđ€āļ­āđ‡āļ™āļ•āđŒāļŠāļģāļŦāļĢāļąāļš IAM āđ‚āļ›āļĢāļ”āļ”āļđāđ„āļĨāļšāļĢāļēāļĢāļĩāđ„āļ„āļĨāđ€āļ­āđ‡āļ™āļ•āđŒ IAM āļŠāļģāļŦāļĢāļąāļšāļ‚āđ‰āļ­āļĄāļđāļĨāđ€āļžāļīāđˆāļĄāđ€āļ•āļīāļĄ āđ‚āļ›āļĢāļ”āļ”āļđāđ€āļ­āļāļŠāļēāļĢāļ­āđ‰āļēāļ‡āļ­āļīāļ‡ IAM Java API āļ™āļģāđ€āļ‚āđ‰āļē com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; āļ™āļģāđ€āļ‚āđ‰āļē com.google.api.client.json.jackson2.JacksonFactory; āļ™āļģāđ€āļ‚āđ‰āļē com.google.api.services.iam.v1.Iam; āļ™āļģāđ€āļ‚āđ‰āļē com.google.api.services.iam.v1.IamScopes; āļ™āļģāđ€āļ‚āđ‰āļē com.google.api.services.iam.v1.model.CreateServiceAccountRequest; āļ™āļģāđ€āļ‚āđ‰āļē com.google.api.services.iam.v1.model.ServiceAccount; āļ™āļģāđ€āļ‚āđ‰āļē com.google.auth.http.HttpCredentialsAdapter; āļ™āļģāđ€āļ‚āđ‰āļē com.google.auth.oauth2.GoogleCredentials; āļ™āļģāđ€āļ‚āđ‰āļē java.io.IOException; āļ™āļģāđ€āļ‚āđ‰āļē java.security.GeneralSecurityException; āļ™āļģāđ€āļ‚āđ‰āļē java.util.Collections; CreateServiceAccount āļ„āļĨāļēāļŠāļŠāļēāļ˜āļēāļĢāļ“āļ° { // āļŠāļĢāđ‰āļēāļ‡āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ āđ‚āļĄāļ†āļ°āļŠāļēāļ˜āļēāļĢāļ“āļ°āļ„āļ‡āļ—āļĩāđˆ createServiceAccount (String projectId, String serviceAccountName) { // String projectId = "my-project-id"; // āļŠāļ•āļĢāļīāļ‡ serviceAccountName = "āļŠāļ·āđˆāļ­āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢāļ‚āļ­āļ‡āļ‰āļąāļ™"; āļšāļĢāļīāļāļēāļĢāđ€āļ­āļĩāđˆāļĒāļĄ = null; āļĨāļ­āļ‡ { service = initService } catch (IOException | GeneralSecurityException e) { System.out.println("āđ„āļĄāđˆāļŠāļēāļĄāļēāļĢāļ–āđ€āļĢāļīāđˆāļĄāļ•āđ‰āļ™āļšāļĢāļīāļāļēāļĢ: "+ e.toString return; } āļĨāļ­āļ‡ { ServiceAccount serviceAccount = new ServiceAccount serviceAccount.setDisplayName("your-display-name CreateServiceAccountRequest request = new CreateServiceAccountRequest request.setAccountId(serviceAccountName); request.setServiceAccount(serviceAccount); serviceAccount = service.projectsserviceAccountscreate( "projects/"+ projectId, request).execute System.out.println("āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢāļ—āļĩāđˆāļŠāļĢāđ‰āļēāļ‡: "+ serviceAccount.getEmail } catch (IOException e) { System.out.println("āđ„āļĄāđˆāļŠāļēāļĄāļēāļĢāļ–āļŠāļĢāđ‰āļēāļ‡āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ: "+ e.toString } } āļŠāđˆāļ§āļ™āļ•āļąāļ§āļ„āļ‡ Iam initService() āļŠāđˆāļ‡ GeneralSecurityException, IOException { // āđƒāļŠāđ‰āļāļĨāļĒāļļāļ—āļ˜āđŒāļ‚āđ‰āļ­āļĄāļđāļĨāļĢāļąāļšāļĢāļ­āļ‡āđ€āļĢāļīāđˆāļĄāļ•āđ‰āļ™āļ‚āļ­āļ‡āđāļ­āļ›āļžāļĨāļīāđ€āļ„āļŠāļąāļ™āļŠāļģāļŦāļĢāļąāļšāļāļēāļĢāļ•āļĢāļ§āļˆāļŠāļ­āļšāļŠāļīāļ—āļ˜āļīāđŒ āļŠāļģāļŦāļĢāļąāļšāļ‚āđ‰āļ­āļĄāļđāļĨāđ€āļžāļīāđˆāļĄāđ€āļ•āļīāļĄ āđ‚āļ›āļĢāļ”āļ”āļđāļ—āļĩāđˆ: // httpscloud.google.com/docs/authentication/production#finding_credentials_automatically āļ‚āđ‰āļ­āļĄāļđāļĨāļĢāļąāļšāļĢāļ­āļ‡ GoogleCredentials = GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(IamScopes.CLOUD_PLATFORM // āđ€āļĢāļīāđˆāļĄāļ•āđ‰āļ™āļšāļĢāļīāļāļēāļĢ IAM āļ‹āļķāđˆāļ‡āļŠāļēāļĄāļēāļĢāļ–āđƒāļŠāđ‰āđ€āļžāļ·āđˆāļ­āļŠāđˆāļ‡āļ„āļģāļ‚āļ­āđ„āļ›āļĒāļąāļ‡ IAM API āļšāļĢāļīāļāļēāļĢ Iam = Iam.Builder āđƒāļŦāļĄāđˆ ( GoogleNetHttpTransport.newTrustedTransport JacksonFactory getDefaultInstance āđƒāļŦāļĄāđˆ HttpCredentialsAdapter(āļ‚āđ‰āļ­āļĄāļđāļĨāļĢāļąāļšāļĢāļ­āļ‡)) .setApplicationName("āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ") .build āļšāļĢāļīāļāļēāļĢāļŠāđˆāļ‡āļ„āļ·āļ™ } } āļŦāļĨāļēāļĄ āļŦāļēāļāļ•āđ‰āļ­āļ‡āļāļēāļĢāđ€āļĢāļĩāļĒāļ™āļĢāļđāđ‰āļ§āļīāļ˜āļĩāļ•āļīāļ”āļ•āļąāđ‰āļ‡āđāļĨāļ°āđƒāļŠāđ‰āđ„āļĨāļšāļĢāļēāļĢāļĩāđ„āļ„āļĨāđ€āļ­āđ‡āļ™āļ•āđŒāļŠāļģāļŦāļĢāļąāļš IAM āđ‚āļ›āļĢāļ”āļ”āļđāđ„āļĨāļšāļĢāļēāļĢāļĩāđ„āļ„āļĨāđ€āļ­āđ‡āļ™āļ•āđŒ IAM āļŠāļģāļŦāļĢāļąāļšāļ‚āđ‰āļ­āļĄāļđāļĨāđ€āļžāļīāđˆāļĄāđ€āļ•āļīāļĄ āđ‚āļ›āļĢāļ”āļ”āļđāđ€āļ­āļāļŠāļēāļĢāļ­āđ‰āļēāļ‡āļ­āļīāļ‡ IAM Python API āļ™āļģāđ€āļ‚āđ‰āļēāļĢāļ°āļšāļšāļ›āļāļīāļšāļąāļ•āļīāļāļēāļĢāļˆāļēāļ google.oauth2 āļ™āļģāđ€āļ‚āđ‰āļē service_account āļ™āļģāđ€āļ‚āđ‰āļē googleapiclient.discovery def create_service_account(project_id, name, display_name): āļŠāļĢāđ‰āļēāļ‡āļ‚āđ‰āļ­āļĄāļđāļĨāļ›āļĢāļ°āļˆāļģāļ•āļąāļ§āļ‚āļ­āļ‡āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ = service_account.Credentials.from_service_account_file( filename=os.environ['GOOGLE_APPLICATION_CREDENTIALS scopeshttpswww.googleapis.com/auth/ āļšāļĢāļīāļāļēāļĢāđāļžāļĨāļ•āļŸāļ­āļĢāđŒāļĄāļ„āļĨāļēāļ§āļ”āđŒ = googleapiclient.discovery.build( 'iam', 'v1', credentials=credentials) my_service_account = service.projectsserviceAccountscreate( name='projects/'+ project_id, body={ 'accountId': name, 'serviceAccount': { 'displayName': display_name } execute() print('āļŠāļĢāđ‰āļēāļ‡āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ: '+ my_service_account['email return my_service_account āļŦāļĨāļąāļ‡āļˆāļēāļāļ—āļĩāđˆāļ„āļļāļ“āļŠāļĢāđ‰āļēāļ‡āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢāđāļĨāđ‰āļ§ āđƒāļŦāđ‰āļĄāļ­āļšāļšāļ—āļšāļēāļ—āļ­āļĒāđˆāļēāļ‡āļ™āđ‰āļ­āļĒāļŦāļ™āļķāđˆāļ‡āļšāļ—āļšāļēāļ—āđƒāļŦāđ‰āļāļąāļšāļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢāđ€āļžāļ·āđˆāļ­āđƒāļŦāđ‰āļ”āļģāđ€āļ™āļīāļ™āļāļēāļĢāđƒāļ™āļ™āļēāļĄāļ‚āļ­āļ‡āļ„āļļāļ“āđ„āļ”āđ‰ āļ™āļ­āļāļˆāļēāļāļ™āļĩāđ‰ āļŦāļēāļāļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢāļˆāļģāđ€āļ›āđ‡āļ™āļ•āđ‰āļ­āļ‡āđ€āļ‚āđ‰āļēāļ–āļķāļ‡āļ—āļĢāļąāļžāļĒāļēāļāļĢāđƒāļ™āđ‚āļ„āļĢāļ‡āļāļēāļĢāļ­āļ·āđˆāļ™āđ† āđ‚āļ”āļĒāļ›āļāļ•āļīāđāļĨāđ‰āļ§ āļ„āļļāļ“āļ•āđ‰āļ­āļ‡āđ€āļ›āļīāļ”āđƒāļŠāđ‰ API āļŠāļģāļŦāļĢāļąāļšāļ—āļĢāļąāļžāļĒāļēāļāļĢāđ€āļŦāļĨāđˆāļēāļ™āļąāđ‰āļ™āđƒāļ™āđ‚āļ„āļĢāļ‡āļāļēāļĢāļ—āļĩāđˆāļ„āļļāļ“āļŠāļĢāđ‰āļēāļ‡āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ ## āļĢāļēāļĒāļŠāļ·āđˆāļ­āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ āļ„āļļāļ“āļŠāļēāļĄāļēāļĢāļ–āđāļŠāļ”āļ‡āļĢāļēāļĒāļāļēāļĢāļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢāļ‚āļ­āļ‡āļ„āļļāļ“āđ€āļžāļ·āđˆāļ­āļŠāđˆāļ§āļĒāļ„āļļāļ“āļ•āļĢāļ§āļˆāļŠāļ­āļšāļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢāđāļĨāļ°āļ„āļĩāļĒāđŒ āļŦāļĢāļ·āļ­āđ€āļ›āđ‡āļ™āļŠāđˆāļ§āļ™āļŦāļ™āļķāđˆāļ‡āļ‚āļ­āļ‡āđ€āļ„āļĢāļ·āđˆāļ­āļ‡āļĄāļ·āļ­āļ—āļĩāđˆāļāļģāļŦāļ™āļ”āđ€āļ­āļ‡āļŠāļģāļŦāļĢāļąāļšāļˆāļąāļ”āļāļēāļĢāļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ āļ„āļ­āļ™āđ‚āļ‹āļĨ āđƒāļ™āļ„āļ­āļ™āđ‚āļ‹āļĨ Google Cloud āđƒāļŦāđ‰āđ„āļ›āļ—āļĩāđˆ āļŦāļ™āđ‰āļēāļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ āđ€āļĨāļ·āļ­āļāđ‚āļ„āļĢāļ‡āļāļēāļĢ āđ€āļ”āļ­āļ° āļŦāļ™āđ‰āļēāļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢāđāļŠāļ”āļ‡āļĢāļēāļĒāļāļēāļĢāļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢāļ—āļĩāđˆāļˆāļąāļ”āļāļēāļĢāđ‚āļ”āļĒāļœāļđāđ‰āđƒāļŠāđ‰āļ—āļąāđ‰āļ‡āļŦāļĄāļ”āđƒāļ™āđ‚āļ„āļĢāļ‡āļāļēāļĢāļ—āļĩāđˆāļ„āļļāļ“āđ€āļĨāļ·āļ­āļ āļŦāļ™āđ‰āļēāļ™āļĩāđ‰āđ„āļĄāđˆāļĄāļĩāļĢāļēāļĒāļāļēāļĢāļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢāļ—āļĩāđˆāļˆāļąāļ”āļāļēāļĢāđ‚āļ”āļĒ Google gcloud CLI āļ”āļģāđ€āļ™āļīāļ™āļāļēāļĢ āļĢāļēāļĒāļāļēāļĢāļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ gcloud iam āļ„āļģāļŠāļąāđˆāļ‡āđ€āļžāļ·āđˆāļ­āđāļŠāļ”āļ‡āļĢāļēāļĒāļāļēāļĢāļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢāļ—āļąāđ‰āļ‡āļŦāļĄāļ”āđƒāļ™āđ‚āļ„āļĢāļ‡āļāļēāļĢ āļŠāļąāđˆāļ‡āļāļēāļĢ: āļĢāļēāļĒāļāļēāļĢāļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ gcloud iam āļœāļĨāļĨāļąāļžāļ˜āđŒāļ„āļ·āļ­āļĢāļēāļĒāļāļēāļĢāļ‚āļ­āļ‡āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢāļ—āļąāđ‰āļ‡āļŦāļĄāļ”āđƒāļ™āđ‚āļ„āļĢāļ‡āļāļēāļĢ: āļŠāļ·āđˆāļ­ āļ­āļĩāđ€āļĄāļĨ SA_DISPLAY_NAME_1 SA_NAME_1@ PROJECT_ID.iam.gserviceaccount.com SA_DISPLAY_NAME_2 SA_NAME_2@ PROJECT_ID.iam.gserviceaccount.com āļžāļąāļāļœāđˆāļ­āļ™ āđ€āļ”āļ­āļ° serviceAccounts.list āđ€āļĄāļ˜āļ­āļ”āđāļŠāļ”āļ‡āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢāļ—āļąāđ‰āļ‡āļŦāļĄāļ”āđƒāļ™āđ‚āļ„āļĢāļ‡āļāļēāļĢāļ‚āļ­āļ‡āļ„āļļāļ“ āļāđˆāļ­āļ™āđƒāļŠāđ‰āļ‚āđ‰āļ­āļĄāļđāļĨāļ„āļģāļ‚āļ­āđƒāļ”āđ† āđƒāļŦāđ‰āļ”āļģāđ€āļ™āļīāļ™āļāļēāļĢāđāļ—āļ™āļ—āļĩāđˆāļ”āļąāļ‡āļ•āđˆāļ­āđ„āļ›āļ™āļĩāđ‰: : āļĢāļŦāļąāļŠāđ‚āļ„āļĢāļ‡āļāļēāļĢ Google Cloud āļ‚āļ­āļ‡āļ„āļļāļ“ āļĢāļŦāļąāļŠāđ‚āļ„āļĢāļ‡āļāļēāļĢāļ„āļ·āļ­āļŠāļ•āļĢāļīāļ‡āļ—āļĩāđˆāđ€āļ›āđ‡āļ™āļ•āļąāļ§āļ­āļąāļāļĐāļĢāđāļĨāļ°āļ•āļąāļ§āđ€āļĨāļ‚āļ„āļĨāļ°āļāļąāļ™ āđ€āļŠāđˆāļ™ PROJECT_ID āđ‚āļ„āļĢāļ‡āļāļēāļĢāļ‚āļ­āļ‡āļ‰āļąāļ™ āļ§āļīāļ˜āļĩāļāļēāļĢ HTTP āđāļĨāļ° URL: āļĢāļąāļš httpsiam.googleapis.com/v1/projects/ PROJECT_ID/āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ āļŦāļēāļāļ•āđ‰āļ­āļ‡āļāļēāļĢāļŠāđˆāļ‡āļ„āļģāļ‚āļ­ āđƒāļŦāđ‰āļ‚āļĒāļēāļĒāļ•āļąāļ§āđ€āļĨāļ·āļ­āļāđƒāļ”āļ•āļąāļ§āđ€āļĨāļ·āļ­āļāļŦāļ™āļķāđˆāļ‡āļ•āđˆāļ­āđ„āļ›āļ™āļĩāđ‰ āļ‚āļ” (Linux, macOS āļŦāļĢāļ·āļ­ Cloud Shell) āļ”āļģāđ€āļ™āļīāļ™āļāļēāļĢāļ„āļģāļŠāļąāđˆāļ‡āļ•āđˆāļ­āđ„āļ›āļ™āļĩāđ‰: curl -X GET -H "āļāļēāļĢāļ­āļ™āļļāļāļēāļ•: Bearer $(gcloud auth print-access-token)""httpsiam.googleapis.com/v1/projects/ PROJECT_ID/āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ"āļžāļēāļ§āđ€āļ§āļ­āļĢāđŒāđ€āļŠāļĨāļĨāđŒ (āļ§āļīāļ™āđ‚āļ”āļ§āļŠāđŒ) āļ”āļģāđ€āļ™āļīāļ™āļāļēāļĢāļ„āļģāļŠāļąāđˆāļ‡āļ•āđˆāļ­āđ„āļ›āļ™āļĩāđ‰: $cred = gcloud āļĢāļąāļšāļĢāļ­āļ‡āđ‚āļ—āđ€āļ„āđ‡āļ™āļāļēāļĢāđ€āļ‚āđ‰āļēāļ–āļķāļ‡āļāļēāļĢāļžāļīāļĄāļžāđŒ $headers = @{ "Authorization"= "Bearer $cred"} āđ€āļĢāļĩāļĒāļāđƒāļŠāđ‰ WebRequest ` -āđ€āļĄāļ˜āļ­āļ” GET ` -Headers $āđ€āļŪāļ”āđ€āļ”āļ­āļĢāđŒ ` -Uri "httpsiam.googleapis.com/v1/projects/ PROJECT_ID/serviceAccounts"| āđ€āļĨāļ·āļ­āļāļ§āļąāļ•āļ–āļļ - āļ‚āļĒāļēāļĒāđ€āļ™āļ·āđ‰āļ­āļŦāļē API Explorer (āđ€āļšāļĢāļēāļ§āđŒāđ€āļ‹āļ­āļĢāđŒ) āđ€āļ›āļīāļ” āļŦāļ™āđ‰āļēāļ­āđ‰āļēāļ‡āļ­āļīāļ‡āļ§āļīāļ˜āļĩāļāļēāļĢ āđāļœāļ‡ API Explorer āļˆāļ°āđ€āļ›āļīāļ”āļ‚āļķāđ‰āļ™āļ—āļēāļ‡āļ”āđ‰āļēāļ™āļ‚āļ§āļēāļ‚āļ­āļ‡āļŦāļ™āđ‰āļē āļ„āļļāļ“āļŠāļēāļĄāļēāļĢāļ–āđ‚āļ•āđ‰āļ•āļ­āļšāļāļąāļšāđ€āļ„āļĢāļ·āđˆāļ­āļ‡āļĄāļ·āļ­āļ™āļĩāđ‰āđ€āļžāļ·āđˆāļ­āļŠāđˆāļ‡āļ„āļģāļ‚āļ­ āļāļĢāļ­āļāļ‚āđ‰āļ­āļĄāļđāļĨāđƒāļ™āļŸāļīāļĨāļ”āđŒāļ—āļĩāđˆāļˆāļģāđ€āļ›āđ‡āļ™āđāļĨāļ°āļ„āļĨāļīāļ **āļ”āļģāđ€āļ™āļīāļ™āļāļēāļĢ** āļ„āļļāļ“āļ„āļ§āļĢāđ„āļ”āđ‰āļĢāļąāļšāļāļēāļĢāļ•āļ­āļšāļŠāļ™āļ­āļ‡ JSON āļ„āļĨāđ‰āļēāļĒāļāļąāļšāļ•āđˆāļ­āđ„āļ›āļ™āļĩāđ‰: { "āļšāļąāļāļŠāļĩ": [ { "āļŠāļ·āđˆāļ­": "projects/my-project/serviceAccounts/sa-1@my-project.iam.gserviceaccount.com", "projectId": "my-project", "uniqueId": "123456789012345678901", "email": "sa-1@my-project.iam.gserviceaccount.com", "description": "āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢāđāļĢāļāļ‚āļ­āļ‡āļ‰āļąāļ™", "displayName": "āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ 1", "etag": "BwUpTsLVUkQ "oauth2ClientId": "987654321098765432109"}, { "name": "projects/my-project/serviceAccounts/sa-2@my-project.iam.gserviceaccount.com", "projectId": "my-project", "uniqueId ": "234567890123456789012", "email": "sa-2@my-project.iam.gserviceaccount.com", "description": "āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢāļ—āļĩāđˆāļŠāļ­āļ‡āļ‚āļ­āļ‡āļ‰āļąāļ™", "displayName": "āļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢ 2", "etag": "UkQpTwBVUsL "oauth2ClientId": "876543210987654321098"} ] } āļ āļēāļĐāļēāļ‹āļĩ++ āļŦāļēāļāļ•āđ‰āļ­āļ‡āļāļēāļĢāđ€āļĢāļĩāļĒāļ™āļĢāļđāđ‰āļ§āļīāļ˜āļĩāļ•āļīāļ”āļ•āļąāđ‰āļ‡āđāļĨāļ°āđƒāļŠāđ‰āđ„āļĨāļšāļĢāļēāļĢāļĩāđ„āļ„āļĨāđ€āļ­āđ‡āļ™āļ•āđŒāļŠāļģāļŦāļĢāļąāļš IAM āđ‚āļ›āļĢāļ”āļ”āļđāđ„āļĨāļšāļĢāļēāļĢāļĩāđ„āļ„āļĨāđ€āļ­āđ‡āļ™āļ•āđŒ IAM āļŠāļģāļŦāļĢāļąāļšāļ‚āđ‰āļ­āļĄāļđāļĨāđ€āļžāļīāđˆāļĄāđ€āļ•āļīāļĄ āđ‚āļ›āļĢāļ”āļ”āļđāđ€āļ­āļāļŠāļēāļĢāļ­āđ‰āļēāļ‡āļ­āļīāļ‡ IAM C++ API āđ€āļ™āļĄāļŠāđ€āļ›āļ‹ iam = ::google::cloud::iam; std::āļŠāļ•āļĢāļīāļ‡ const& project_id) { iam::IAMClient client(iam::MakeIAMConnection int count = 0; for (auto const& sa : client.ListServiceAccounts("projects/"+ project_id)) { āļ–āđ‰āļē (!sa) āđ‚āļĒāļ™ std::runtime_error(sa.statusmessage std::cout<< "āļ”āļķāļ‡āļ‚āđ‰āļ­āļĄāļđāļĨ ServiceAccount āļŠāļģāđ€āļĢāđ‡āļˆāđāļĨāđ‰āļ§: "<< sa->name()<< ""; ++count; } if (count == 0) { std::cout<< "āđ„āļĄāđˆāļžāļšāļšāļąāļāļŠāļĩāļšāļĢāļīāļāļēāļĢāđƒāļ™āđ‚āļ„āļĢāļ‡āļāļēāļĢ: "<< project_id<< ""; } } āļ„# āļŦāļēāļāļ•āđ‰āļ­āļ‡āļāļēāļĢāđ€āļĢāļĩāļĒāļ™āļĢāļđāđ‰āļ§āļīāļ˜āļĩāļ•āļīāļ”āļ•āļąāđ‰āļ‡āđāļĨāļ°āđƒāļŠāđ‰āđ„āļĨāļšāļĢāļēāļĢāļĩāđ„āļ„āļĨāđ€āļ­āđ‡āļ™āļ•āđŒāļŠāļģāļŦāļĢāļąāļš IAM āđ‚āļ›āļĢāļ”āļ”āļđāđ„āļĨāļšāļĢāļēāļĢāļĩāđ„āļ„āļĨāđ€āļ­āđ‡āļ™āļ•āđŒ IAM āļŠāļģāļŦāļĢāļąāļšāļ‚āđ‰āļ­āļĄāļđāļĨāđ€āļžāļīāđˆāļĄāđ€āļ•āļīāļĄ āđ‚āļ›āļĢāļ”āļ”āļđāđ€āļ­āļāļŠāļēāļĢāļ­āđ‰āļēāļ‡āļ­āļīāļ‡ IAM C# API āđƒāļŠāđ‰āļĢāļ°āļšāļš; āđƒāļŠāđ‰ System.Collections.Generic; āđƒāļŠāđ‰ Google.Apis.Auth.OAuth2; āđƒāļŠāđ‰ Google.Apis.Iam.v1; āđƒāļŠāđ‰ Google.Apis.Iam.v1.Data; ServiceAccounts āļ„āļĨāļēāļŠāļŠāļēāļ˜āļēāļĢāļ“āļ°āļšāļēāļ‡āļŠāđˆāļ§āļ™ { āļŠāļēāļ˜āļēāļĢāļ“āļ°āļ„āļ‡āļ—āļĩāđˆ IList ListServiceAccounts(string projectId) { var credential = GoogleCredential.GetApplicationDefault() .CreateScoped(IamService.Scope.CloudPlatform); var service = new IamService(new IamService.Initializer { HttpClientInitializer = credentialvar response = service.Projects.ServiceAccounts.List( "projects/" + projectId).Execute foreach (ServiceAccount account in response.Accounts) { Console.WriteLine("Name: " + account.Name); Console.WriteLine("Display Name: " + account.DisplayName); Console.WriteLine("Email: " + account.Email); Console.WriteLine } return response.Accounts; } } Go To learn how to install and use the client library for IAM, see IAM client libraries. For more information, see the IAM Go API reference documentation import ( "context" "fmt" "io" iam "google.golang.org/api/iam/v1" ) // listServiceAccounts lists a project's service accounts. func listServiceAccounts(w io.Writer, projectID string) iam.ServiceAccount, error) { ctx := context.Background() service, err := iam.NewService(ctx) if err != nil { return nil, fmt.Errorf("iam.NewService: %v", err) } response, err := service.Projects.ServiceAccounts.List("projects/" + projectID).Do() if err != nil { return nil, fmt.Errorf("Projects.ServiceAccounts.List: %v", err) } for _, account := range response.Accounts { fmt.Fprintf(w, "Listing service account: %v ", account.Name) } return response.Accounts, nil } Java To learn how to install and use the client library for IAM, see IAM client libraries. For more information, see the IAM Java API reference documentation import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.iam.v1.Iam; import com.google.api.services.iam.v1.IamScopes; import com.google.api.services.iam.v1.model.ListServiceAccountsResponse; import com.google.api.services.iam.v1.model.ServiceAccount; import com.google.auth.http.HttpCredentialsAdapter; import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.security.GeneralSecurityException; import java.util.Collections; import java.util.List; public class ListServiceAccounts { // Lists all service accounts for the current project. public static void listServiceAccounts(String projectId) { // String projectId = "my-project-id" Iam service = null; try { service = initService } catch (IOException | GeneralSecurityException e) { System.out.println("Unable to initialize service: " + e.toString return; } try { ListServiceAccountsResponse response = service.projectsserviceAccountslist("projects/" + projectId).execute List serviceAccounts = response.getAccounts for (ServiceAccount account : serviceAccounts) { System.out.println("Name: " + account.getName System.out.println("Display Name: " + account.getDisplayName System.out.println("Email: " + account.getEmail System.out.println } } catch (IOException e) { System.out.println("Unable to list service accounts: " + e.toString } } private static Iam initService() throws GeneralSecurityException, IOException { // Use the Application Default Credentials strategy for authentication. For more info, see: // httpscloud.google.com/docs/authentication/production#finding_credentials_automatically GoogleCredentials credential = GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(IamScopes.CLOUD_PLATFORM // Initialize the IAM service, which can be used to send requests to the IAM API. Iam service = new Iam.Builder( GoogleNetHttpTransport.newTrustedTransport JacksonFactory.getDefaultInstance new HttpCredentialsAdapter(credential)) .setApplicationName("service-accounts") .build return service; } } Python To learn how to install and use the client library for IAM, see IAM client libraries. For more information, see the IAM Python API reference documentation import os from google.oauth2 import service_account import googleapiclient.discovery def list_service_accounts(project_id): Lists all service accounts for the current project credentials = service_account.Credentials.from_service_account_file( filename=os.environ['GOOGLE_APPLICATION_CREDENTIALS scopeshttpswww.googleapis.com/auth/cloud-platform service = googleapiclient.discovery.build( 'iam', 'v1', credentials=credentials) service_accounts = service.projectsserviceAccountslist( name='projects/' + project_id).execute() for account in service_accounts['accounts print('Name: ' + account['name print('Email: ' + account['email print(' ') return service_accounts ## Updating a service account The display name (friendly name) and description of a service account are commonly used to capture additional information about the service account, such as the purpose of the service account or a contact person for the account Console In the Google Cloud console, go to the Service accountspage Select a project Click the email address of the service account that you want to rename Enter the new name in the Namebox, then click Save gcloud CLI Execute the gcloud iam service-accounts update command to update a service account Command: gcloud iam service-accounts update SA_NAME@ PROJECT_ID.iam.gserviceaccount.com \ --description=" UPDATED_SA_DESCRIPTION" \ --display-name=" UPDATED_DISPLAY_NAME" The output is the renamed service account: description: UPDATED_SA_DESCRIPTIONdisplayName: UPDATED_DISPLAY_NAMEname: projects/ PROJECT_ID/serviceAccounts/ SA_NAME@ PROJECT_ID.iam.gserviceaccount.com REST The serviceAccounts.patch method updates a service account Before using any of the request data, make the following replacements: : Your Google Cloud project ID. Project IDs are alphanumeric strings, like PROJECT_ID my-project : The ID of your service account. This can either be the service account's email address in the form SA_ID , or the service account's unique numeric ID SA_NAME@ PROJECT_ID.iam.gserviceaccount.com : The alphanumeric ID of your service account. This name must be between 6 and 30 characters, and can contain lowercase alphanumeric characters and dashes SA_NAME - Replace at least one of the following: : A new display name for your service account UPDATED_DISPLAY_NAME : A new description for your service account UPDATED_DESCRIPTION HTTP method and URL: PATCH httpsiam.googleapis.com/v1/projects/ PROJECT_ID/serviceAccounts/ SA_ID Request JSON body: { "serviceAccount": { "email": " SA_NAME@ PROJECT_ID.iam.gserviceaccount.com", "displayName": " UPDATED_DISPLAY_NAME", "description": " UPDATED_DESCRIPTION" }, "updateMask": "displayName,description" } To send your request, expand one of these options: curl (Linux, macOS, or Cloud Shell) Save the request body in a file called request.json, and execute the following command: curl -X PATCH -H "Authorization: Bearer $(gcloud auth print-access-token)" -H "Content-Type: application/json; charset=utf-8" -d @request.json "httpsiam.googleapis.com/v1/projects/ PROJECT_ID/serviceAccounts/ SA_ID" PowerShell (Windows) Save the request body in a file called request.json, and execute the following command: $cred = gcloud auth print-access-token $headers = @{ "Authorization" = "Bearer $cred" } Invoke-WebRequest ` -Method PATCH ` -Headers $headers ` -ContentType: "application/json; charset=utf-8" ` -InFile request.json ` -Uri "httpsiam.googleapis.com/v1/projects/ PROJECT_ID/serviceAccounts/ SA_ID" | Select-Object -Expand Content API Explorer (browser) Copy the request body and open the method reference page The API Explorer panel opens on the right side of the page You can interact with this tool to send requests Paste the request body in this tool, complete any other required fields, and click **Execute** You should receive a JSON response similar to the following: { "name": "projects/my-project/serviceAccounts/my-service-account@my-project.iam.gserviceaccount.com", "displayName": "My updated service account", "description": "An updated description of my service account" } C++ To learn how to install and use the client library for IAM, see IAM client libraries. For more information, see the IAM C++ API reference documentation namespace iam = ::google::cloud::iam; std::string const& name, std::string const& display_name) { iam::IAMClient client(iam::MakeIAMConnection google::iam::admin::v1::PatchServiceAccountRequest request; google::iam::admin::v1::ServiceAccount service_account; service_account.set_name(name); service_account.set_display_name(display_name); google::protobuf::FieldMask update_mask; *update_mask.add_paths() = "display_name"; *request.mutable_service_account() = service_account; *request.mutable_update_mask() = update_mask; auto response = client.PatchServiceAccount(request); if (!response) throw std::runtime_error(response.statusmessage std::cout << "ServiceAccount successfully updated: " << response->DebugString() << " "; } C# To learn how to install and use the client library for IAM, see IAM client libraries. For more information, see the IAM C# API reference documentation using System; using Google.Apis.Auth.OAuth2; using Google.Apis.Iam.v1; using Google.Apis.Iam.v1.Data; public partial class ServiceAccounts { public static ServiceAccount RenameServiceAccount(string email, string newDisplayName) { var credential = GoogleCredential.GetApplicationDefault() .CreateScoped(IamService.Scope.CloudPlatform); var service = new IamService(new IamService.Initializer { HttpClientInitializer = credential// First, get a ServiceAccount using List() or Get string resource = "projectsserviceAccounts/" + email; var serviceAccount = service.Projects.ServiceAccounts.Get(resource) .Execute // Then you can update the display name. serviceAccount.DisplayName = newDisplayName; serviceAccount = service.Projects.ServiceAccounts.Update( serviceAccount, resource).Execute Console.WriteLineUpdated display name for {serviceAccount.Email} " + "to: " + serviceAccount.DisplayName); return serviceAccount; } } Go To learn how to install and use the client library for IAM, see IAM client libraries. For more information, see the IAM Go API reference documentation import ( "context" "fmt" "io" iam "google.golang.org/api/iam/v1" ) // renameServiceAccount renames a service account. func renameServiceAccount(w io.Writer, email, newDisplayName string) (*iam.ServiceAccount, error) { ctx := context.Background() service, err := iam.NewService(ctx) if err != nil { return nil, fmt.Errorf("iam.NewService: %v", err) } // First, get a ServiceAccount using List() or Get resource := "projectsserviceAccounts/" + email serviceAccount, err := service.Projects.ServiceAccounts.Get(resource).Do() if err != nil { return nil, fmt.Errorf("Projects.ServiceAccounts.Get: %v", err) } // Then you can update the display name. serviceAccount.DisplayName = newDisplayName serviceAccount, err = service.Projects.ServiceAccounts.Update(resource, serviceAccount).Do() if err != nil { return nil, fmt.Errorf("Projects.ServiceAccounts.Update: %v", err) } fmt.Fprintf(w, "Updated service account: %v", serviceAccount.Email) return serviceAccount, nil } Java To learn how to install and use the client library for IAM, see IAM client libraries. For more information, see the IAM Java API reference documentation import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.iam.v1.Iam; import com.google.api.services.iam.v1.IamScopes; import com.google.api.services.iam.v1.model.ServiceAccount; import com.google.auth.http.HttpCredentialsAdapter; import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.security.GeneralSecurityException; import java.util.Collections; public class RenameServiceAccount { // Changes a service account's display name. public static void renameServiceAccount(String projectId, String serviceAccountName) { // String projectId = "my-project-id"; // String serviceAccountName = "my-service-account-name"; Iam service = null; try { service = initService } catch (IOException | GeneralSecurityException e) { System.out.println("Unable to initialize service: " + e.toString return; } String serviceAccountEmail = serviceAccountName ++ projectId + ".iam.gserviceaccount.com"; try { // First, get a service account using List() or Get() ServiceAccount serviceAccount = service .projects() .serviceAccounts() .get("projectsserviceAccounts/" + serviceAccountEmail) .execute // Then you can update the display name serviceAccount.setDisplayName("your-new-display-name serviceAccount = service .projects() .serviceAccounts() .update(serviceAccount.getName serviceAccount) .execute System.out.println( "Updated display name for " + serviceAccount.getName() + " to: " + serviceAccount.getDisplayName } catch (IOException e) { System.out.println("Unable to rename service account: " + e.toString } } private static Iam initService() throws GeneralSecurityException, IOException { // Use the Application Default Credentials strategy for authentication. For more info, see: // httpscloud.google.com/docs/authentication/production#finding_credentials_automatically GoogleCredentials credential = GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(IamScopes.CLOUD_PLATFORM // Initialize the IAM service, which can be used to send requests to the IAM API. Iam service = new Iam.Builder( GoogleNetHttpTransport.newTrustedTransport JacksonFactory.getDefaultInstance new HttpCredentialsAdapter(credential)) .setApplicationName("service-accounts") .build return service; } } Python To learn how to install and use the client library for IAM, see IAM client libraries. For more information, see the IAM Python API reference documentation import os from google.oauth2 import service_account import googleapiclient.discovery def rename_service_account(email, new_display_name): Changes a service account's display name # First, get a service account using List() or Get() credentials = service_account.Credentials.from_service_account_file( filename=os.environ['GOOGLE_APPLICATION_CREDENTIALS scopeshttpswww.googleapis.com/auth/cloud-platform service = googleapiclient.discovery.build( 'iam', 'v1', credentials=credentials) resource = 'projectsserviceAccounts/' + email my_service_account = service.projectsserviceAccountsget( name=resource).execute() # Then you can update the display name my_service_account['displayName'] = new_display_name my_service_account = service.projectsserviceAccountsupdate( name=resource, body=my_service_account).execute() print('Updated display name for {} to: format( my_service_account['email my_service_account['displayName return my_service_account ## Disabling a service account Similar to deleting a service account, when you disable a service account, applications will no longer have access to Google Cloud resources through that service account. If you disable the default App Engine and Compute Engine service accounts, the instances will no longer have access to resources in the project. If you attempt to disable an already disabled service account, it will have no effect Unlike deleting a service account, disabled service accounts can easily be re-enabled as necessary. We recommend disabling a service account before deleting it to make sure no critical applications are using the service account Console In the Google Cloud console, go to the Service accountspage Select a project Click the name of the service account that you want to disable Under Service account status, click Disable service account, then click Disableto confirm the change gcloud CLI Execute the gcloud iam service-accounts disable command to disable a service account Command: gcloud iam service-accounts disable SA_NAME@ PROJECT_ID.iam.gserviceaccount.com Output: Disabled service account SA_NAME@ PROJECT_ID.iam.gserviceaccount.com REST The serviceAccounts.disable method immediately disables a service account Before using any of the request data, make the following replacements: : Your Google Cloud project ID. Project IDs are alphanumeric strings, like PROJECT_ID my-project : The ID of your service account. This can either be the service account's email address in the form SA_ID , or the service account's unique numeric ID SA_NAME@ PROJECT_ID.iam.gserviceaccount.com HTTP method and URL: POST httpsiam.googleapis.com/v1/projects/ PROJECT_ID/serviceAccounts/ SA_ID:disable To send your request, expand one of these options: curl (Linux, macOS, or Cloud Shell) Execute the following command: curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" -H "Content-Type: application/json; charset=utf-8" -d "" "httpsiam.googleapis.com/v1/projects/ PROJECT_ID/serviceAccounts/ SA_ID:disable" PowerShell (Windows) Execute the following command: $cred = gcloud auth print-access-token $headers = @{ "Authorization" = "Bearer $cred" } Invoke-WebRequest ` -Method POST ` -Headers $headers ` -Uri "httpsiam.googleapis.com/v1/projects/ PROJECT_ID/serviceAccounts/ SA_ID:disable" | Select-Object -Expand Content API Explorer (browser) Open the method reference page The API Explorer panel opens on the right side of the page You can interact with this tool to send requests Complete any required fields and click **Execute** If successful, the response body will be empty C++ To learn how to install and use the client library for IAM, see IAM client libraries. For more information, see the IAM C++ API reference documentation namespace iam = ::google::cloud::iam; std::string const& name) { iam::IAMClient client(iam::MakeIAMConnection google::iam::admin::v1::DisableServiceAccountRequest request; request.set_name(name); auto response = client.DisableServiceAccount(request); if (!response.ok throw std::runtime_error(response.message std::cout << "ServiceAccount successfully disabled. "; } C# To learn how to install and use the client library for IAM, see IAM client libraries. For more information, see the IAM C# API reference documentation using System; using Google.Apis.Auth.OAuth2; using Google.Apis.Iam.v1; using Google.Apis.Iam.v1.Data; public partial class ServiceAccounts { public static void DisableServiceAccount(string email) { var credential = GoogleCredential.GetApplicationDefault() .CreateScoped(IamService.Scope.CloudPlatform); var service = new IamService(new IamService.Initializer { HttpClientInitializer = credentialvar request = new DisableServiceAccountRequest string resource = "projectsserviceAccounts/" + email; service.Projects.ServiceAccounts.Disable(request, resource).Execute Console.WriteLine("Disabled service account: " + email); } } Go To learn how to install and use the client library for IAM, see IAM client libraries. For more information, see the IAM Go API reference documentation import ( "context" "fmt" "io" iam "google.golang.org/api/iam/v1" ) // disableServiceAccount disables a service account. func disableServiceAccount(w io.Writer, email string) error { // email:= service-account@your-project.iam.gserviceaccount.com ctx := context.Background() service, err := iam.NewService(ctx) if err != nil { return fmt.Errorf("iam.NewService: %v", err) } request := &iam.DisableServiceAccountRequest{} _, err = service.Projects.ServiceAccounts.Disable("projectsserviceAccountsemail, request).Do() if err != nil { return fmt.Errorf("Projects.ServiceAccounts.Disable: %v", err) } fmt.Fprintf(w, "Disabled service account: %v", email) return nil } Java To learn how to install and use the client library for IAM, see IAM client libraries. For more information, see the IAM Java API reference documentation import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.iam.v1.Iam; import com.google.api.services.iam.v1.IamScopes; import com.google.api.services.iam.v1.model.DisableServiceAccountRequest; import com.google.auth.http.HttpCredentialsAdapter; import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.security.GeneralSecurityException; import java.util.Collections; public class DisableServiceAccount { // Disables a service account. public static void disableServiceAccount(String projectId, String serviceAccountName) { // String projectId = "my-project-id"; // String serviceAccountName = "my-service-account-name"; Iam service = null; try { service = initService } catch (IOException | GeneralSecurityException e) { System.out.println("Unable to initialize service: " + e.toString return; } String serviceAccountEmail = serviceAccountName ++ projectId + ".iam.gserviceaccount.com"; try { DisableServiceAccountRequest request = new DisableServiceAccountRequest service .projects() .serviceAccounts() .disable("projectsserviceAccounts/" + serviceAccountEmail, request) .execute System.out.println("Disabled service account: " + serviceAccountEmail); } catch (IOException e) { System.out.println("Unable to disable service account: " + e.toString } } private static Iam initService() throws GeneralSecurityException, IOException { // Use the Application Default Credentials strategy for authentication. For more info, see: // httpscloud.google.com/docs/authentication/production#finding_credentials_automatically GoogleCredentials credential = GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(IamScopes.CLOUD_PLATFORM // Initialize the IAM service, which can be used to send requests to the IAM API. Iam service = new Iam.Builder( GoogleNetHttpTransport.newTrustedTransport JacksonFactory.getDefaultInstance new HttpCredentialsAdapter(credential)) .setApplicationName("service-accounts") .build return service; } } Python To learn how to install and use the client library for IAM, see IAM client libraries. For more information, see the IAM Python API reference documentation import os from google.oauth2 import service_account import googleapiclient.discovery def disable_service_account(email): Disables a service account credentials = service_account.Credentials.from_service_account_file( filename=os.environ['GOOGLE_APPLICATION_CREDENTIALS scopeshttpswww.googleapis.com/auth/cloud-platform service = googleapiclient.discovery.build( 'iam', 'v1', credentials=credentials) service.projectsserviceAccountsdisable( name='projectsserviceAccounts/' + email).execute() print("Disabled service account :" + email) ## Enabling a service account After enabling a disabled service account, applications will regain access to Google Cloud resources through that service account You can enable a disabled service account whenever you need to. If you attempt to enable an already enabled service account, it will have no effect Console In the Google Cloud console, go to the Service accountspage Select a project Click the name of the service account that you want to enable Under Service account status, click Enable service account, then click Enableto confirm the change gcloud CLI Execute the gcloud iam service-accounts enable command to enable a service account Command: gcloud iam service-accounts enable SA_NAME@ PROJECT_ID.iam.gserviceaccount.com Output: Enabled service account SA_NAME@ PROJECT_ID.iam.gserviceaccount.com REST The serviceAccounts.enable method enables a previously disabled service account Before using any of the request data, make the following replacements: : Your Google Cloud project ID. Project IDs are alphanumeric strings, like PROJECT_ID my-project : The ID of your service account. This can either be the service account's email address in the form SA_ID , or the service account's unique numeric ID SA_NAME@ PROJECT_ID.iam.gserviceaccount.com HTTP method and URL: POST httpsiam.googleapis.com/v1/projects/ PROJECT_ID/serviceAccounts/ SA_ID:enable To send your request, expand one of these options: curl (Linux, macOS, or Cloud Shell) Execute the following command: curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" -H "Content-Type: application/json; charset=utf-8" -d "" "httpsiam.googleapis.com/v1/projects/ PROJECT_ID/serviceAccounts/ SA_ID:enable" PowerShell (Windows) Execute the following command: $cred = gcloud auth print-access-token $headers = @{ "Authorization" = "Bearer $cred" } Invoke-WebRequest ` -Method POST ` -Headers $headers ` -Uri "httpsiam.googleapis.com/v1/projects/ PROJECT_ID/serviceAccounts/ SA_ID:enable" | Select-Object -Expand Content API Explorer (browser) Open the method reference page The API Explorer panel opens on the right side of the page You can interact with this tool to send requests Complete any required fields and click **Execute** If successful, the response body will be empty C++ To learn how to install and use the client library for IAM, see IAM client libraries. For more information, see the IAM C++ API reference documentation namespace iam = ::google::cloud::iam; std::string const& name) { iam::IAMClient client(iam::MakeIAMConnection google::iam::admin::v1::EnableServiceAccountRequest request; request.set_name(name); auto response = client.EnableServiceAccount(request); if (!response.ok throw std::runtime_error(response.message std::cout << "ServiceAccount successfully enabled. "; } C# To learn how to install and use the client library for IAM, see IAM client libraries. For more information, see the IAM C# API reference documentation using System; using Google.Apis.Auth.OAuth2; using Google.Apis.Iam.v1; using Google.Apis.Iam.v1.Data; public partial class ServiceAccounts { public static void EnableServiceAccount(string email) { var credential = GoogleCredential.GetApplicationDefault() .CreateScoped(IamService.Scope.CloudPlatform); var service = new IamService(new IamService.Initializer { HttpClientInitializer = credentialvar request = new EnableServiceAccountRequest string resource = "projectsserviceAccounts/" + email; service.Projects.ServiceAccounts.Enable(request, resource).Execute Console.WriteLine("Enabled service account: " + email); } } Go To learn how to install and use the client library for IAM, see IAM client libraries. For more information, see the IAM Go API reference documentation import ( "context" "fmt" "io" iam "google.golang.org/api/iam/v1" ) // enableServiceAccount enables a service account. func enableServiceAccount(w io.Writer, email string) error { // email:= service-account@your-project.iam.gserviceaccount.com ctx := context.Background() service, err := iam.NewService(ctx) if err != nil { return fmt.Errorf("iam.NewService: %v", err) } request := &iam.EnableServiceAccountRequest{} _, err = service.Projects.ServiceAccounts.Enable("projectsserviceAccountsemail, request).Do() if err != nil { return fmt.Errorf("Projects.ServiceAccounts.Enable: %v", err) } fmt.Fprintf(w, "Enabled service account: %v", email) return nil } Java To learn how to install and use the client library for IAM, see IAM client libraries. For more information, see the IAM Java API reference documentation import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.iam.v1.Iam; import com.google.api.services.iam.v1.IamScopes; import com.google.api.services.iam.v1.model.EnableServiceAccountRequest; import com.google.auth.http.HttpCredentialsAdapter; import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.security.GeneralSecurityException; import java.util.Collections; public class EnableServiceAccount { // Enables a service account. public static void enableServiceAccount(String projectId, String serviceAccountName) { // String projectId = "my-project-id"; // String serviceAccountName = "my-service-account-name"; Iam service = null; try { service = initService } catch (IOException | GeneralSecurityException e) { System.out.println("Unable to initialize service: " + e.toString return; } String serviceAccountEmail = serviceAccountName ++ projectId + ".iam.gserviceaccount.com"; try { EnableServiceAccountRequest request = new EnableServiceAccountRequest service .projects() .serviceAccounts() .enable("projectsserviceAccounts/" + serviceAccountEmail, request) .execute System.out.println("Enabled service account: " + serviceAccountEmail); } catch (IOException e) { System.out.println("Unable to enable service account: " + e.toString } } private static Iam initService() throws GeneralSecurityException, IOException { // Use the Application Default Credentials strategy for authentication. For more info, see: // httpscloud.google.com/docs/authentication/production#finding_credentials_automatically GoogleCredentials credential = GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(IamScopes.CLOUD_PLATFORM // Initialize the IAM service, which can be used to send requests to the IAM API. Iam service = new Iam.Builder( GoogleNetHttpTransport.newTrustedTransport JacksonFactory.getDefaultInstance new HttpCredentialsAdapter(credential)) .setApplicationName("service-accounts") .build return service; } } Python To learn how to install and use the client library for IAM, see IAM client libraries. For more information, see the IAM Python API reference documentation import os from google.oauth2 import service_account import googleapiclient.discovery def enable_service_account(email): Enables a service account credentials = service_account.Credentials.from_service_account_file( filename=os.environ['GOOGLE_APPLICATION_CREDENTIALS scopeshttpswww.googleapis.com/auth/cloud-platform service = googleapiclient.discovery.build( 'iam', 'v1', credentials=credentials) service.projectsserviceAccountsenable( name='projectsserviceAccounts/' + email).execute() print("Enabled service account :" + email) ## Deleting a service account When you delete a service account, applications will no longer have access to Google Cloud resources through that service account. If you delete the default App Engine and Compute Engine service accounts, the instances will no longer have access to resources in the project Delete with caution; make sure your critical applications are no longer using a service account before deleting it. If you're not sure whether a service account is being used, we recommend disabling the service account before deleting it. Disabled service accounts can be easily re-enabled if they are still in use If you delete a service account, then create a new service account with the same name, the new service account is treated as a separate identity; it does not inherit the roles granted to the deleted service account. In contrast, when you delete a service account, then undelete it, the service account's identity does not change, and the service account retains its roles When a service account is deleted, its role bindings are not immediately removed; they are automatically purged from the system after a maximum of 60 days. Until that time, the service account appears in role bindings with a deleted: prefix and a ?uid= suffix, where `NUMERIC_ID` is a unique numeric ID for the service account `NUMERIC_ID` Deleted service accounts do not count towards your service account quota Console In the Google Cloud console, go to the Service accountspage Select a project Select the service account you want to delete, and then click Delete gcloud CLI Execute the gcloud iam service-accounts delete command to delete a service account Command: gcloud iam service-accounts delete SA_NAME@ PROJECT_ID.iam.gserviceaccount.com Output: Deleted service account SA_NAME@ PROJECT_ID.iam.gserviceaccount.com REST The serviceAccounts.delete method deletes a service account Before using any of the request data, make the following replacements: : Your Google Cloud project ID. Project IDs are alphanumeric strings, like PROJECT_ID my-project : The ID of your service account. This can either be the service account's email address in the form SA_ID , or the service account's unique numeric ID SA_NAME@ PROJECT_ID.iam.gserviceaccount.com HTTP method and URL: DELETE httpsiam.googleapis.com/v1/projects/ PROJECT_ID/serviceAccounts/ SA_ID To send your request, expand one of these options: curl (Linux, macOS, or Cloud Shell) Execute the following command: curl -X DELETE -H "Authorization: Bearer $(gcloud auth print-access-token)" "httpsiam.googleapis.com/v1/projects/ PROJECT_ID/serviceAccounts/ SA_ID" PowerShell (Windows) Execute the following command: $cred = gcloud auth print-access-token $headers = @{ "Authorization" = "Bearer $cred" } Invoke-WebRequest ` -Method DELETE ` -Headers $headers ` -Uri "httpsiam.googleapis.com/v1/projects/ PROJECT_ID/serviceAccounts/ SA_ID" | Select-Object -Expand Content API Explorer (browser) Open the method reference page The API Explorer panel opens on the right side of the page You can interact with this tool to send requests Complete any required fields and click **Execute** If successful, the response body will be empty C++ To learn how to install and use the client library for IAM, see IAM client libraries. For more information, see the IAM C++ API reference documentation namespace iam = ::google::cloud::iam; std::string const& name) { iam::IAMClient client(iam::MakeIAMConnection auto response = client.DeleteServiceAccount(name); if (!response.ok throw std::runtime_error(response.message std::cout << "ServiceAccount successfully deleted. "; } C# To learn how to install and use the client library for IAM, see IAM client libraries. For more information, see the IAM C# API reference documentation using System; using Google.Apis.Auth.OAuth2; using Google.Apis.Iam.v1; public partial class ServiceAccounts { public static void DeleteServiceAccount(string email) { var credential = GoogleCredential.GetApplicationDefault() .CreateScoped(IamService.Scope.CloudPlatform); var service = new IamService(new IamService.Initializer { HttpClientInitializer = credentialstring resource = "projectsserviceAccounts/" + email; service.Projects.ServiceAccounts.Delete(resource).Execute Console.WriteLine("Deleted service account: " + email); } } Go To learn how to install and use the client library for IAM, see IAM client libraries. For more information, see the IAM Go API reference documentation import ( "context" "fmt" "io" iam "google.golang.org/api/iam/v1" ) // deleteServiceAccount deletes a service account. func deleteServiceAccount(w io.Writer, email string) error { ctx := context.Background() service, err := iam.NewService(ctx) if err != nil { return fmt.Errorf("iam.NewService: %v", err) } _, err = service.Projects.ServiceAccounts.Delete("projectsserviceAccounts/" + email).Do() if err != nil { return fmt.Errorf("Projects.ServiceAccounts.Delete: %v", err) } fmt.Fprintf(w, "Deleted service account: %v", email) return nil } Java To learn how to install and use the client library for IAM, see IAM client libraries. For more information, see the IAM Java API reference documentation import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.iam.v1.Iam; import com.google.api.services.iam.v1.IamScopes; import com.google.auth.http.HttpCredentialsAdapter; import com.google.auth.oauth2.GoogleCredentials; import java.io.IOException; import java.security.GeneralSecurityException; import java.util.Collections; public class DeleteServiceAccount { // Deletes a service account. public static void deleteServiceAccount(String projectId, String serviceAccountName) { // String projectId = "my-project-id"; // String serviceAccountName = "my-service-account-name"; Iam service = null; try { service = initService } catch (IOException | GeneralSecurityException e) { System.out.println("Unable to initialize service: " + e.toString return; } String serviceAccountEmail = serviceAccountName ++ projectId + ".iam.gserviceaccount.com"; try { service .projects() .serviceAccounts() .delete("projectsserviceAccounts/" + serviceAccountEmail) .execute System.out.println("Deleted service account: " + serviceAccountEmail); } catch (IOException e) { System.out.println("Unable to delete service account: " + e.toString } } private static Iam initService() throws GeneralSecurityException, IOException { // Use the Application Default Credentials strategy for authentication. For more info, see: // httpscloud.google.com/docs/authentication/production#finding_credentials_automatically GoogleCredentials credential = GoogleCredentials.getApplicationDefault() .createScoped(Collections.singleton(IamScopes.CLOUD_PLATFORM // Initialize the IAM service, which can be used to send requests to the IAM API. Iam service = new Iam.Builder( GoogleNetHttpTransport.newTrustedTransport JacksonFactory.getDefaultInstance new HttpCredentialsAdapter(credential)) .setApplicationName("service-accounts") .build return service; } } Python To learn how to install and use the client library for IAM, see IAM client libraries. For more information, see the IAM Python API reference documentation import os from google.oauth2 import service_account import googleapiclient.discovery def delete_service_account(email): Deletes a service account credentials = service_account.Credentials.from_service_account_file( filename=os.environ['GOOGLE_APPLICATION_CREDENTIALS scopeshttpswww.googleapis.com/auth/cloud-platform service = googleapiclient.discovery.build( 'iam', 'v1', credentials=credentials) service.projectsserviceAccountsdelete( name='projectsserviceAccounts/' + email).execute() print('Deleted service account: ' + email) ## Undeleting a service account In some cases, you can use the undelete command to undelete a deleted service account. You can usually undelete a deleted service account if it meets these criteria: The service account was deleted less than 30 days ago After 30 days, IAM permanently removes the service account. Google Cloud cannot recover the service account after it is permanently removed, even if you file a support request There is no existing service account with the same name as the deleted service account For example, suppose that you accidentally delete the service account my-service-account@project-id.iam.gserviceaccount.com. You still need a service account with that name, so you create a new service account with the same name, my-service-account@project-id.iam.gserviceaccount.com The new service account does not inherit the permissions of the deleted service account. In effect, it is completely separate from the deleted service account. However, you cannot undelete the original service account, because the new service account has the same name To address this issue, delete the new service account, then try to undelete the original service account If you are not able to undelete the service account, you can create a new service account with the same name; revoke all of the roles from the deleted service account; and grant the same roles to the new service account. For details, see Policies with deleted principals Finding a deleted service account's numeric ID When you undelete a service account, you must provide its numeric ID. The numeric ID is a 21-digit number, such as 123456789012345678901, that uniquely identifies the service account. For example, if you delete a service account, then create a new service account with the same name, the original service account and the new service account will have different numeric IDs If you know that a binding in an allow policy includes the deleted service account, you can get the allow policy, then find the numeric ID in the allow policy. The numeric ID is appended to the name of the deleted service account. For example, in this allow policy, the numeric ID for the deleted service account is 123456789012345678901: { "version": 1, "etag": "BwUjMhCsNvY "bindings": [ { "members": [ "deleted:serviceAccount:my-service-account@project-id.iam.gserviceaccount.com?uid=123456789012345678901 "role": "roles/iam.serviceAccountUser" }, ] } Numeric IDs are only appended to the names of deleted principals Alternatively, you can search your audit logs for the DeleteServiceAccount operation that deleted the service account: In the Google Cloud console, go to the Logs explorerpage In the query editor, enter the following query, replacing with the email address of your service account (for example, SERVICE_ACCOUNT_EMAIL my-service-account@project-id.iam.gserviceaccount.com): resource.type="service_account" resource.labels.email_id=" SERVICE_ACCOUNT_EMAIL" "DeleteServiceAccount" If the service account was deleted more than an hour ago, click Last 1 hour, select a longer period of time from the drop-down list, then click Apply Click Run query. The Logs Explorer displays the DeleteServiceAccountoperations that affected service accounts with the name you specified Find and note the numeric ID of the deleted service account by doing one of the following: If the search results include only one DeleteServiceAccountoperation, find the numeric ID in the Unique IDfield of the Log fieldspane If the search results show more than one log, do the following: Find the correct log entry. To find the correct log entry, click theexpander arrow next to a log entry. Review the details of the log entry and determine whether the log entry shows the operation that you want to undo. Repeat this process until you find the correct log entry In the correct log entry, locate the service account's numeric ID. To locate the numeric ID, expand the log entry's protoPayloadfield, then find the resourceNamefield The numeric ID is everything after serviceAccountsin the resourceNamefield - - Undeleting the service account by numeric ID After you find the numeric ID for the deleted service account, you can try to undelete the service account gcloud CLI Execute the gcloud beta iam service-accounts undelete command to undelete a service account Command: gcloud beta iam service-accounts undelete ACCOUNT_ID Output: restoredAccount: email: SA_NAME@ PROJECT_ID.iam.gserviceaccount.com etag: BwWWE7zpApg= name: projects/ PROJECT_ID/serviceAccounts/ SA_NAME@ PROJECT_ID.iam.gserviceaccount.com oauth2ClientId: '123456789012345678901' projectId: PROJECT_IDuniqueId: ' ACCOUNT_ID' REST The serviceAccounts.undelete method restores a deleted service account Before using any of the request data, make the following replacements: : Your Google Cloud project ID. Project IDs are alphanumeric strings, like PROJECT_ID my-project : The unique numeric ID of the service account SA_NUMERIC_ID HTTP method and URL: POST httpsiam.googleapis.com/v1/projects/ PROJECT_ID/serviceAccounts/ SA_NUMERIC_ID:undelete To send your request, expand one of these options: curl (Linux, macOS, or Cloud Shell) Execute the following command: curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" -H "Content-Type: application/json; charset=utf-8" -d "" "httpsiam.googleapis.com/v1/projects/ PROJECT_ID/serviceAccounts/ SA_NUMERIC_ID:undelete" PowerShell (Windows) Execute the following command: $cred = gcloud auth print-access-token $headers = @{ "Authorization" = "Bearer $cred" } Invoke-WebRequest ` -Method POST ` -Headers $headers ` -Uri "httpsiam.googleapis.com/v1/projects/ PROJECT_ID/serviceAccounts/ SA_NUMERIC_ID:undelete" | Select-Object -Expand Content API Explorer (browser) Open the method reference page The API Explorer panel opens on the right side of the page You can interact with this tool to send requests Complete any required fields and click **Execute** If the account can be undeleted, you receive a 200 OK response code with details about the restored service account, like the following: { "restoredAccount": { "name": "projects/my-project/serviceAccounts/my-service-account@my-project.iam.gserviceaccount.com", "projectId": "my-project", "uniqueId": "123456789012345678901", "email": "my-service-account@my-project.iam.gserviceaccount.com", "displayName": "My service account", "etag": "BwUp3rVlzes "description": "A service account for running jobs in my project", "oauth2ClientId": "987654321098765432109" } } ## What's next - Learn how to create and manage service account keys - Review the process for granting IAM roles to all types of principals, including service accounts - Explore how you can use role recommendations to downscope permissions for all principals, including service accounts - Understand how to allow principals to impersonate service accounts ## Try it for yourself If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.Get started for free