Issue getting data from morningstar API using token

3 次查看(过去 30 天)
I have been trying to pull data from a Morninstar API with the following code:
% Manually set Authorization header field in weboptions
options = weboptions('HeaderFields',{'Authorization',...
['Basic ' matlab.net.base64encode([Username ':' Password])];...
'Cache-Control', 'no-cache'; ...
'Content-Type', 'application/x-www-form-urlencoded';...
'grant_type=authorization_code&code=7962e5a1-dd81-412f-8993-6&redirect_uri=https%3A%2F%2abcd8027.morningstar.com%2Ftestapp%2Fcallback', ''});
% Make a request using these options
response = webwrite('https://www.us-api.morningstar.com/token/oauth', options)
access_token = response.access_token;
token_type = response.token_type;
headerFields = {'Authorization', ['Bearer: ', access_token]};
options2 = weboptions('HeaderFields', headerFields);
data = webread('https://www.us-api.morningstar.com/ec/v1/securities/FOUSA06N2B%7CFEUSA04ABV?universeIds=FOUSA%7CFEUSA%7CFCUSA&langCult=en-US&dataPoints=n%7Ct&currencyId=USD', options2);
I am trying to pull the data on the securities in the following example for starters just to prove out that I can get access to the server before I try getting the data I actually want:
I am successfully getting the token as the value of response above is what is expected if you look at the example here:
So I think my issue is the syntax I am using in setting up options2 to pass my token to the server with the data. I have tried multiple ways of forming the request using both HeaderFields and KeyName/KeyValue. I have also tried setting RequestMethod to 'GET' even though I shouldnt have to do that with webread.
I am sure I am missing something simple and obvious but for the life of me I cannot figure it out!
  1 个评论
Jesse Mills
Jesse Mills 2021-11-29
Apologies, the error I get is in the data = webread line and says the server returns "status 401" which is unauthorized, this is why I think my syntax regarding sending the authorization token is incorrect..

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Call Web Services from MATLAB Using HTTP 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by