func GetServicePrincipalTokenFromMSI(ctx context.Context, endpoint string) (*adal.ServicePrincipalToken, error) {
	idEndpoint, err := adal.GetMSIAppServiceEndpoint()
	if err != nil {
		return &adal.ServicePrincipalToken{}, fmt.Errorf("failed to retreive MSI endpoint: %s", err.Error())
	}
	idHeader, exists := os.LookupEnv("IDENTITY_HEADER")
	if !exists {
		return &adal.ServicePrincipalToken{}, fmt.Errorf("failed to retreive MSI Identity Header: %s", err.Error())
	}
	apiVersion := "2019-08-01"
	url := fmt.Sprintf("%s?resource=%s&api-version=%s", idEndpoint, endpoint, apiVersion)
	client := &http.Client{
		Timeout: 10 * time.Second,
	}
	req, _ := http.NewRequestWithContext(ctx, "GET", url, nil)
	req.Header.Set("X-IDENTITY-HEADER", idHeader)
	res, err := client.Do(req)
	if err != nil {
		return &adal.ServicePrincipalToken{}, fmt.Errorf("failed token request with URL %s: %s", url, err.Error())
	}
	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return &adal.ServicePrincipalToken{}, fmt.Errorf("failed to read token request response body: %s", err.Error())
	}
	var msiToken MSIResourceToken
	err = json.Unmarshal(body, &msiToken)
	if err != nil {
		return &adal.ServicePrincipalToken{}, fmt.Errorf("failed to unmarshal response into MSIResponseToken: %s", err.Error())
	}
	tenantID := strings.Split(os.Getenv("WEBSITE_AUTH_OPENID_ISSUER"), "/")[3]
	oauthConfig, err := adal.NewOAuthConfig(endpoint, tenantID)
	if err != nil {
		return &adal.ServicePrincipalToken{}, fmt.Errorf("failed to create new OAuth config: %s", err.Error())
	}
	spToken, err := adal.NewServicePrincipalTokenFromManualToken(*oauthConfig, msiToken.ClientID, msiToken.Resource, adal.Token{
		AccessToken: msiToken.AccessToken,
		Resource:    msiToken.Resource,
		ExpiresOn:   msiToken.ExpiresOn,
		Type:        msiToken.TokenType,
	}, nil)
	if err != nil {
		return &adal.ServicePrincipalToken{}, fmt.Errorf("failed to create Service Principal token: %s", err.Error())
	}
	return spToken, nil
}
GetServicePrincipalTokenFromMSI gets a standard Service Principal Token from a Managed Service Identity that's
assigned to an Azure Function.
package azure
import (
	"context"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"os"
	"strings"
	"time"
	"github.com/Azure/go-autorest/autorest/adal"
)
type MSIResourceToken struct {
	AccessToken string      `json:"access_token"`
	ExpiresOn   json.Number `json:"expires_on"`
	Resource    string      `json:"resource"`
	TokenType   string      `json:"token_type"`
	ClientID    string      `json:"client_id"`
}