Basic Authentication with sendHttpGet

Posted by ByronB on 22-Apr-2015 06:20

Hi, I am trying to integrate RB with the OneAll services held at www.oneall.com. They are a middleware to a lot of social media sites connecting with either OAuth 1 or OAuth 2. 

I want to use rbv_api.sendHttpGet to authenticate the user server side against the system and then do something with the returned JSON but I cant find a way to add basic authentication to the header?

Authenticating with your API Credentials

You have to use HTTP Basic Authentication to confirm your identity via the API. All requests must come over SSL/HTTPS, and have to be sent to the Site Domain

In java it would be something like:

// Setup connection
  URL url = new URL (resource_uri);
  HttpURLConnection connection = (HttpURLConnection) url.openConnection();
   
  // Connect using basic auth
  connection.setRequestMethod("GET");  
  connection.setRequestProperty("Authorization", "Basic " +  encoded_site_authentication);
  connection.setDoOutput(true);
  connection.setReadTimeout(10000);
  connection.connect();
  connection.getInputStream();
   
  StringBuilder sb = new StringBuilder();
  String line = null;

All Replies

Posted by Anoop Premachandran on 22-Apr-2015 06:29

You can use rbv_api.sendJSONRequest() API

Use method as GET and set Body as Null and pass username and password or directly use headers to set Basic Auth Header

http://documentation.progress.com/output/Rollbase/index.html#page/rb/rbv-api.sendjsonrequest().html

Posted by ByronB on 22-Apr-2015 06:48

Hi Anoop, thanks for your response. I have done the following:

var publicKey = '114696e7';
var privateKey = '701489da-';

var url = "https://subdomain.api.oneall.com/connections.json";
var data = null;
var method = "GET";
var contentType = "application/json; charset=UTF-8";
var username = "114696e7";
var password = "701489da-";
//var header = "Authorization", "Basic " +  encodedSiteAuth);

var request = rbv_api.sendJSONRequest(url, data, method, contentType, username, password);

rbv_api.println(request);

And when i go to debug I get this:

Error Can't find method com.rb.core.services.api.ServerSideAPI.sendJSONRequest(string,null,string,string,string,string). (line #14) in formula:

Posted by murali on 22-Apr-2015 06:57

var request = rbv_api.sendJSONRequest(url, data, method, contentType, username, password, null);
 
Please pass null for headers. 
 
 
[collapse]
From: ByronB [mailto:bounce-ByronB@community.progress.com]
Sent: Wednesday, April 22, 2015 7:50 AM
To: TU.Rollbase@community.progress.com
Subject: RE: [Technical Users - Rollbase] Basic Authentication with sendHttpGet
 
Reply by ByronB

Hi Anoop, thanks for your response. I have done the following:

var publicKey = '114696e7';
var privateKey = '701489da-';
 
var url = "https://subdomain.api.oneall.com/connections.json";
var data = null;
var method = "GET";
var contentType = "application/json; charset=UTF-8";
var username = "114696e7-832d-42d2-8003-8a07e0a25582";
var password = "701489da-532f-4e6f-a68b-23a48f70e9a2";
//var header = "Authorization", "Basic " +  encodedSiteAuth);
 
var request = rbv_api.sendJSONRequest(url, data, method, contentType, username, password);
 
rbv_api.println(request);

 

And when i go to debug I get this:

Error Can't find method com.rb.core.services.api.ServerSideAPI.sendJSONRequest(string,null,string,string,string,string). (line #14) in formula:

Stop receiving emails on this subject.

Flag this post as spam/abuse.

[/collapse]

Posted by ByronB on 22-Apr-2015 07:32

Thanks Murali, I have the following code using a base64 encode function:

var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  
function encode64(input) {
     input = escape(input);
     var output = "";
     var chr1, chr2, chr3 = "";
     var enc1, enc2, enc3, enc4 = "";
     var i = 0;
     do {
        chr1 = input.charCodeAt(i++);
        chr2 = input.charCodeAt(i++);
        chr3 = input.charCodeAt(i++);
		
        enc1 = chr1 >> 2;
        enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
        enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
        enc4 = chr3 & 63;
        if (isNaN(chr2)) {
           enc3 = enc4 = 64;
        } else if (isNaN(chr3)) {
           enc4 = 64;
        }
        output = output +
           keyStr.charAt(enc1) +
           keyStr.charAt(enc2) +
           keyStr.charAt(enc3) +
           keyStr.charAt(enc4);
        chr1 = chr2 = chr3 = "";
        enc1 = enc2 = enc3 = enc4 = "";
     } while (i < input.length);
     return output;
  }

