Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with MSGraph SDK, user: sendMail. #654

Closed
Nivedita-coder opened this issue Jan 24, 2024 · 2 comments
Closed

Issue with MSGraph SDK, user: sendMail. #654

Nivedita-coder opened this issue Jan 24, 2024 · 2 comments
Assignees
Labels
Needs Attention 👋 question Further information is requested

Comments

@Nivedita-coder
Copy link

Nivedita-coder commented Jan 24, 2024

Hi Team,

Here, I want to use the accessToken which I obtained by following "device code" flow authentication. Now, I want to use that accessToken in place of cred to send the email via MSAL Graph API. After using accessToken in place of cred the mail initiation process is not working.

Error:
2024/01/24 15:52:16 Error sending mail: error status code received from the API

Previous implementation:

cred, err := azidentity.NewDeviceCodeCredential(&azidentity.DeviceCodeCredentialOptions{
		TenantID: "",
		ClientID: "",
		UserPrompt: func(ctx context.Context, message azidentity.DeviceCodeMessage) error {
			fmt.Println(message.Message)
			return nil
		},
	})

	if err != nil {
		fmt.Printf("Error creating credentials: %v\n", err)
	}
	auth, err := a.NewAzureIdentityAuthenticationProviderWithScopes(cred, []string{"Mail.Read", "Mail.Send"})
	if err != nil {
		fmt.Printf("Error authentication provider : %v\n", err)
		return
	}
	adapter, err := msgraphsdk.NewGraphRequestAdapter(auth)
	if err != nil {
		fmt.Printf("Error creating adapter: %v\n", err)
		return
	}

	graphClient := msgraphsdk.NewGraphServiceClient(adapter)

Current implementaion:

type CustomTokenCredential struct {
	accessToken string
}

func (c *CustomTokenCredential) GetToken(ctx context.Context, options policy.TokenRequestOptions) (azcore.AccessToken, error) {
	return azcore.AccessToken{
		Token:     c.accessToken,
		ExpiresOn: time.Now().Add(90 * 24 * time.Hour),
	}, nil
}

func SendEmail(accessToken string) {

	tokenCredential := &CustomTokenCredential{
		accessToken: accessToken,
	}

	auth, err := azureKiota.NewAzureIdentityAuthenticationProviderWithScopes(tokenCredential, []string{"Mail.Read", "Mail.Send"})
	if err != nil {
		log.Printf("Error authentication provider : %v\n", err)
		return
	}
	adapter, err := msgraphsdk.NewGraphRequestAdapter(auth)
	if err != nil {
		log.Printf("Error creating adapter: %v\n", err)
		return
	}

	graphClient := msgraphsdk.NewGraphServiceClient(adapter)
	
@baywet baywet added question Further information is requested Needs Attention 👋 labels Jan 24, 2024
@Nivedita-coder
Copy link
Author

Hey @baywet @rkodev,
Thanks for your attention!

Any updates? It's bit urgent! 🙌

@Nivedita-coder
Copy link
Author

Hey @baywet,

The problem is now better understood; although the provided code functions properly, it encounters difficulties in sending an email with the "Mail.Read" and "Mail.Send" scopes using the cached access token. Which scope should I employ to enable email transmission with the cached token, allowing us to bypass authentication for a specific duration!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Attention 👋 question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants