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

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?

 Risposta accettata

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 Commenti

Oh okay perfect. However, how do I make it so that when it sends the request it opens a new tab in the window- so that the user can authorize their account- and then get the re-directed url?
Right now, this code is just giving me the same uri request that I sent in because I never get re-direct.
I think you cannot open a window to ask user to login. However if you are able to use authentication scheme you can try the following
Oh ok, that's what I was afraid of but wasn't sure. Thank you! Appreciate the help!
@Mohammad Sami, what if finaluri is a ZIP file. How can it be saved?

Accedi per commentare.

Più risposte (0)

Prodotti

Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by