Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^15: How can i capture the site_id in Dancer 2 app?

by 1nickt (Abbot)
on Jun 18, 2021 at 11:31 UTC ( #11133998=note: print w/replies, xml ) Need Help??


in reply to Re^14: How can i capture the site_id in Dancer 2 app?
in thread How can i capture the site_id in Dancer 2 app?

return html_page("Error: " . $access_token->to_string) if $access_token->{error};

You have not written a subroutine named html_page ... (you probably don't need one as you can use the Dancer2 page rendering or error page templates. But you cannot call a sub that does not exist in your code!)

Try printing out $access_token->{error} and see what the issue is.


The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^16: How can i capture the site_id in Dancer 2 app?
by chandantul (Scribe) on Jun 18, 2021 at 13:50 UTC

    Hi Master, I was able to fix the issue and able to generate the access token successfully

    Please check the code in webapp.pm

    #!/usr/bin/env perl use strict; use warnings; use FindBin; use lib "$FindBin::Bin/../lib"; use webapp; use Net::OAuth2::Client; use Dancer2; use URI; use HTML::Entities; use Data::Dumper; get '/:site_id' => sub { my $id = route_parameters->get('site_id'); redirect client(params->{$id})->authorize; }; get '/got/:site_id' => sub { my $p = params->{code}; #template 'query2' => { 'title' => 'webapp' }; defined params->{code} or print "Error: Missing access code"; my $id = route_parameters->get('site_id'); my $site_id = params->{$id}; #my $site_id = params->{site_id}; my $access_token = client($site_id)->get_access_token(params->{cod +e}); print "Error: " . $access_token->to_string if $access_token->{err +or}; my $accestk = $access_token->to_string; my $content = "<h2>Access token retrieved successfully!</h2>\n +" . '<p>'.encode_entities($access_token->to_string)."</p +>\n"; #template 'query2' => { 'title' => 'webapp' }; $content =~ s[\n][<br/>\n]g; print "My content: $content" . "\n"; }; sub client ($){ my $id = route_parameters->get('site_id'); my $site_id = $id; #my $site_config = config->{sites}{$site_id} || {}; my $redirect = uri_for("/got/$site_id"); Net::OAuth2::Profile::WebServer->new( client_id => 'xxxxxxxxxxxxxxxxxxxxx', client_secret => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx +xx', authorize_url => 'https://dev.oktapreview.com/oauth2/ +v1/authorize', access_token_url => 'https://dev.oktapreview.com/oauth2/v1/token +', response_type => 'code', grant_type => 'authorization_code', scope => 'openid', state => '1234', redirect_uri => $redirect ); }

    Thank you for helping me hope this code snippet will help others. Its not the best way but , yes we can achieve it. Please try to modify the source Webserver.pm as per your requirement.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2021-10-23 21:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (88 votes). Check out past polls.

    Notices?