Web Services - Invalid Token

webservices

(Jonathan Rivera) #1

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?


(Tim Slagle) #2

I am wondering if the double “slash” between the uri and path might be messing with things.


(Jonathan Rivera) #3

Hey @slagle!
Thanks for the help. I think that was part of the issue! I did have to tweak the Node.js repo code that was linked to in the documentation some as well to get it working. The code was missing required (according to the docs) info like “grant_type” when getting the token and ‘response_type’. I’m assuming there were changes to the auth process since the repo was created?

After fixing all that it worked and gave me authorization and i’m able to see my switches and their statuses.

I’d be happy to post a simple repo to help others get going with node. It’s not perfect but it’ll work.


(Tony Richards) #4

Hi @jrivera. Do you mind posting a sample of what you tweaked? I’m having very similar issues. Thanks for any help.


(Jonathan Rivera) #5

What issues are you having?

If you can post some of your errors and your code I’ll see if I can point out any errors. Unfortunately I can’t post my code and I don’t have time to spin up a sample server with working code.


(Sahil Arora) #6

Hello ,

I have been trying this for a long time could you help me here , I would like to reach out to end point /switch
i have coded a script if i hit endpoint with /switchon and /switchoff hence i want to use those end point in my native app button so that when user turn on the switch in my application it should turn on or off the switch but every time i am getting a 401 error

It will be great if you could help me