Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^4: Need to resolve the API call query

by davido (Cardinal)
on Jun 04, 2021 at 17:37 UTC ( #11133532=note: print w/replies, xml ) Need Help??


in reply to Re^3: Need to resolve the API call query
in thread Need to resolve the API call query

Or, for those who prefer to be able to read the code after it's been written, here's a version that is legible:

use strict; use warnings; use URI; use LWP::UserAgent; my $ua = LWP::UserAgent->new(); $ua->proxy(['http'] => 'http://proxy.org.com:8080'); my $uri = URI->new('https://dev.preview.com/oauth2/default/v1/authoriz +e'); $uri->query_form( client_id => 'XXXXXXXXXXXXXXX', response_type => 'code', scope => 'openid', redirect_uri => 'http%3A%2F%2Ftestchandan.com%3A5001', state => '1234', nonce => 'UBGW' ); $ua->get($uri); print Dumper "$uri\n";

Now that I can see it legibly, I'm wondering why you are printing the $uri and not doing anything with the $ua->get($uri) response. Additionally, you're using the Dumper keyword without bothering to use Data::Dumper, which means you didn't actually run this test snippet.

However, we can learn something from your code, now that it can be read. Let's remove the useragent portion. Maybe you've got problems there too (why are you using http protocol for oauth, for example), but if we focus just on getting the URI right, you can see there are problems:

#!/usr/bin/env perl use strict; use warnings; use URI; my $uri = URI->new('https://dev.preview.com/oauth2/default/v1/authoriz +e'); $uri->query_form( client_id => 'XXXXXXXXXXXXXXX', response_type => 'code', scope => 'openid', redirect_uri => 'http%3A%2F%2Ftestchandan.com%3A5001', state => '1234', nonce => 'UBGW' ); my $wanted = "https://dev.preview.com/oauth2/default/v1/authorize?clie +nt_id=XXXXXXXXXXXXXXX&response_type=code&scope=openid&redirect_uri=ht +tp%3A%2F%2Ftestchandan.com%3A5001&state=1234&nonce=UBGW"; print "WANTED: $wanted\n"; print "FORMED: $uri\n\n"; print $uri eq $wanted ? "<<perfect match!>>\n" : "<<they're different! +>>\n";

Any guesses what the output will be? Here:

WANTED: https://dev.preview.com/oauth2/default/v1/authorize?client_id= +XXXXXXXXXXXXXXX&response_type=code&scope=openid&redirect_uri=http%3A% +2F%2Ftestchandan.com%3A5001&state=1234&nonce=UBGW FORMED: https://dev.preview.com/oauth2/default/v1/authorize?client_id= +XXXXXXXXXXXXXXX&response_type=code&scope=openid&redirect_uri=http%253 +A%252F%252Ftestchandan.com%253A5001&state=1234&nonce=UBGW <<they're different!>>

Well, that's not a big surprise, now is it? So let's look at what is different. In part of your wanted query string, you see: "%3A%2F%2F". In part of your actually formed query string, you see "%253A%252F%252F". The source string that is producing that is redirect_uri   => 'http%3A%2F%2Ftestchandan.com%3A5001'. What do you think is happening here?

Additionally, we see this in your wanted string: "%3A5001", and this in our actually formed URL: "%253A5001". So what is "%3A%2F%2F"? That's "://". And what is "%3A5001"?, that's ":5001". The first one is part of the http://example.com syntax, and the second one is a port specification: example.com:5001. And why are they becoming something different? Because you're double encoding them. It is incorrect to pass this line: redirect_uri   => 'http%3A%2F%2Ftestchandan.com%3A5001'. Putting encoding into the redirect_uri is wrong, because the URI module already encodes for you. You should have passed redirect_uri   => 'http://testchandan.com:5001', and let URI encode that for you. The double encoding is creating mojibake. Let's fix that part of your code:

#!/usr/bin/env perl use strict; use warnings; use URI; my $uri = URI->new('https://dev.preview.com/oauth2/default/v1/authoriz +e'); $uri->query_form( client_id => 'XXXXXXXXXXXXXXX', response_type => 'code', scope => 'openid', redirect_uri => 'http://testchandan.com:5001', state => '1234', nonce => 'UBGW' ); my $wanted = "https://dev.preview.com/oauth2/default/v1/authorize?clie +nt_id=XXXXXXXXXXXXXXX&response_type=code&scope=openid&redirect_uri=ht +tp%3A%2F%2Ftestchandan.com%3A5001&state=1234&nonce=UBGW"; print "WANTED: $wanted\n"; print "FORMED: $uri\n\n"; print $uri eq $wanted ? "<<perfect match!>>\n" : "<<they're different! +>>\n";

And what output do you think we'll get now? Let's see:

WANTED: https://dev.preview.com/oauth2/default/v1/authorize?client_id= +XXXXXXXXXXXXXXX&response_type=code&scope=openid&redirect_uri=http%3A% +2F%2Ftestchandan.com%3A5001&state=1234&nonce=UBGW FORMED: https://dev.preview.com/oauth2/default/v1/authorize?client_id= +XXXXXXXXXXXXXXX&response_type=code&scope=openid&redirect_uri=http%3A% +2F%2Ftestchandan.com%3A5001&state=1234&nonce=UBGW <<perfect match!>>

Now I don't know if that's your only problem. It seems a little suspicious to not be using SSL for an oauth request, but I don't know how your SSL termination is set up so I can't be sure there. But double-encoding a URL should be pretty obvious if you just take the time to look at your "want" and verify that's what you are forming.

PLEASE, in the future, provide small, self-contained working examples that compile, and that you have run yourself first. And take the three minutes required to format your code legibly, so people can look at it and make sense of it without re-formatting it themselves. How much time do you think we owe you? If the answer is greater than zero you're wrong. Responding here is because we want to be helpful. But by posting incomplete, or overly verbose example code that doesn't compile, is poorly formatted, and that you haven't run yourself is simply an abuse of peoples' compulsion to be helpful.


Dave

Replies are listed 'Best First'.
Re^5: Need to resolve the API call query
by chandantul (Scribe) on Jun 05, 2021 at 15:46 UTC

    Thanks Dave for advices. You are true in your every point. I am encouraged now. Thanks for your help. I will post my questions properly henceforth

A reply falls below the community's threshold of quality. You may see it by logging in.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11133532]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2021-10-24 23:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (89 votes). Check out past polls.

    Notices?