Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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


In reply to Re^4: Need to resolve the API call query by davido
in thread Need to resolve the API call query by chandantul

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2021-11-28 23:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?