python-o365 icon indicating copy to clipboard operation
python-o365 copied to clipboard

MissingCodeError: (missing_code) Missing code parameter in response.

Open dsuch opened this issue 3 years ago • 2 comments

Hello,

many thanks for creating the library.

I am at version 2.0.19, using the code below in order to implement a custom flow:

from O365 import Account

credentials = ('123', '456')
scopes = ['basic']

account = Account(credentials)
auth_url, state = account.con.get_authorization_url(requested_scopes=scopes)
account.con.request_token(authorization_url=auth_url, state=state)

I am getting the exception below (I am providing a full traceback for completeness):

Traceback (most recent call last):
  File "office1.py", line 9, in <module>
    account.con.request_token(authorization_url=auth_url, state=state)
  File "/home/dsuch/projects/zatosource-zato/3.2/code/lib/python3.8/site-packages/O365/connection.py", line 510, in request_token
    token_response = self.session.fetch_token(
  File "/home/dsuch/projects/zatosource-zato/3.2/code/lib/python3.8/site-packages/requests_oauthlib/oauth2_session.py", line 244, in fetch_token
    self._client.parse_request_uri_response(
  File "/home/dsuch/projects/zatosource-zato/3.2/code/lib/python3.8/site-packages/oauthlib/oauth2/rfc6749/clients/web_application.py", line 220, in parse_request_uri_response
    response = parse_authorization_code_response(uri, state=state)
  File "/home/dsuch/projects/zatosource-zato/3.2/code/lib/python3.8/site-packages/oauthlib/oauth2/rfc6749/parameters.py", line 284, in parse_authorization_code_response
    raise MissingCodeError("Missing code parameter in response.")
oauthlib.oauth2.rfc6749.errors.MissingCodeError: (missing_code) Missing code parameter in response.

I am not quite sure how to interpret it because there is no response yet at this point - the parse_authorization_code_response function is trying to extract a code from what is actually the authentication URL, i.e. it is doing it before the request is even sent.

I am not certain if this is what should be happening or perhaps whether I am not passing the correct parameters to account.con.request_token, although I think this is what README says to use?

Could you please help me out with that?

Thank you.

dsuch avatar Aug 24 '22 15:08 dsuch

To answer my own question - yes, I was using an invalid input parameter. The auth_url is something that a "user" needs to open in a web browser and explicitly approve the result of.

Then, Azure will redirect the user to a URL such as "https://login.microsoftonline.com/common/oauth2/nativeclient?code=Q.123.4567890&state=abcdef".

It is the above URL that the account.con.request_token method should be given.

I was hoping I would be able to avoid this entire approval flow in my case which is one with complete automation, without a need for user intervention, yet without requiring the usage of the auth_flow_type of "credentials" either because that requires admin privileges or cooperation from an administrator.

It appears that this is not doable with Office 365 as such?

Thank you once more.

dsuch avatar Aug 24 '22 16:08 dsuch

Hi there are different authentication methods. But iyou can use the delegated authentication, sign in once and then just use the api at least once every 90 days. That way you will never need to authenticate again.

alejcas avatar Aug 26 '22 13:08 alejcas