Help: Bearer Authentication

Posted on
Fri Nov 06, 2020 4:33 pm
roquej offline
User avatar
Posts: 609
Joined: Jan 04, 2015
Location: South Florida, USA

Help: Bearer Authentication

I am trying to get GhostXML working with Bearer Authentication, but a bit lost. Please, know I am not a programmer, just a tinkerer.

To test the issue, I wrote this piece of code, which kind of works

********
Code: Select all
import simplejson as json
import urllib2
from base64 import b64encode
import datetime

#import value from indigo variable
token =  indigo.variables["AWAir_token"].value
AWAir_Family_deviceID =  indigo.variables["AWAir_Family_deviceID"].value
 
url = "https://developer-apis.awair.is/v1/users/self/devices/awair/" + AWAir_Family_deviceID + "/air-data/latest?fahrenheit=true"

#get DEVICE Data in json from api url
request = urllib2.Request(url)
request.add_header('Authorization', 'Bearer ' + token)

#get TOKEN in json from api url
try:
   urlReturned = urllib2.urlopen(request)
except:
   indigo.variable.updateValue("awair_family_status", value="Could not retreive Family data")
   indigo.server.log ("awair_family_status = " + "Could not retrive Family")
   exit(0)

#format api returned data
data_getStatus = urlReturned.read()

#interpret json
try:
   dict = json.loads(data_getStatus)
except:
   indigo.variable.updateValue("awair_family_status", value="error json.loads(data_getStatus)")
   indigo.server.log ("awair data_getStatus = " + "error json.loads(data_getStatus)")
   exit(0)      

#temperature
try:
   awair_family_0_temp=dict[u'data'][0][u'sensors'][0][u'value']
except:
   indigo.variable.updateValue("awair_family_status", value="error parcing TEMPERATURE")
   indigo.server.log ("awair_family_0_temp = " + "error parcing TEMPERATURE")
   exit(0)

awair_family_0_temp=str(round(awair_family_0_temp,1))

*******
GhostXML using "Token" is asking for "token URL" and "token password", for which I am not sure what to use.

Thank you in advance.

JP

Posted on
Fri Nov 06, 2020 7:33 pm
DaveL17 offline
User avatar
Posts: 6753
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Help: Bearer Authentication

I don't think that the token auth option will work for you. A quick Google shows that Bearer authentication requires a different token payload than we're using. That said, adding it into the plugin shouldn't be a huge lift.

I don't have a good way to test the changes so I will need some help with testing any new features.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Fri Nov 06, 2020 8:01 pm
roquej offline
User avatar
Posts: 609
Joined: Jan 04, 2015
Location: South Florida, USA

Re: Help: Bearer Authentication

Anything to help. Just point the way!


Sent from my iPad using Tapatalk Pro

Posted on
Sat Nov 07, 2020 9:20 am
DaveL17 offline
User avatar
Posts: 6753
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Help: Bearer Authentication

This may not be exactly right, but it should be close. This is the first part of your script rewritten to follow the format of the plugin structure. Please run the script and post (or DM) the results.

Code: Select all
#! /usr/bin/env python
# -*- coding: utf-8 -*-

import subprocess
import json

token = indigo.variables["AWAir_token"].value
glob_off = ''

AWAir_Family_deviceID =  indigo.variables["AWAir_Family_deviceID"].value
url = "https://developer-apis.awair.is/v1/users/self/devices/awair/" + AWAir_Family_deviceID + "/air-data/latest?fahrenheit=true"

curl_arg = 'curl -vskX' + glob_off + ' GET ' + url + ' -H "accept: application/json" -H "Authorization: Bearer "' + token

proc = subprocess.Popen(curl_arg, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)

reply_in = proc.communicate()
reply    = json.loads(reply_in[0])

indigo.server.log(u"{0}".format(reply))

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Sat Nov 07, 2020 9:27 am
roquej offline
User avatar
Posts: 609
Joined: Jan 04, 2015
Location: South Florida, USA

Re: Help: Bearer Authentication

Here you go:

Nov 7, 2020 at 10:26:38 AM
Script {u'data': [{u'indices': [{u'comp': u'dust', u'value': 4.0}, {u'comp': u'humid', u'value': 1.0}, {u'comp': u'co2', u'value': 0.0}, {u'comp': u'temp', u'value': 0.0}, {u'comp': u'voc', u'value': 1.0}], u'timestamp': u'2020-11-07T15:26:34.000Z', u'sensors': [{u'comp': u'humid', u'value': 53.310001373291016}, {u'comp': u'temp', u'value': 78.0980010986328}, {u'comp': u'voc', u'value': 397.0}, {u'comp': u'dust', u'value': 75.30000305175781}, {u'comp': u'co2', u'value': 661.0}], u'score': 70.0}]}

