question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error Message: Resource not found for the segment 'sendMail'

See original GitHub issue

Hi,

I think that I am missing something in the code, but I keep getting this error: requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://graph.microsoft.com/v1.0/sendMail | Error Message: Resource not found for the segment 'sendMail'. Here is a snippet for the code:

def send_email(message, email, email_title, APP_CLIENT_ID, APP_CLIENT_SECRET, TENANT_ID):
    print('Starting send_email function')
    credentials = (APP_CLIENT_ID, APP_CLIENT_SECRET)
    account = Account(credentials, auth_flow_type='credentials', tenant_id=TENANT_ID)
    if account.authenticate():
        print('Authenticated!')
    m = account.new_message()
    m.to.add(email)
    m.subject = email_title
    m.body = 'message'
    m.send()

I’ve changed m.body to string in this snippet, since originally message variable stores MIMEMultipart value, which (according to another reported issue) is not supported. Could you please have a quick look and see if there is any issue in the code.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
janscascommented, Feb 6, 2020

Yes, when using the client credentials auth flow, you are authentication with the app identity. So you will have access to all the resources the app has access to.

In order to send or read emails (or whatever action) you need to tell the library on who’s account you want to do this action. The app can’t guess it…

This is done using resources.

Following you example (and provided that your app has access to “example@example.com” account):

def send_email(message, email, email_title, APP_CLIENT_ID, APP_CLIENT_SECRET, TENANT_ID):
    print('Starting send_email function')
    credentials = (APP_CLIENT_ID, APP_CLIENT_SECRET)
    account = Account(credentials, auth_flow_type='credentials', tenant_id=TENANT_ID)
    if account.authenticate():
        print('Authenticated!')
    m = account.new_message(resource='example@example.com')
    m.to.add(email)
    m.subject = email_title
    m.body = 'message'
    m.send()

If you are going to do a lot of actions it’s better to use the mailbox so you define the resource just once:

# ...

account = Account(credentials, auth_flow_type='credentials', tenant_id=TENANT_ID)
example_mailbox = account.mailbox(resource='example@example.com')
msg = example_mailbox.new_message()

# ...
0reactions
swetasingh0113commented, Oct 11, 2021

Hi, This code is working for powershell and I am getting a valid token and able to send mail using Microsoft API and also I am able to get response from Postman.

User=“abc@.com” #User to be used for the delegated Permissions $PW=“******” #Password

The resource URI

$resource = “https://graph.microsoft.com” $clientid = “XXXX” #Application (client) ID from Azure App registration Page

$tokenBody = @{
Grant_Type = “password”
Scope = “user.read+openid+profile+offline_access”
Client_Id = $clientId
username = $User password = $pw resource = $resource }

Please help me to do in python script, I am using o365 library but I don’t know how to use it to create a connection with clientId,username and password. and also how I can set above those properties? I can’t find any examples of how to use delegated (“On behalf of a user”) access automatically.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting error Resource not found for the segment while using ...
I wanted to Send email via graph API. I followed the documentation and Created a application in Azure Directory and got Admin Consent...
Read more >
microsoft graph api - sendMail returns ResourceNotFound
I'm trying to send an email message using the Microsoft Graph REST API ... "ResourceNotFound", "message": "Resource could not be discovered.
Read more >
[Solved] Resource not found for the segment in Power Automate
This will solve the error message Resource not found for the segment in Power Automate. The image below shows the value inputs without ......
Read more >
getting an error "Resource not found for the segment...
Hi,. I am trying to send an email through ms flow. This ms Flow is triggering from the custom entity with help of...
Read more >
I am receiving "Resource not found for the segment ...
However, when I am trying to download the same files through REST API, I am receiving error: {"error":{"code":"4a9b2a5d-a6c7-465c-b001- ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found