Is there a way to get the redirect url after a https / api request?

7 views (last 30 days)
I'm using the Spotify API and in order to get authorization I need to get the url of the redirected page after completing the first api request.
client_id = "CLIENT_CODE"; % Specific Application client id
redirect_uri = 'REDIRECT_URL';
scope = 'user-read-private user-read-email';
response_type = 'code';
% Authorization url
auth_url = "https://accounts.spotify.com/authorize?";
auth_url = strcat(auth_url, "client_id="+client_id);
auth_url = strcat(auth_url, "&redirect_uri="+redirect_uri);
auth_url = strcat(auth_url, "&response_type="+response_type);
web(auth_url)
Right now, when you input my actual client code and redirect_url, it opens up the authorization page and then after you authorize (or don't) the page is redirect to a url like this:
REDIRECT_URL/?code=AQDkK3AcQqCnoiV37f1Ied1DjOyEINjnzKB1HA-JO7c0HIyYL2VR0PGrbawMQiKkuquXUl7UlI19VaEvDp2jzxsOF_scEItkktas0voMUgc4p2q1W8i0Kx4a_i3KB0dB6AtRO_ruQWXCVto_9ZQLn_bqJ7soiGsYN_3rLO6nheky5_VREMdzi9IVlzQg0A
This information after the 'code=' is the authorization key that I need.
Does anyone know how to retrieve this in MATLAB?

Accepted Answer

Mohammad Sami
Mohammad Sami on 7 Apr 2022
You can use the matlab.net.http interface to follow the redirects.
request = matlab.net.http.RequestMessage;
uri = matlab.net.URI('https://goo.gl/maps/co8U2mMv8zFweDzN8');
[response,completedrequest,history] = send(request,uri);
finaluri = history(end).URI;
disp(finaluri);
URI with properties: Scheme: "https" UserInfo: [0×0 string] Host: "consent.google.com" Port: [] EncodedAuthority: "consent.google.com" Path: ["" "ml"] EncodedPath: "/ml" Query: [1×6 matlab.net.QueryParameter] EncodedQuery: "continue=https://www.google.com/maps/place/Houston,%2BTX,%2BUSA/@29.8174782,-95.6814846,10z/data%3D!3m1!4b1!4m5!3m4!1s0x8640b8b4488d8501:0xca0d02def365053b!8m2!3d29.7596088!4d-95.3723145&gl=IE&m=0&pc=m&hl=en&src=1" Fragment: [0×0 string] Absolute: 1 EncodedURI: "https://consent.google.com/ml?continue=https://www.google.com/maps/place/Houston,%2BTX,%2BUSA/@29.8174782,-95.6814846,10z/data%3D!3m1!4b1!4m5!3m4!1s0x8640b8b4488d8501:0xca0d02def365053b!8m2!3d29.7596088!4d-95.3723145&gl=IE&m=0&pc=m&hl=en&src=1"
  4 Comments
Hank Helmers
Hank Helmers on 8 Apr 2022
Oh ok, that's what I was afraid of but wasn't sure. Thank you! Appreciate the help!

Sign in to comment.

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!