Posted on
Sat Nov 07, 2020 9:38 am
DaveL17 offline
User avatar
Posts: 6753
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Help: Bearer Authentication

That's awesome. I think we're going to be able to make it work. I'll be in touch. :D

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Sat Nov 07, 2020 9:59 am
roquej offline
User avatar
Posts: 609
Joined: Jan 04, 2015
Location: South Florida, USA

Re: Help: Bearer Authentication




Sent from my iPhone using Tapatalk Pro

Posted on
Sat Nov 07, 2020 10:01 am
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Help: Bearer Authentication

Super easy if you're using the requests lib:

Code: Select all
headers = {"Authorization": "Bearer {}".format(token), "accept": "application/json"}
reply = requests.get(url, headers=headers)

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sat Nov 07, 2020 1:22 pm
DaveL17 offline
User avatar
Posts: 6753
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Help: Bearer Authentication

Thanks Jay. We're using curl for all the calls. I don't remember why we chose curl over requests, but I know we had a super solid reason. :D

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Sat Nov 07, 2020 2:10 pm
DaveL17 offline
User avatar
Posts: 6753
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Help: Bearer Authentication

This Pre-Release of the GhostXML plugin (v0.5.0.03) includes the first crack at bearer authentication. To use it, create a new device and select 'Bearer' as the authentication method and enter the token value in the appropriate field.

For your specific construction (getting a device id out of an Indigo variable), select 'Use Substitutions' and enter the variable ID in the appropriate field. With substitutions, your source URL would be something in the neighborhood of:
Code: Select all
https://developer-apis.awair.is/v1/users/self/devices/awair/[1]/air-data/latest?fahrenheit=true

The revisions are tested against an authentication response service but obviously not for your specific service.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Sat Nov 07, 2020 7:08 pm
roquej offline
User avatar
Posts: 609
Joined: Jan 04, 2015
Location: South Florida, USA

Re: Help: Bearer Authentication

Dave,

First, you are awesome!! Second, can’t get it to work:

Using “https://developer-apis.awair.is/v1/users/self/devices/awair/4832/air-data/latest?fahrenheit=true” - the correct info for the URL, which the same in the code but config dialog has an error.

Any suggestions?

JP

Posted on
Sat Nov 07, 2020 7:21 pm
DaveL17 offline
User avatar
Posts: 6753
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Help: Bearer Authentication

roquej wrote:
Dave,

First, you are awesome!! Second, can’t get it to work:

Using “https://developer-apis.awair.is/v1/users/self/devices/awair/4832/air-data/latest?fahrenheit=true” - the correct info for the URL, which the same in the code but config dialog has an error.

Any suggestions?

JP

What is the config dialog error? I entered your info in a device and didn't get an error.

Things to try: ensure that you don't have any extra spaces in the field and check to see if you may have accidentally entered the url in more than once into the field (happens to me all the time).

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Sat Nov 07, 2020 7:42 pm
roquej offline
User avatar
Posts: 609
Joined: Jan 04, 2015
Location: South Florida, USA

Re: Help: Bearer Authentication

Never mind, I was using my iPad to connect to the mac-min and the iPad seem to have caused issues with the copy and paste. Switch to the Macbook Pro and all good now.

AND it works great. Thank you!

You rock!

Posted on
Sat Nov 07, 2020 8:10 pm
roquej offline
User avatar
Posts: 609
Joined: Jan 04, 2015
Location: South Florida, USA

Re: Help: Bearer Authentication

Ok - another quick question. Out of the values provided, by the JSON, they move...is this normal? What I man is that index 0, sometimes is temp, but later is co2, and later is dust. Is that normal behavior?

If it is, what would be the best way to always find temperature, even if it changes index position?

Thank you in advance.

JP

Posted on
Sat Nov 07, 2020 8:34 pm
DaveL17 offline
User avatar
Posts: 6753
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Help: Bearer Authentication

Yes, that's normal behavior and one of the things that the plugin is meant to handle. Without getting super technical, the incoming json includes data structures that can change (mutable) and depending on the source -- varying. That's part of the reason for the Ghost part. :D

What do the custom states for the device look like?

Screen Shot 2020-11-07 at 8.32.43 PM.png
Screen Shot 2020-11-07 at 8.32.43 PM.png (90.24 KiB) Viewed 3359 times

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Who is online

Users browsing this forum: No registered users and 3 guests