Authentiate with login credentials
23 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Rik
am 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.
Antworten (1)
Chetan
am 2 Mai 2024
Bearbeitet: Chetan
am 2 Mai 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:
- https://www.mathworks.com/help/matlab/ref/webread.html
- https://www.mathworks.com/help/matlab/ref/webwrite.html
- https://www.mathworks.com/help/matlab/ref/weboptions.html
- https://www.mathworks.com/help/matlab/ref/matlab.net.base64encode.html
Hope it helps
0 Kommentare
Siehe auch
Kategorien
Mehr zu Web Services finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!