Skip to main content

Get IAM policies

This page describes how to programmatically retrieve policies assigned to a Library API resource.

Sample

The following code sample shows how to retrieve policies assigned to a Library API resource.

Replace [PLAYLIST] with the resource name of the playlist that the policy should be retrieved from, e.g. users/123/playlists/456.

tip

Tip: do not forget to replace CLIENT-ID and CLIENT-SECRET with valid IAM Service Account client credentials.

package main

import (
"context"
"log"

"golang.org/x/oauth2/clientcredentials"

"google.golang.org/api/option"
"google.golang.org/genproto/googleapis/iam/v1"

gapic "github.com/animeapis/api-go-client/library/v1alpha1"
)

var (
Playlist = "[PLAYLIST]"

ClientID = "[CLIENT-ID]"
ClientSecret = "[CLIENT-SECRET]"
)

var (
TokenURL = "https://accounts.animeshon.com/o/oauth2/token"
Endpoint = "library.animeapis.com:443"
)

func main() {
ctx := context.Background()

config := &clientcredentials.Config{
ClientID: ClientID,
ClientSecret: ClientSecret,
TokenURL: TokenURL,
}

options := []option.ClientOption{
option.WithEndpoint(Endpoint),
option.WithTokenSource(config.TokenSource(ctx)),
}

client, err := gapic.NewClient(ctx, options...)
if err != nil {
log.Fatalf("NewClient: %s", err)
}

request := &iam.GetIamPolicyRequest{
Resource: Playlist,
}

policy, err := client.GetIamPolicy(ctx, request)
if err != nil {
log.Fatalf("GetIamPolicy: %s", err)
}

log.Printf("resource: %s", Playlist)
log.Printf("bindings: %v", policy.Bindings)
}
View on GitHub