Clear Filters
Clear Filters

Authentiate with login credentials

57 views (last 30 days)
Jesse Mills
Jesse Mills on 21 Nov 2021
Edited: Chetan on 2 May 2024
I am trying to login to my portfolio on morningstar (https://www.morningstar.com/portfolio-manager/my-view) and am having some issues. I have reviewed several other posts on this topic (certainly this one: https://www.mathworks.com/matlabcentral/answers/480026-how-do-i-preemptively-include-a-basic-authentication-header-when-working-with-webread-webwrite) and I am having a hard time. I am getting an bad request error (status 400) when I try to communicate my login credentials to the login page. I have tried:
options = weboptions('HeaderFields',{'Authorization',...
['Basic ' matlab.net.base64encode([myUsername ':' myPassword])]});
AND
options = weboptions('Username',myUsername,'Password',myPassword);
to set my credentials before using webread on the login page. What am I missing? I am able to communicate (without real username or password obviously) with https://httpbin.org/hidden-basic-auth/myUser/myPassword succesfully as discussed in the example above.
  2 Comments
Rik
Rik on 21 Nov 2021
Maybe the site uses cookies to store authentication on the login page, which it then uses on other pages. I don't think this syntax handles that.
You could try by disabling all cookies in your normal browser and then try to use the website.
Jesse Mills
Jesse Mills on 24 Nov 2021
Edited: Jesse Mills on 24 Nov 2021
Hey rik, thanks for the suggestion, I tried this without success, have also tried webwrite to the login page as well.
I think morningstar may have a oauth2 password grant request which is different (I think?) than the basic authorization that some of the examples and my script are using.
This thread seems to point the way but I havent digested how all this works. This type of stuff is not my wheelhouse...

Sign in to comment.

Answers (1)

Chetan
Chetan on 2 May 2024
Edited: Chetan on 2 May 2024
I understand that you're attempting to log in to your Morningstar portfolio using MATLAB's `webread` function but are encountering a status 400 bad request error.
Given the authentication flow you've described, it appears the direct use of 'Username' and 'Password' in `weboptions` isn't compatible with Morningstar's OAuth token-based authentication system.
Instead, you should initially request an authentication token from Morningstar's OAuth endpoint and then utilize this token for your requests as referred to in the documentation for the Morningstar website:
Here’s how you can modify your MATLAB code to comply with the required authentication process:
1. Request an Authentication Token
Utilize MATLAB's `webread` or `webwrite` function to request a token. You must encode your username and password in Base64 and include this in the Authorization header for the token request.
username = 'yourUsername';
password = 'yourPassword';
credentials = matlab.net.base64encode([username ':' password]);
options = weboptions('HeaderFields', {'Authorization', ['Basic ' credentials]});
tokenResponse = webwrite('https://www.us-api.morningstar.com/token/oauth', '', options);
token = tokenResponse.token; % Assuming the token is returned in this field
2. Use the Token for Subsequent Requests:
After obtaining the token, include it in the Authorization header of your subsequent requests.
dataOptions = weboptions('HeaderFields', {'Authorization', ['Bearer ' token]});
dataURL = 'https://www.morningstar.com/portfolio-manager/my-view'; % Adjust to the actual data URL you need
responseData = webread(dataURL, dataOptions);
For more information, refer to the following MathWorks documentation:
Hope it helps

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!