Hey! I’m having what I think is a noob issue regarding authentication with a web services smart app.
I’m able to authenticate my extremely basic web app and i’m redirected back to my app where I receive my bearer ID and the access url. However when I send a get request to the /switches endpoint i’m receiving a 401 error “invalid_token”
I used this node.js example https://github.com/schettj/SmartThings which was linked in the ST documentation to retrieve the url and access token.
Here’s what my GET request looks like when making the request to /switches endpoint:
request({
url: 'https://graph.api.smartthings.com//api/smartapps/installations/ACCESS_URL/switches', //URL to hit
method: 'GET', //Specify the method
headers: {
'Authorization: Bearer': 'ACCESS_TOKEN',
'redirect_uri': 'http://127.0.0.1:3000/callback',
'client_id': 'MY_CLIENT_ID',
'client_secret': 'MY_CLIENT_SECRET'
}
}, function(error, response, body){
if(error) {
console.log(error);
} else {
console.log( request.headers, response.statusCode, body);
}
});`
Here is what the auth request looks like
oauth2.authCode.getToken({
code: code,
redirect_uri: 'http://127.0.0.1:3000/callback'
}, saveToken);
function saveToken(error, result) {
if (error) { console.log('Access Token Error', error.message); }
// result.access_token is the token, get the endpoint
var bearer = result.access_token
var sendreq = { method: "GET", uri: endpoints_uri + "?access_token=" + result.access_token };
request(sendreq, function (err, res1, body) {
var endpoints = JSON.parse(body);
// we just show the final access URL and Bearer code
var access_url = endpoints[0].url
res.send('<pre>https://graph.api.smartthings.com/' + access_url + '</pre><br><pre>Bearer ' + bearer + '</pre>');
});
}
Any ideas on what could be the issue?