Authentiate with login credentials
조회 수: 14 (최근 30일)
이전 댓글 표시
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
Rik
2021년 11월 21일
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.
답변 (1개)
Chetan
2024년 5월 2일
편집: Chetan
2024년 5월 2일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!