Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Getting an Access Token

by tangent (Parson)
on Mar 05, 2022 at 00:37 UTC ( [id://11141844]=note: print w/replies, xml ) Need Help??


in reply to Solved: Getting an Access Token

In Postman you should be able to see the details of your request in the Postman console - see this page. If you can copy those details (obscuring the credential values) and post them here it should be easy enough for us to help.

Replies are listed 'Best First'.
Re^2: Getting an Access Token
by PerlMonger79 (Sexton) on Mar 05, 2022 at 07:43 UTC
    Hi. When I open the postman application and request a token manually, I get the following in the console.
    POST https://xxx.xx.xx.xx/api/v2/access/token 200 62 ms Warning: Self signed certificate POST /api/v2/access/token HTTP/1.1 Content-Type: application/x-www-form-urlencoded Authorization: Basic bGJLN0dLaGNmVTM2MkxNbjpzOUtKRXR0UVBHeGIyTkxkcEpMM +3hHbmlLN0MwaUk= User-Agent: PostmanRuntime/7.29.0 Accept: */* Cache-Control: no-cache Postman-Token: 1979ab3e-0607-4544-a239-05d37cb2dd87 Host: 172.20.19.21 Accept-Encoding: gzip, deflate, br Connection: keep-alive Content-Length: 29 grant_type=client_credentials HTTP/1.1 200 OK Server: nginx Date: Sat, 05 Mar 2022 07:31:30 GMT Content-Type: application/json; charset=utf-8 Transfer-Encoding: chunked Connection: keep-alive ETag: W/"63-lVO9g4PVOvVMxmtOQ57cFQLhFck" Strict-Transport-Security: max-age=31536000; includeSubDomains Content-Encoding: gzip {"access_token":"16f7a07cc25b2cf3618b8286180eb264744e145f","expires_in +":3600,"token_type":"bearer"}
    I'm trying to use the following perl script to request the token. But I get a Bad Request error.
    #!/usr/bin/perl -w use strict; use warnings; use LWP::UserAgent; my $netloc = "xxx.xx.xx.xx"; my $realm = "Realm"; my $user = "usercode"; my $pass = "passcode"; my $ua = LWP::UserAgent->new(); $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME}=0; $ua->default_header( "Authorization" => "Basic bGJLN0dLaGNmVTM2MkxNbjp +zOUtKRXR0UVBHeGIyTkxkcEpMM3hHbmlLN0MwaUk="); $ua->default_header( "Accept" => "*/*" ); $ua->default_header( "Content-Type" => "application/x-www-form-urlenco +ded"); $ua->credentials( $netloc, $realm, $user, $pass); $ua->ssl_opts( verify_hostname => 0, SSL_verify_mode => 0x00); my $query = "https://".$netloc."/api/v2/access/token"; my $req = HTTP::Request->new( POST => $query); my $resp = $ua->request($req); if ($resp->is_success) { my $output = $resp->decoded_content; print $output."\n"; } else { print $resp->as_string(); }
    I get this reponse...
    [user@localhost wifi]$ ./wifi-api-test-token.pl HTTP/1.1 400 Bad Request Connection: close Date: Sat, 05 Mar 2022 07:34:43 GMT ETag: W/"1b-WJVp2bnCdBNm40JHvp3t41llPJs" Server: nginx Content-Length: 27 Content-Type: application/json; charset=utf-8 Client-Date: Sat, 05 Mar 2022 07:35:10 GMT Client-Peer: 172.20.19.21:443 Client-Response-Num: 1 Client-SSL-Cert-Issuer: /O=Cambium Networks Ltd. Client-SSL-Cert-Subject: /O=Cambium Networks Ltd. Client-SSL-Cipher: ECDHE-RSA-AES128-GCM-SHA256 Client-SSL-Socket-Class: IO::Socket::SSL Client-SSL-Version: TLSv1_2 Client-SSL-Warning: Peer certificate not verified Strict-Transport-Security: max-age=31536000; includeSubDomains {"error":"invalid_request"}
      OK guys, it's ok I figured it out through viewing several other script examples. So this is my final script.
      #!/usr/bin/perl -w use strict; use warnings; use LWP::UserAgent; my $netloc = "xxx.xx.xx.xx"; my $realm = "Realm"; my $user = "usercode"; my $pass = "passcode"; my $ua = LWP::UserAgent->new(); $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME}=0; $ua->default_header( "Authorization" => "Basic bGJLN0dLaGNmVTM2MkxNbjp +zOUtKRXR0UVBHeGIyTkxkcEpMM3hHbmlLN0MwaUk="); $ua->default_header( "Accept" => "*/*" ); $ua->default_header( "Content-Type" => "application/x-www-form-urlenco +ded"); $ua->credentials( $netloc, $realm, $user, $pass); $ua->ssl_opts( verify_hostname => 0, SSL_verify_mode => 0x00); my $url = "https://".$netloc."/api/v2/access/token"; my $resp = $ua->post($url, Content => ['grant_type' => "client_creden +tials"]); if ($resp->is_success) { my $output = $resp->decoded_content; print $output."\n"; } else { print $resp->as_string(); }
      And I now get the following output.
      {"access_token":"ea1dda77710f64b0db328415427a1463e74ea204","expires_in +":3600,"token_type":"bearer"}
      Thank you for taking time and steering me in the right direction. That postman console really helped me a lot. Thank you very much.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-25 16:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found