PushBullet and DroidScript

200 views
Skip to first unread message

Eric Geer

unread,
Nov 23, 2016, 8:16:47 PM11/23/16
to DroidScript
Since I've been struggling to get email to function reliably, I'm looking into alternate methods.  PushBullet has an open API, but I am not seeing how to send the appropriate auth information via the API key I have squired.  If I use the following from a linux command line, I get the message:

curl -k -o /dev/null -s -H "Content-Type: application/json" -u "MY_API_KEY": -d "{\"type\": \"note\", \"title\": \"This is a test\"}" https://api.pushbullet.com/v2/pushes

Here is the code I'm trying to use:

var httpRequest = new XMLHttpRequest();
var token = 'MY_API_KEY';                                                                   //This is the key I received from PushBullet
var url = "https://api.pushbullet.com/v2/pushes";                                   //URL to send notes
var message = "{\"type\": \"note\", \"title\": \"This is a test\"}";                //Message to send as type Note
function OnStart()
{
    httpRequest.onreadystatechange = function() { HandleReply(httpRequest); };
    httpRequest.open("POST", url, true);
    httpRequest.setRequestHeader( 'Content-Type', 'application/json' );
    httpRequest.setRequestHeader( 'Authorization', token );
    httpRequest.setRequestHeader( 'DATA', message );
    httpRequest.send();
}

//Handle the servers reply (a json object).
function HandleReply( httpRequest )
{
    if( httpRequest.readyState==4 )
    {
        //If we got a valid response.
        if( httpRequest.status==200 )  console.log( "Response: " + httpRequest.responseText);
        //An error occurred
        else console.log( "Error: " + httpRequest.status + httpRequest.responseText);
    }
}

Eric Geer

unread,
Nov 25, 2016, 3:59:26 AM11/25/16
to DroidScript
Has anyone sent Authentication and dat over setRequestHeader before?

Eric Geer

unread,
Nov 26, 2016, 4:20:24 AM11/26/16
to DroidScript
Progress.  So I am no longer getting authentication failure, but it appears my message won't post due to an error.  Here's my debug output:

  • App.GetModel( )
  • -> Nexus 7
  • Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects t...
  • Response: {"accounts":[],"blocks":[],"channels":[],"chats":[],"clients":[],"contacts":[],"device...
and the updated code:

var httpRequest = new XMLHttpRequest();
var token = "YOUR_KEY_HERE";
var message = { type: "note", title: "Note title", body: "Note Body" };

function OnStart()
{
    httpRequest.onreadystatechange = function() { HandleReply(httpRequest); };
    httpRequest.open( "GET", url, false );
    httpRequest.setRequestHeader( "Authorization", "Bearer " + token );
    httpRequest.send( JSON.stringify( message ) );

}

//Handle the servers reply (a json object).
function HandleReply( httpRequest )
{
    if( httpRequest.readyState==4 )
    {
        //If we got a valid response.
        if( httpRequest.status==200 )  console.log( "Response: " + httpRequest.responseText);
        //An error occurred
        else console.log( "Error: " + httpRequest.status + httpRequest.responseText);
    }
}

Anyone know what would cause that error?

Steve Garman

unread,
Nov 26, 2016, 4:26:55 AM11/26/16
to DroidScript

Eric Geer

unread,
Nov 26, 2016, 4:47:27 AM11/26/16
to DroidScript
Thanks, not familiar with this method though.  How would I input the authentication and headers as are posted in the above code?

Steve Garman

unread,
Nov 26, 2016, 7:12:01 AM11/26/16
to DroidScript
Presumably it should be something like

app.HttpRequest( "get",
"https://api.pushbullet.com",
"/v2/pushes",
JSON.stringify( message ),
HandleReply,
"Authorization=Bearer " + token );

though I don't know for sure whether the params need to be a JSON string or something more like

"type=note,title=Note%20title,body=Note%20Body"

Eric Geer

unread,
Nov 27, 2016, 3:44:57 AM11/27/16
to DroidScript
it didn't throw an error, but the message didn't post either. No response from the HandleReply function either. Here's what I've tried:

//var message = {
// type: "note",
// title: "Note title",
// body: "Note Body"
//}
var message = "{type=note,title=Note%20title,body=Note%20Body}"
//var message = "type:%20note,%20title:%20test%20body:%20body"
var token = "MY_TOKEN";
var xhr = new XMLHttpRequest();

app.HttpRequest( "post",


"https://api.pushbullet.com",
"/v2/pushes",
JSON.stringify( message ),
HandleReply,
"Authorization=Bearer " + token );

function HandleReply( httpRequest )

Eric Geer

unread,
Nov 29, 2016, 12:02:45 AM11/29/16
to DroidScript
Here is how I finally got it working:


var httpRequest = new XMLHttpRequest();
var token = "YOUR_API_KEY_HERE";
var message = { type: "note", title: "Note title", body: "Note Body" };
i = 0;


function OnStart()
{
    httpRequest.onreadystatechange = function() { HandleReply(httpRequest); };
    httpRequest.open( "POST", url, true );

    httpRequest.setRequestHeader( "Authorization", "Bearer " + token );
    httpRequest.setRequestHeader( "Content-Type", "application/json" );
    httpRequest.send( JSON.stringify( message ) );
}

//Handle the servers reply (a json object).
function HandleReply( httpRequest )
{
    if( httpRequest.readyState==4 )
    {
        //If we got a valid response.
        if( httpRequest.status==200 ) {
            var str = httpRequest.responseText;

            //Strip out characters to form the array
            while ( str.match( /":\[\],"/ ) ) var str = str.replace(/":\[\],"/, '\",\"');
            while ( str.match( /{/ ) ) var str = str.replace(/{/, '');
            while ( str.match( /}/ ) ) var str = str.replace(/}/, '');
            while ( str.match( /:\[/ ) ) var str = str.replace(/:\[/, ',');
           
            //Form the array
            var array = str.split( "," );
            counts = array.length;       //Get length of array to know how many to log to output
            while ( counts > i ) {          //Log output for each array number
                console.log( array[i] );
                i++
            }
        }
        else {
            var str = httpRequest.responseText;
            while ( str.match( /{/ ) ) var str = str.replace(/{/, '');
            while ( str.match( /}/ ) ) var str = str.replace(/}/, '');
            var array = str.split( "," );
            counts = array.length;
            while ( counts > i ) {
                console.log( array[i] );
                i++
            }
        }
    }
}

Eric Geer

unread,
Nov 30, 2016, 6:49:02 PM11/30/16
to DroidScript
I forgot to mention, you can easily change this from a note push to a note read by changing the following:


httpRequest.open("POST", url, true);

to

httpRequest.open("GET", url, true);
Reply all
Reply to author
Forward
0 new messages