github.com/yaegashi/msgraph.go@v0.1.4/msauth/client_credentials_grant.go (about)

     1  package msauth
     2  
     3  import (
     4  	"context"
     5  
     6  	"golang.org/x/oauth2"
     7  	"golang.org/x/oauth2/clientcredentials"
     8  	"golang.org/x/oauth2/microsoft"
     9  )
    10  
    11  // ClientCredentialsGrant performs OAuth 2.0 client credentials grant and returns auto-refreshing TokenSource
    12  func (m *Manager) ClientCredentialsGrant(ctx context.Context, tenantID, clientID, clientSecret string, scopes []string) (oauth2.TokenSource, error) {
    13  	config := &clientcredentials.Config{
    14  		ClientID:     clientID,
    15  		ClientSecret: clientSecret,
    16  		TokenURL:     microsoft.AzureADEndpoint(tenantID).TokenURL,
    17  		Scopes:       scopes,
    18  		AuthStyle:    oauth2.AuthStyleInParams,
    19  	}
    20  	var err error
    21  	ts := config.TokenSource(ctx)
    22  	_, err = ts.Token()
    23  	if err != nil {
    24  		return nil, err
    25  	}
    26  	return ts, nil
    27  }