var publicKey = '114696e7';
var privateKey = '701489da-';

var siteAuth = publicKey + ":" + privateKey;

//var encodedSiteAuth = encode64(siteAuth);
var encodedSiteAuth = 'MTE0Njk2ZTctODMyZC00MmQyLTgwMDMtOGEwN2UwYTI1NTgyOjcwMTQ4OWRhLTUzMmYtNGU2Zi1hNjhiLTIzYTQ4ZjcwZTlhMg==';


var url = "https://orbis.api.oneall.com/connections.json";
var data = null;
var method = "GET";
var contentType = "application/json; charset=UTF-8";
var header = "Authorization: Basic " +  encodedSiteAuth;

var request = rbv_api.sendJSONRequest(url, null, method, contentType, null, null, header);

rbv_api.println(request);

I am getting the error:
Error Wrapped java.lang.NullPointerException (line #52) in formula:

If I null the header and add the username and password I get an unauthorised error

Posted by murali on 22-Apr-2015 07:55

Hi Byron,
                Headers is supposed to be a Map.  Please pass a json object.
var header = { "Authorization”   :  “Basic " +  encodedSiteAuth }
 
rbv_api.sendJSONRequest()
 
Purpose
Sends a JSON request to the specified URL and returns the HTTP response body.
Syntax
rbv_api.sendJSONRequest(url, data, method, contentType, username,
password, headers)
Parameters
url
URL to send an HTTP request
data
Text string or a JSON object to send as a body of the request
method
HTTP method, one of GET (default), POST, PUT or DELETE
contentType
MIME content type of data. For example:
application/json; charset=UTF-8 (default)
username
Login name for BASIC authentication, by default null
password
Password for BASIC authentication, by default null
headers
Optional array of HTTP headers in name/value pairs, by default null
 
 
 
 
[collapse]
From: ByronB [mailto:bounce-ByronB@community.progress.com]
Sent: Wednesday, April 22, 2015 8:33 AM
To: TU.Rollbase@community.progress.com
Subject: RE: [Technical Users - Rollbase] Basic Authentication with sendHttpGet
 
Reply by ByronB

Thanks Murali, I have the following code using a base64 encode function:

var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  
function encode64(input) {
     input = escape(input);
     var output = "";
     var chr1, chr2, chr3 = "";
     var enc1, enc2, enc3, enc4 = "";
     var i = 0;
     do {
        chr1 = input.charCodeAt(i++);
        chr2 = input.charCodeAt(i++);
        chr3 = input.charCodeAt(i++);
               
        enc1 = chr1 >> 2;
        enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
        enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
        enc4 = chr3 & 63;
        if (isNaN(chr2)) {
           enc3 = enc4 = 64;
        } else if (isNaN(chr3)) {
           enc4 = 64;
        }
        output = output +
           keyStr.charAt(enc1) +
           keyStr.charAt(enc2) +
           keyStr.charAt(enc3) +
           keyStr.charAt(enc4);
        chr1 = chr2 = chr3 = "";
        enc1 = enc2 = enc3 = enc4 = "";
     } while (i < input.length);
     return output;
  }
 
var publicKey = '114696e7';
var privateKey = '701489da-';
 
var siteAuth = publicKey + ":" + privateKey;
 
//var encodedSiteAuth = encode64(siteAuth);
var encodedSiteAuth = 'MTE0Njk2ZTctODMyZC00MmQyLTgwMDMtOGEwN2UwYTI1NTgyOjcwMTQ4OWRhLTUzMmYtNGU2Zi1hNjhiLTIzYTQ4ZjcwZTlhMg==';
 
 
var url = "https://orbis.api.oneall.com/connections.json";
var data = null;
var method = "GET";
var contentType = "application/json; charset=UTF-8";
var header = "Authorization: Basic " +  encodedSiteAuth;
 
var request = rbv_api.sendJSONRequest(url, null, method, contentType, null, null, header);
 
rbv_api.println(request);

I am getting the error:
Error Wrapped java.lang.NullPointerException (line #52) in formula:

If I null the header and add the username and password I get an unauthorised error
Stop receiving emails on this subject.

Flag this post as spam/abuse.

[/collapse]

Posted by Anoop Premachandran on 22-Apr-2015 07:56

You should pass username and password un-encoded and header as null

var url = "orbis.api.oneall.com/connections.json";

var data = null;

var method = "GET";

var contentType = "application/json; charset=UTF-8";

var request = rbv_api.sendJSONRequest(url, null, method, contentType, <username>, <password>, null);

Posted by ByronB on 22-Apr-2015 08:12

Thanks Murali, worked a charm

Anoop, the creds needed to be passed in the header :-)

Posted by kanav20011997 on 22-Dec-2016 12:52

var publicKey = 'guest';

var privateKey = 'guest';

var url = "localhost:15672/

var data = null;

var method = "GET";

var contentType = "application/json; charset=UTF-8";

var username = "guest";

var password = "guest";

var header = { "Authorization”   :  “Basic " + Z3Vlc3Q6Z3Vlc3Q=} ;

var request = rbv_api.sendJSONRequest(url, data, method, contentType, username,     password,header);

rbv_api.println(request);

Error missing : after property id (line #11) in formula:

This is the error that i'm getting

Posted by jquerijero on 22-Dec-2016 13:20

Is Z3Vlc3Q6Z3Vlc3Q= a variable or a string?

Posted by kanav20011997 on 23-Dec-2016 00:32

002 var publicKey = 'guest';

003 var privateKey = 'guest';

004 var siteAuth = publicKey + ":" + privateKey ;

005 var encodedSiteAuth = 'Z3Vlc3Q6Z3Vlc3Q=';

006 var url ="localhost:15672/.../publish"

007  var data = "null";

008  var method = "POST";

009 var contentType = "application/json; charset=UTF-8";

010 var username = "guest";

011  var password = "guest";

012 var header = { "Authorization”   :  “Basic " +  encodedSiteAuth };

013 var request = rbv_api.sendJSONRequest(url, data, method, contentType, username, password);

014 rbv_api.println(request);

Error missing : after property id (line #13) in formula:

Posted by mpiscoso@gmail.com on 23-Dec-2016 02:32

Make your username and password null since you are using basic auth.

Then add the header parameter to the sendjsonrequest call.

Hope this helps

Posted by kanav20011997 on 23-Dec-2016 04:38

001

002 var publicKey = 'guest';

003 var privateKey = 'guest';

004 var siteAuth = publicKey + ":" + privateKey ;

005 var encodedSiteAuth = 'Z3Vlc3Q6Z3Vlc3Q=';

006 var url ="localhost:15672/.../publish";

007  var data = null;

008  var method = "POST";

009 var contentType = "application/json; charset=UTF-8";

010 var username = null;

011 var password = null;

012 var header = { "Authorization”   :  “Basic " +  encodedSiteAuth };

013 var result = rbv_api.sendJSONRequest(url,data,method,contentType,username,password,headers);

Error missing : after property id (line #13) in formula

Posted by kanav20011997 on 23-Dec-2016 04:39

Still Getting the same error - after making the mentioned changes.

Posted by mpiscoso@gmail.com on 23-Dec-2016 05:56

That's weird.

Instead of null try empty strings ''

Also, are you performing this in the public cloud? Or a private cloud?

Posted by kanav20011997 on 23-Dec-2016 09:15

Thanks for your help. I have now made a public rabbitMQ instance available for troubleshooting.

The code that i am using now:

var url ="ec2-54-172-14-24.compute-1.amazonaws.com:15672/.../publish";

var data = {"routing_key":"queue_test",

"payload":"message test ",

"payload_encoding":"string"};

var method = "POST";

var contentType = "application/json; charset=UTF-8";

var username = "guest";

var password = "guest";

var encodedSiteAuth = "Z3Vlc3Q6Z3Vlc3Q=";

var headers ={ "Authorization"   :  "Basic " +  encodedSiteAuth };

var headers ="";

var result = rbv_api.sendJSONRequest(url,data,method,contentType,username,password,headers);

015 rbv_api.println(result);

However, I am still getting the following error:

HTTP Error 400: HTTP call failed: Bad Request (line #15)

What do you think?

Posted by mpiscoso@gmail.com on 23-Dec-2016 11:51

Your url needs http or https

Then try using headers (basic auth) and null username and pass.

Hope this helps

Posted by kanav20011997 on 24-Dec-2016 07:27

Thanks for the help again, I've tried what you have mentioned but I'm getting the same error -

HTTP Error 400: HTTP call failed: Bad Request (line #15)

This thread is closed