node-toggl-api icon indicating copy to clipboard operation
node-toggl-api copied to clipboard

Get my user ID?

Open darkpixel opened this issue 9 years ago • 4 comments

If I use curl to authenticate using an API token to the Toggl API, it returns a blob of JSON that includes my user ID.

If I authenticate using node-toggl-api and a username and password and call the authenticate() method, I get a blob of data back with my user ID.

But if I call authenticate() when using an API Token, I get an error Error: No need to authenticate thus you use apiToken and I don't see a way to get my user ID. Am I missing something?

darkpixel avatar Oct 12 '16 02:10 darkpixel

Temp work-around:

var toggl = new api({apiToken: process.env.TOGGL_API_KEY});

var req = {
  method: 'GET',
  auth: {}
};

req.auth[process.env.TOGGL_API_KEY] = 'api_token';

toggl.apiRequest('/api/v8/me', req, function(err, authdata) {
  if (err) {
    debug(err);
  } else {
    // do whatever
  }
});

darkpixel avatar Oct 12 '16 03:10 darkpixel

@darkpixel I'm kind of late here, sorry. Is it still actual?

estliberitas avatar Aug 03 '17 00:08 estliberitas

It may be, but I switched jobs and no longer have to provide reports based on Toggl data. ;)

darkpixel avatar Aug 03 '17 01:08 darkpixel

I can confirm that using the authenticate(callback) method does still return the same error that @darkpixel mentioned: Error: No need to authenticate thus you use apiToken.

However, using the getUserData(options, callback) method will give you your user id in the JSON response object under the id property like so:

const config = require('./config')
const TogglClient = require('toggl-api')
const toggl = new TogglClient({apiToken: config.togglToken})

toggl.getUserData({}, (err, userData) => {
  if (err) console.log(err)

  console.log(userData.id)
})

EDIT: It appears PR #11 should fix the authenticate(callback) method I referred to.

4lch4 avatar Nov 28 '17 12:11 4lch4