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

400 Client Error: Bad Request for url: https://graph.microsoft.com/v1.0/sendMail | Error Message: Resource not found for thesegment 'sendMail'. #418

Open
maisnamraju opened this issue Apr 3, 2020 · 7 comments

Comments

@maisnamraju
Copy link

I've been trying to get the following piece of code working but have been receiving the errors above. I looked into the other issue here related to this error but coulnd't solve it. Would appreciate any tips on this. Thanks. I've attached the logs below and my permissions are also correct in the azure panel.

def send_InvitationToDCM_email(template, P_EMAIL):
    msg_header = 'Invitation To Digital Coffee Meeting'
    credentials = (clientid, secret_key)
    account = Account(credentials, auth_flow_type='credentials', tenant_id=tenant_id)
    m = account.mailbox(resource='dcm@whaii.com')
    print('mailbox')
    m = account.new_message()
    m.to.add(P_EMAIL)
    m.subject = msg_header
    m.body = template
    m.send()
rror on request:
Traceback (most recent call last):
  File "/home/cabox/.pyenv/versions/3.7.0/lib/python3.7/site-packages/O365/connection.py", line 675, in _internal_request
    response.raise_for_status()  # raise 4XX and 5XX error codes.
  File "/home/cabox/.pyenv/versions/3.7.0/lib/python3.7/site-packages/requests/models.py", line 941, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://graph.microsoft.com/v1.0/sendMail

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/cabox/.pyenv/versions/3.7.0/lib/python3.7/site-packages/werkzeug/serving.py", line 323, in run_wsgi
    execute(self.server.app)
  File "/home/cabox/.pyenv/versions/3.7.0/lib/python3.7/site-packages/werkzeug/serving.py", line 312, in execute
    application_iter = app(environ, start_response)
  File "/home/cabox/.pyenv/versions/3.7.0/lib/python3.7/site-packages/flask/app.py", line 2463, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/cabox/.pyenv/versions/3.7.0/lib/python3.7/site-packages/flask/app.py", line 2449, in wsgi_app
    response = self.handle_exception(e)
  File "/home/cabox/.pyenv/versions/3.7.0/lib/python3.7/site-packages/flask_cors/extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/home/cabox/.pyenv/versions/3.7.0/lib/python3.7/site-packages/flask/app.py", line 1866, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/cabox/.pyenv/versions/3.7.0/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/cabox/.pyenv/versions/3.7.0/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/cabox/.pyenv/versions/3.7.0/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/cabox/.pyenv/versions/3.7.0/lib/python3.7/site-packages/flask_cors/extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/home/cabox/.pyenv/versions/3.7.0/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/cabox/.pyenv/versions/3.7.0/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/cabox/.pyenv/versions/3.7.0/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/cabox/.pyenv/versions/3.7.0/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/cabox/workspace/server/routes/index.py", line 78, in send_mail
    send_InvitationToDCM_email('Tet','maisnamraju@gmail.com')
  File "/home/cabox/workspace/server/routes/index.py", line 693, in send_InvitationToDCM_email
    m.send()
  File "/home/cabox/.pyenv/versions/3.7.0/lib/python3.7/site-packages/O365/message.py", line 694, in send
    response = self.con.post(url, data=data)
  File "/home/cabox/.pyenv/versions/3.7.0/lib/python3.7/site-packages/O365/connection.py", line 789, in post
    return self.oauth_request(url, 'post', data=data, **kwargs)
  File "/home/cabox/.pyenv/versions/3.7.0/lib/python3.7/site-packages/O365/connection.py", line 767, in oauth_request
    return self._internal_request(self.session, url, method, **kwargs)
  File "/home/cabox/.pyenv/versions/3.7.0/lib/python3.7/site-packages/O365/connection.py", line 729, in _internal_request
    raise HTTPError('{} | Error Message: {}'.format(e.args[0], error_message), response=response) from None
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://graph.microsoft.com/v1.0/sendMail | Error Message: Resource not found for thesegment 'sendMail'.
@alejcas
Copy link
Member

alejcas commented Apr 7, 2020

m = account.mailbox(resource='dcm@whaii.com')
print('mailbox')
m = account.new_message()

You are overwriting the variable 'm' and also not setting the resource for the message.
You need to set it.

Also, you can generate a new message from the mailbox so you don't have to specify the resource every time.
msg = mailbox.new_message()

account = Account(credentials, auth_flow_type='credentials', tenant_id=tenant_id)
m = account.mailbox(resource='dcm@whaii.com')

Between the above calls, you should always call:

if not account.is_authenticated:
    account.authenticate()

@yegorsb
Copy link

yegorsb commented Jul 20, 2021

@janscas I have a similar code and getting the same bad request url error:

from O365 import Account

#removed the tenant_id
account = Account(credentials, auth_flow_type = 'credentials', tenant_id = '*****')
if account.authenticate():
    print('Authenticated!')

mailbox = account.mailbox()

m = mailbox.new_message()
m.to.add('test@test.com')
m.sender.address = 'test@test.com'
m.body = 'test'
m.subject = 'test'
m.send()

I noticed the api calls a url: "https://graph.microsoft.com/v1.0/sendMail" but shouldn't it be "https://graph.microsoft.com/Mail.Send" ?

I am able to do everything else except for sending an email. Am I missing a step or misunderstanding something?

@alejcas
Copy link
Member

alejcas commented Jul 20, 2021

@yegorsb when using “credentials” flow type the api doesn’t know what account to send from..

You are just logged in as “the app”.

provide a resource to mailbox and proceed.

like:
mailbox = account.mailbox(“user@example.com”)

you will of course need to have access to that account.

@yegorsb
Copy link

yegorsb commented Jul 20, 2021

@janscas Thank you!
Where would I look to see if the application has access to the specified mailbox? I assume its something that can be found in the app registration in Azure? We have an email address that we can use for system notifications. I am just unsure to where to configure that information on Azure enable emailing from that mailbox.

401 Client Error: Unauthorized for url: https://graph.microsoft.com/v1.0/users/user@example.org/sendMail |
Error Message: The token contains no permissions, or permissions can not be understood.

Forgive me for I am new to the configuring things on the back end.

Thank you!

@alejcas
Copy link
Member

alejcas commented Sep 3, 2021

If your account has access to a certain address and you're the creator of the app it should work.

I'm not very deep into azure app registration so i can't really tell

@brytonbeesley
Copy link

For anyone struggling with this issue, you may find it helpful to know this:

If you are using the client credentials grant flow (a.k.a. "Authenticate with your own identity"), and if the account you wish to use as the resource mailbox has been brought into your tenant as a Guest account, it will not work. This approach is not supported. I learned the hard way.

Sources cited:
O365 GitHub documentation
jagjit singh's answer on StackOverflow

@alejcas
Copy link
Member

alejcas commented Apr 28, 2022

Didn’t know. Very useful!

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants