How to access the Wx Horizon API with credentials cliend_ID and client_secret and then download the data?

8 views (last 30 days)
Hi all,
I want to access and download data from the Wx Horizon API and I only have the client_id and client_secret. Can someone help with combining the credentials for use in access token request authorization, and getting the data?
Thank you for your help.

Accepted Answer

Pratik
Pratik on 30 Jan 2024
Hi Sanley,
As per my understanding, you want access token using “client_id” and “client_secret” as credentials and download data using the token from Wx Horizon API.
To obtain an access token in MATLAB, an HTTP “POST” request is made to the API's token endpoint using the provided “client_id” and “client_secret”. Subsequently, to retrieve data from the API, an HTTP “GET” request with the acquired access token can be used.
Please refer to the MATLAB code below for reference:
% Your client credentials
client_id = 'your_client_id';
client_secret = 'your_client_secret';
% API token endpoint
url = 'https://authorization-server.com/token'; % The URL of the authorization server
% HTTP options for the token request
options = weboptions('RequestMethod', 'post', 'MediaType', 'application/x-www-form-urlencoded');
% Prepare the body for the token request
body = ['grant_type=client_credentials&client_id=' urlencode(client_id) '&client_secret=' urlencode(client_secret)];
% Request the access token
token_response = webwrite(url, body, options);
access_token = token_response.access_token;
data_url = 'https://protected-resource.com/data'; % The URL of the protected resource
% Create a web options object with the authorization header
options = weboptions('RequestMethod', 'get', 'HeaderFields', {'Authorization', ['Bearer ' access_token]});
% Send the request and get the data as a structure
data = webread(data_url, options);
% Display the data
disp(data);
Please refer to the documentation of “weboptions”, “webwrite” and “webread” for more information:
  1. “weboptions”: www.mathworks.com/help/matlab/ref/weboptions.html
  2. “webwrite”: www.mathworks.com/help/matlab/ref/webwrite.html
  3. “webread”: www.mathworks.com/help/matlab/ref/webread.html
I hope this helps.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!