microsoftgraph / microsoftgraph/msgraph-sdk-go

Unable to assign licence to user

Open
#573 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Needs Attention :wave: question
Dominant language
Go
Stars
335
Forks
43
Avg merge
15h 19m
Merged PRs (30d)
3

Description

Hello!

Following the example here I am trying to assing a licence to an user in Azure AD. I created the following dummy script to illustrate my problem:

package main

import (
	"context"
	"log"

	"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
	"github.com/google/uuid"
	msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
	graphmodels "github.com/microsoftgraph/msgraph-sdk-go/models"
	graphusers "github.com/microsoftgraph/msgraph-sdk-go/users"
)

func main() {
	// For testing fill the values here
	tenantID := "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
	clientID := "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
	clientSecret := "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
	userID := "02a83423-br0c-4376-93a8-ed405936e0e6" // I made this up for creating the issue
	licenceSKUID := "e8b967fb-3c0c-4ba7-225e-f857999a9d30_f30db741-07e9-47e9-999c-80727f46fr8f" // This as well

	// Authenticate with Azure AD and create a Graph client
	cred, err := azidentity.NewClientSecretCredential(tenantID, clientID, clientSecret, nil)
	if err != nil {
		log.Fatal(err)
	}
	graphClient, err := msgraphsdk.NewGraphServiceClientWithCredentials(
		cred,
		[]string{"https://graph.microsoft.com/.default"},
	)
	if err != nil {
		log.Fatal(err)
	}

	// Look for the user to make sure the UUID is correct
	u, err := graphClient.Users().ByUserIdString(userID).Get(context.Background(), nil)
	if err != nil {
		log.Fatal(err)
	}
	username := *u.GetDisplayName()
	log.Printf("User %s found", username)

	// Look for the license to make sure the UUID is correct
	l, err := graphClient.SubscribedSkus().BySubscribedSkuIdString(licenceSKUID).Get(context.Background(), nil)
	if err != nil {
		log.Fatal(err)
	}
	skuID := l.GetSkuId()
	licenceName := *l.GetSkuPartNumber()
	log.Printf("License %s found", licenceName)
	log.Printf("Assigning license %s to user %s", licenceName, username)

	// Assign the license to the user according to the documentation
	requestBody := graphusers.NewItemAssignLicensePostRequestBody()

	assignedLicense := graphmodels.NewAssignedLicense()
	disabledPlans := []uuid.UUID{}
	assignedLicense.SetDisabledPlans(disabledPlans)
	assignedLicense.SetSkuId(skuID)
	addLicenses := []graphmodels.AssignedLicenseable{
		assignedLicense,
	}
	requestBody.SetAddLicenses(addLicenses)
	removeLicenses := []uuid.UUID{}
	requestBody.SetRemoveLicenses(removeLicenses)
	assignLicense, err := graphClient.Users().ByUserIdString(userID).AssignLicense().Post(context.Background(), requestBody, nil)
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("%v", assignLicense.GetAssignedLicenses())
}

So to make sure I only pass existing IDs to the licence assignment code, I use the same graph client to get the objects before. After running it, the logs are:

2023/09/08 16:25:02 User Test User found
2023/09/08 16:25:02 License FLOW_FREE found
2023/09/08 16:25:02 Assigning license FLOW_FREE to user Test User
2023/09/08 16:25:03 error status code received from the API
exit status 1

I made sure the client being used has the appropriate permissions:

image

Given the very vague error message I can not really debug any further on the issue. I tried to use a debugger to look at the error but the values are strange:

err.(*odataerrors.ODataError).ApiError.Message = ""
err.(*odataerrors.ODataError).ApiError.ResponseStatusCode = 0
err.(*odataerrors.ODataError).ApiError.ResponseHeaders.header.headers = map[string]map[string]struct {} []

As a side note (maybe it its of importance). I am using the "long" version of the licence ID in the code (<TENANT_ID>_<SKU_ID>) because if I don't then this line

l, err := graphClient.SubscribedSkus().BySubscribedSkuIdString(licenceSKUID).Get(context.Background(), nil)

will be the one failing with a similar error (also empty message and status code 0)

I would appreciate some help, because a response code of 0 seems to be me like an indication of something bigger being wrong. It could also be that the documentation is not correct.

Versions

I am using the following library versions:

github.com/google/uuid v1.3.1
github.com/microsoftgraph/msgraph-sdk-go v1.17.0

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the provided Go example, focusing on the SubscribedSkus().BySubscribedSkuIdString(...).Get and Users().ByUserIdString(...).AssignLicense().Post entry points. Compare the returned errors and API responses to determine whether the status-0 error comes from the SDK or Microsoft Graph, then document a reproducible cause and actionable resolution.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.